Maximize the Sum of Squares After Bitwise Pair Operations
Quick Overview
Choose exactly k final array elements after any number of simultaneous bitwise AND and OR pair operations, maximizing their squared sum modulo the stated modulus.
Maximize the Sum of Squares After Bitwise Pair Operations
Company: Google
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Other
# Maximize the Sum of Squares After Bitwise Pair Operations
You are given a 0-indexed integer array `nums` and a positive integer `k`.
You may perform the following operation any number of times, including zero times. Choose two distinct indices `i` and `j`, and simultaneously replace their values using the old values:
- Set `nums[i]` to `nums[i] AND nums[j]`.
- Set `nums[j]` to `nums[i] OR nums[j]`.
Here `AND` and `OR` are bitwise operations. After completing the operations, choose exactly `k` array elements and sum their squares.
Return the maximum possible sum of squares, modulo `10^9 + 7`. The objective is the maximum actual sum; apply the modulus to that result.
### Examples
**Example 1**
```text
Input: nums = [1,2,4], k = 2
Output: 49
```
An operation with `(i,j) = (0,1)` changes the array to `[0,3,4]`. An operation with `(i,j) = (1,2)` then gives `[0,0,7]`. Choosing `7` and one zero gives `49`. Each of the three available bit positions occurs only once, so the largest possible squared-sum arrangement concentrates them in `7`.
**Example 2**
```text
Input: nums = [3,5,6], k = 2
Output: 98
```
Operations with `(i,j) = (0,1)` and then `(0,2)` produce `[1,7,6]` and `[0,7,7]`. Choose the two sevens for `49 + 49 = 98`. Each of the three bit positions occurs twice, allowing two values of `7` but no larger value.
### Constraints
- `1 <= k <= nums.length <= 10^5`.
- `1 <= nums[i] <= 10^9` initially.
### Clarifying Questions
- Do both assignments in an operation use the original pair values? Yes; they occur simultaneously.
- Must the two indices be distinct? Yes.
- Must exactly `k` positions be chosen, even if some final values are zero? Yes. Operations may create zero values despite the positive initial bounds.
- Do repeated equal values represent different selectable elements? Yes; selection is by distinct array positions.
- Are operations or selected positions required as output? No; return only the maximum sum modulo the specified modulus.
### C++ Interface
```cpp
class Solution {
public:
int maxSum(vector<int>& nums, int k) {
}
};
```
### Reasoning Checkpoints
- Identify what an operation preserves and which final values it can create.
- Justify both reachability and optimality of the selected final values.
- Respect exact selection count, simultaneous updates, and arithmetic limits before taking the modulus.
```hint Count bits and concentrate them
Count the occurrences of each set bit across the array. What happens to the sum of squares when an AND/OR operation gives the OR to the larger of two nonnegative values? Try forming one largest remaining value at a time.
```
Overview: Choose exactly k final array elements after any number of simultaneous bitwise AND and OR pair operations, maximizing their squared sum modulo the stated modulus.
Maximize the Sum of Squares After Bitwise Pair Operations
Google
Source window: 0–3 months · as of Oct 21, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0
Maximize the Sum of Squares After Bitwise Pair Operations
You are given a 0-indexed integer array nums and a positive integer k.
You may perform the following operation any number of times, including zero times. Choose two distinct indices i and j, and simultaneously replace their values using the old values:
Set
nums[i]
to
nums[i] AND nums[j]
.
Set
nums[j]
to
nums[i] OR nums[j]
.
Here AND and OR are bitwise operations. After completing the operations, choose exactly k array elements and sum their squares.
Return the maximum possible sum of squares, modulo 10^9 + 7. The objective is the maximum actual sum; apply the modulus to that result.
Examples
Example 1
Input: nums = [1,2,4], k = 2
Output: 49
An operation with (i,j) = (0,1) changes the array to [0,3,4]. An operation with (i,j) = (1,2) then gives [0,0,7]. Choosing 7 and one zero gives 49. Each of the three available bit positions occurs only once, so the largest possible squared-sum arrangement concentrates them in 7.
Example 2
Input: nums = [3,5,6], k = 2
Output: 98
Operations with (i,j) = (0,1) and then (0,2) produce [1,7,6] and [0,7,7]. Choose the two sevens for 49 + 49 = 98. Each of the three bit positions occurs twice, allowing two values of 7 but no larger value.
Constraints
1 <= k <= nums.length <= 10^5
.
1 <= nums[i] <= 10^9
initially.
Clarifying Questions Guidance
Do both assignments in an operation use the original pair values? Yes; they occur simultaneously.
Must the two indices be distinct? Yes.
Must exactly
k
positions be chosen, even if some final values are zero? Yes. Operations may create zero values despite the positive initial bounds.
Do repeated equal values represent different selectable elements? Yes; selection is by distinct array positions.
Are operations or selected positions required as output? No; return only the maximum sum modulo the specified modulus.
C++ Interface
class Solution {
public:
int maxSum(vector<int>& nums, int k) {
}
};
Reasoning Checkpoints
Identify what an operation preserves and which final values it can create.
Justify both reachability and optimality of the selected final values.
Respect exact selection count, simultaneous updates, and arithmetic limits before taking the modulus.