This is an interesting easy problem.

class Solution {
fun bitwiseComplement(N: Int): Int {
if(N == 0) return 1

var n = N
var x = 1
var output = N

while(n > 0){
output = output xor x
n = n shr 1
x = x shl 1
}
return output
}
}

Runtime: 120 ms, faster than 100.00% of Kotlin online submissions for Complement of Base 10 Integer.
Memory Usage: 31.2 MB, less than 100.00% of Kotlin online submissions for Complement of Base 10 Integer.


The idea is to have two counters. First one shifts to the right until everything becomes zero. This signals that we are at the end of our bits. Second one shifts to the left in which we flip the bits one by one. We can do these steps at the same time in every loop.

output xor x flips the bit accordingly.