Apply Operations on Array to Maximize Sum of Squares
HardUpdated: Aug 2, 2025
Practice on:
Problem
You are given a 0-indexed integer array nums and a positive integer k.
You can do the following operation on the array any number of times:
- Choose any two distinct indices
iandjand simultaneously update the values ofnums[i]to(nums[i] AND nums[j])andnums[j]to(nums[i] OR nums[j]). Here,ORdenotes the bitwiseORoperation, andANDdenotes the bitwiseANDoperation.
You have to choose k elements from the final array and calculate the sum of their squares.
Return themaximum sum of squares you can achieve.
Since the answer can be very large, return it modulo 10^9 + 7.
Examples
Example 1
Input: nums = [2,6,5,8], k = 2
Output: 261
Explanation: We can do the following operations on the array:
- Choose i = 0 and j = 3, then change nums[0] to (2 AND 8) = 0 and nums[3] to (2 OR 8) = 10. The resulting array is nums = [0,6,5,10].
- Choose i = 2 and j = 3, then change nums[2] to (5 AND 10) = 0 and nums[3] to (5 OR 10) = 15. The resulting array is nums = [0,6,0,15].
We can choose the elements 15 and 6 from the final array. The sum of squares is 152 + 62 = 261.
It can be shown that this is the maximum value we can get.
Example 2
Input: nums = [4,5,4,7], k = 3
Output: 90
Explanation: We do not need to apply any operations.
We can choose the elements 7, 5, and 4 with a sum of squares: 72 + 52 + 42 = 90.
It can be shown that this is the maximum value we can get.
Constraints
1 <= k <= nums.length <= 10^51 <= nums[i] <= 10^9
Solution
Method 1 – Bitwise Greedy Construction
Intuition
The key insight is that the allowed operation lets us "move" all set bits in the array into a single element, maximizing its value. By greedily collecting the most significant bits into up to k elements, we can maximize the sum of their squares.
Approach
- Count the number of times each bit (from 0 to 30) is set across all numbers in
nums. - For each of the
kelements we want to maximize, construct a number by greedily assigning the highest available bits (using up the bit counts). - For each constructed number, square it and sum up the results.
- Return the sum modulo
10^9 + 7.
Code
C++
class Solution {
public:
int maxSumSquares(vector<int>& nums, int k) {
const int MOD = 1e9 + 7;
vector<int> bitCnt(31, 0);
for (int x : nums)
for (int b = 0; b < 31; ++b)
if (x & (1 << b)) bitCnt[b]++;
vector<long long> vals(k, 0);
for (int b = 30; b >= 0; --b) {
int cnt = bitCnt[b];
for (int i = 0; i < k && cnt > 0; ++i, --cnt)
vals[i] |= (1LL << b);
}
long long ans = 0;
for (long long v : vals)
ans = (ans + v * v % MOD) % MOD;
return (int)ans;
}
};
Java
class Solution {
public int maxSumSquares(int[] nums, int k) {
int MOD = (int)1e9 + 7;
int[] bitCnt = new int[31];
for (int x : nums)
for (int b = 0; b < 31; ++b)
if ((x & (1 << b)) != 0) bitCnt[b]++;
long[] vals = new long[k];
for (int b = 30; b >= 0; --b) {
int cnt = bitCnt[b];
for (int i = 0; i < k && cnt > 0; ++i, --cnt)
vals[i] |= (1L << b);
}
long ans = 0;
for (long v : vals)
ans = (ans + v * v % MOD) % MOD;
return (int)ans;
}
}
Python
class Solution:
def maxSumSquares(self, nums: list[int], k: int) -> int:
MOD = 10**9 + 7
bit_cnt: list[int] = [0] * 31
for x in nums:
for b in range(31):
if x & (1 << b):
bit_cnt[b] += 1
vals: list[int] = [0] * k
for b in range(30, -1, -1):
cnt = bit_cnt[b]
for i in range(k):
if cnt == 0:
break
vals[i] |= (1 << b)
cnt -= 1
ans: int = 0
for v in vals:
ans = (ans + v * v) % MOD
return ans
Complexity
- ⏰ Time complexity:
O(31 * n + 31 * k)(since 31 bits per number and up to k elements) - 🧺 Space complexity:
O(31 + k)