barrettruth.com/public/code/algorithms/leetcode-daily/minend.cpp
2025-04-15 13:43:37 -04:00

15 lines
291 B
C++

long long minEnd(int n, long long x) {
int bits_to_distribute = n - 1;
long long mask = 1;
while (bits_to_distribute > 0) {
if ((x & mask) == 0) {
if ((bits_to_distribute & 1) == 1)
x |= mask;
bits_to_distribute >>= 1;
}
mask <<= 1;
}
return x;
}