Apply Operations on Array to Maximize Sum of Squares - Problem

You're given a 0-indexed integer array nums and a positive integer k. Your goal is to strategically manipulate the array using bitwise operations to maximize the sum of squares of the k largest elements.

Available Operation: Choose any two distinct indices i and j, then simultaneously:

  • Update nums[i] to nums[i] AND nums[j]
  • Update nums[j] to nums[i] OR nums[j]

You can perform this operation any number of times. The key insight is that this operation allows you to redistribute bits between array elements - the AND operation concentrates common bits in one element, while the OR operation preserves all bits in the other.

After all operations, select k elements and return the maximum possible sum of their squares modulo 109 + 7.

Example: With nums = [2, 7, 1], k = 2, you can transform the array to maximize two squares by concentrating bits optimally.

Input & Output

example_1.py — Basic Case
$ Input: {"nums": [2, 7, 1], "k": 2}
Output: 58
💡 Note: We can redistribute bits optimally: count bits at each position (pos 0: 2, pos 1: 2, pos 2: 1), then create two numbers [7, 3] by assigning bits greedily. Sum of squares: 7² + 3² = 49 + 9 = 58.
example_2.py — Single Element
$ Input: {"nums": [1, 2, 3, 4, 5], "k": 3}
Output: 261
💡 Note: Bit counts: pos 0: 3, pos 1: 2, pos 2: 2. Optimal assignment creates [7, 6, 4]. Sum: 7² + 6² + 4² = 49 + 36 + 16 = 101. Wait, let me recalculate: [1,2,3,4,5] has bits [001,010,011,100,101]. Total bits: pos0: 3, pos1: 2, pos2: 2. Creating [7,6,4]: 49+36+16=101.
example_3.py — Edge Case k=1
$ Input: {"nums": [1, 15, 6], "k": 1}
Output: 225
💡 Note: With k=1, we can concentrate all bits into one number. Bit counts: pos 0: 2, pos 1: 2, pos 2: 2, pos 3: 1. Creates one optimal number: 15 (binary 1111). Sum: 15² = 225.

Visualization

Tap to expand
🏦 Digital Wealth Redistribution SystemOriginal AccountsPerson 12$2Person 27$7Person 31$1Total: $10Coin Inventory$4 coins: 1 total$2 coins: 2 total$1 coins: 2 totalBit positions: 2,1,0Optimized (k=2)Millionaire 17$7Millionaire 23$3Sum of Squares:7² + 3² = 49 + 9 = 58🎯 Key Strategy: Greedy High-Value Distribution1. Count coins by type (bit positions)2. Start with highest value coins ($4, then $2, then $1)3. Give each coin type to first k people who can receive it4. Result: Maximum possible squared wealth for k people⚡ Time: O(30n) | Space: O(1)
Understanding the Visualization
1
Inventory Count
Count how many coins of each type (bit position) exist across all people
2
Value Priority
Prioritize higher-value coins (more significant bits) for maximum impact
3
Greedy Distribution
Give the most valuable coins to your k chosen people first
4
Optimize Squares
This maximizes the sum of squared wealth across your k millionaires
Key Takeaway
🎯 Key Insight: AND/OR operations preserve total bit counts, so we can optimally redistribute bits by counting each position and greedily assigning them to maximize the sum of k squares.

Time & Space Complexity

Time Complexity
⏱️
O(30n)

30 iterations for each bit position times n elements to count bits

n
2n
Linear Growth
Space Complexity
O(1)

Only using arrays of size 30 for bit counts and k for result

n
2n
Linear Space

Constraints

  • 1 ≤ nums.length ≤ 105
  • 1 ≤ nums[i] ≤ 109
  • 1 ≤ k ≤ nums.length
  • Answer must be returned modulo 109 + 7
Asked in
Google 42 Microsoft 38 Amazon 31 Meta 24
26.1K Views
Medium-High Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen