Maximize the Bitwise OR Within a Doubling Budget

Quick Overview

Maximize the bitwise OR of a positive integer array using a global budget of at most k doublings. Operations may revisit the same position, and the returned value must preserve the full 64-bit result allowed by the input bounds.

Maximize the Bitwise OR Within a Doubling Budget

Company: Visa

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

# Maximize the Bitwise OR Within a Doubling Budget You are given a zero-indexed array `nums` of positive integers and an integer `k`. One operation chooses one array element and multiplies it by `2`. You may choose the same element more than once. Using at most `k` operations in total across the array, maximize the bitwise OR of all resulting elements. Return that maximum value. The symbol `|` denotes bitwise OR. ### Examples **Example 1** ```text Input: nums = [5,3], k = 2 Output: 23 ``` Doubling `5` twice gives `[20,3]`, whose OR is `23`. For comparison, doubling each element once gives `[10,6]`, whose OR is `14`. **Example 2** ```text Input: nums = [6,5,1], k = 1 Output: 15 ``` Doubling the middle element gives `[6,10,1]` and an OR of `15`. Doubling the first element instead gives an OR of `13`; doubling the last gives `7`. ### Constraints - `1 <= nums.size() <= 100000`. - `1 <= nums[i] <= 1000000000`. - `1 <= k <= 15`. ### Clarifying Questions - Is `k` a separate allowance for each element? No; it is one global operation budget. - Must different operations choose different positions? No; one position may be chosen repeatedly. - Can the result exceed a 32-bit signed integer? Yes; preserve the `long long` return type. - Is the modified array returned? No; only its maximum attainable OR is required. ### C++ Interface ```cpp class Solution { public: long long maximumOr(vector<int>& nums, int k) { } }; ``` ### Reasoning Checkpoints - Respect the global budget and the phrase “at most.” - Compare alternatives by the numerical value of their resulting OR. - Account for arithmetic width before carrying out any doubling or shift. ```hint Compare significant bits How does the highest set bit constrain the result of an operation allocation? ```

Overview: Maximize the bitwise OR of a positive integer array using a global budget of at most k doublings. Operations may revisit the same position, and the returned value must preserve the full 64-bit result allowed by the input bounds.

Visa logo
Visa
Source window: 0–6 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Maximize the Bitwise OR Within a Doubling Budget

You are given a zero-indexed array nums of positive integers and an integer k. One operation chooses one array element and multiplies it by 2. You may choose the same element more than once.

Using at most k operations in total across the array, maximize the bitwise OR of all resulting elements. Return that maximum value. The symbol | denotes bitwise OR.

Examples

Example 1

Input: nums = [5,3], k = 2
Output: 23

Doubling 5 twice gives [20,3], whose OR is 23. For comparison, doubling each element once gives [10,6], whose OR is 14.

Example 2

Input: nums = [6,5,1], k = 1
Output: 15

Doubling the middle element gives [6,10,1] and an OR of 15. Doubling the first element instead gives an OR of 13; doubling the last gives 7.

Constraints

  • 1 <= nums.size() <= 100000 .
  • 1 <= nums[i] <= 1000000000 .
  • 1 <= k <= 15 .

Clarifying Questions Guidance

  • Is k a separate allowance for each element? No; it is one global operation budget.
  • Must different operations choose different positions? No; one position may be chosen repeatedly.
  • Can the result exceed a 32-bit signed integer? Yes; preserve the long long return type.
  • Is the modified array returned? No; only its maximum attainable OR is required.

C++ Interface

class Solution {
public:
    long long maximumOr(vector<int>& nums, int k) {
        
    }
};

Reasoning Checkpoints

  • Respect the global budget and the phrase “at most.”
  • Compare alternatives by the numerical value of their resulting OR.
  • Account for arithmetic width before carrying out any doubling or shift.