Class Solution

java.lang.Object
g2601_2700.s2680_maximum_or.Solution

public class Solution extends Object
2680 - Maximum OR.<p>Medium</p> <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code> and an integer <code>k</code>. In an operation, you can choose an element and multiply it by <code>2</code>.</p> <p>Return <em>the maximum possible value of</em> <code>nums[0] | nums[1] | ... | nums[n - 1]</code> <em>that can be obtained after applying the operation on nums at most</em> <code>k</code> <em>times</em>.</p> <p>Note that <code>a | b</code> denotes the <strong>bitwise or</strong> between two integers <code>a</code> and <code>b</code>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> nums = [12,9], k = 1</p> <p><strong>Output:</strong> 30</p> <p><strong>Explanation:</strong> If we apply the operation to index 1, our new array nums will be equal to [12,18]. Thus, we return the bitwise or of 12 and 18, which is 30.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> nums = [8,1,2], k = 2</p> <p><strong>Output:</strong> 35</p> <p><strong>Explanation:</strong> If we apply the operation twice on index 0, we yield a new array of [32,1,2]. Thus, we return 32|1|2 = 35.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= nums.length <= 10<sup>5</sup></code></li> <li><code>1 <= nums[i] <= 10<sup>9</sup></code></li> <li><code>1 <= k <= 15</code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • maximumOr

      public long maximumOr(int[] nums, int k)