Input: s =10Output: 2Explanation:
* Elements of the array `A`for`n = 2`**:***`A[0][0][0] = 0 * (0 OR 0) = 0`*`A[0][0][1] = 0 * (0 OR 1) = 0`*`A[0][1][0] = 0 * (1 OR 0) = 0`*`A[0][1][1] = 0 * (1 OR 1) = 0`*`A[1][0][0] = 1 * (0 OR 0) = 0`*`A[1][0][1] = 1 * (0 OR 1) = 1`*`A[1][1][0] = 1 * (1 OR 0) = 1`*`A[1][1][1] = 1 * (1 OR 1) = 1`* The total sum of the elements in array `A`is3, which does not exceed 10, so the maximum possible value of `n`is2.
Example 2:
1
2
3
4
5
6
Input: s =0Output: 1Explanation:
* Elements of the array `A`for`n = 1`:*`A[0][0][0] = 0 * (0 OR 0) = 0`* The total sum of the elements in array `A`is0, which does not exceed 0, so the maximum possible value of `n`is1.
We need to maximize n such that the sum of all A[i][j][k] = i * (j | k) for 0 <= i, j, k < n does not exceed s. The sum can be computed efficiently using bitwise properties, and we can use binary search to find the largest valid n.
classSolution {
public:longlong calc(int n) {
longlong res =0;
for (int i =0; i < n; ++i) {
longlong sum =0;
for (int b =0; b <32; ++b) {
longlong cnt =2LL* n * ((n >> b) << b) - ((n >> b) << (b +1)) + ((n & ((1<< b) -1)) +1) * ((n & ((1<< b) -1)) +1);
sum += (1LL<< b) * cnt;
}
res += i * sum;
}
return res;
}
intmaximumSizedArray(longlong s) {
int l =1, r =1000000, ans =1;
while (l <= r) {
int m = (l + r) /2;
if (calc(m) <= s) ans = m, l = m +1;
else r = m -1;
}
return ans;
}
};
classSolution {
privatelongcalc(int n) {
long res = 0;
for (int i = 0; i < n; ++i) {
long sum = 0;
for (int b = 0; b < 32; ++b) {
long cnt = 2L * n * ((n >> b) << b) - ((n >> b) << (b + 1)) + ((n & ((1 << b) - 1)) + 1) * ((n & ((1 << b) - 1)) + 1);
sum += (1L << b) * cnt;
}
res += i * sum;
}
return res;
}
publicintmaximumSizedArray(long s) {
int l = 1, r = 1000000, ans = 1;
while (l <= r) {
int m = (l + r) / 2;
if (calc(m) <= s) { ans = m; l = m + 1; }
else r = m - 1;
}
return ans;
}
}
classSolution {
privatefuncalc(n: Int): Long {
var res = 0Lfor (i in0 until n) {
var sum = 0Lfor (b in0 until 32) {
val cnt = 2L * n * ((n shr b) shl b) - ((n shr b) shl (b + 1)) + ((n and ((1 shl b) - 1)) + 1).toLong() * ((n and ((1 shl b) - 1)) + 1)
sum += (1L shl b) * cnt
}
res += i * sum
}
return res
}
funmaximumSizedArray(s: Long): Int {
var l = 1; var r = 1000000; var ans = 1while (l <= r) {
val m = (l + r) / 2if (calc(m) <= s) { ans = m; l = m + 1 }
else r = m - 1 }
return ans
}
}
classSolution:
defmaximumSizedArray(self, s: int) -> int:
defcalc(n: int) -> int:
res =0for i in range(n):
sum_ =0for b in range(32):
cnt =2* n * ((n >> b) << b) - ((n >> b) << (b +1)) + ((n & ((1<< b) -1)) +1) **2 sum_ += (1<< b) * cnt
res += i * sum_
return res
l, r, ans =1, 10**6, 1while l <= r:
m = (l + r) //2if calc(m) <= s:
ans = m
l = m +1else:
r = m -1return ans
impl Solution {
pubfnmaximum_sized_array(s: i64) -> i32 {
fncalc(n: i32) -> i64 {
letmut res =0i64;
for i in0..n {
letmut sum =0i64;
for b in0..32 {
let cnt =2* n asi64* (((n >> b) << b) asi64) - (((n >> b) << (b +1)) asi64) + (((n & ((1<< b) -1)) +1) asi64).pow(2);
sum += (1i64<< b) * cnt;
}
res += i asi64* sum;
}
res
}
let (mut l, mut r, mut ans) = (1, 1_000_000, 1);
while l <= r {
let m = (l + r) /2;
if calc(m) <= s {
ans = m;
l = m +1;
} else {
r = m -1;
}
}
ans
}
}