Skip to content

Commit 0432065

Browse files
authored
Create 2897-apply-operations-on-array-to-maximize-sum-of-squares.js
1 parent 657ff12 commit 0432065

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} k
4+
* @return {number}
5+
*/
6+
const maxSum = function(nums, k) {
7+
const n = nums.length, mod = BigInt(1e9 + 7)
8+
let res = 0n
9+
const stk = Array(n).fill(0), idx = Array(32).fill(0)
10+
for(const e of nums) {
11+
for(let i = 0; i < 32; i++) {
12+
if((e >> i) & 1) {
13+
stk[idx[i]] += (1 << i)
14+
idx[i]++
15+
}
16+
}
17+
}
18+
19+
for(let i = 0; i < k; i++) {
20+
res += BigInt(stk[i]) * BigInt(stk[i])
21+
res %= mod
22+
}
23+
24+
return Number(res)
25+
};

0 commit comments

Comments
 (0)