-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path2897-apply-operations-on-array-to-maximize-sum-of-squares.js
More file actions
48 lines (44 loc) · 1.38 KB
/
2897-apply-operations-on-array-to-maximize-sum-of-squares.js
File metadata and controls
48 lines (44 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
* 2897. Apply Operations on Array to Maximize Sum of Squares
* https://leetcode.com/problems/apply-operations-on-array-to-maximize-sum-of-squares/
* Difficulty: Hard
*
* 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 i and j and simultaneously update the values of nums[i]
* to (nums[i] AND nums[j]) and nums[j] to (nums[i] OR nums[j]). Here, OR denotes the bitwise
* OR operation, and AND denotes the bitwise AND operation.
*
* You have to choose k elements from the final array and calculate the sum of their squares.
*
* Return the maximum sum of squares you can achieve.
*
* Since the answer can be very large, return it modulo 109 + 7.
*/
/**
* @param {number[]} nums
* @param {number} k
* @return {number}
*/
var maxSum = function(nums, k) {
const MOD = 1e9 + 7;
const bitCounts = new Array(30).fill(0);
for (const num of nums) {
for (let i = 0; i < 30; i++) {
if (num & (1 << i)) bitCounts[i]++;
}
}
let result = 0;
for (let i = 0; i < k; i++) {
let current = 0;
for (let j = 29; j >= 0; j--) {
if (bitCounts[j] > 0) {
current |= (1 << j);
bitCounts[j]--;
}
}
result = (result + Number((BigInt(current) * BigInt(current)) % BigInt(MOD))) % MOD;
}
return result;
};