Sum vs XOR

  • + 0 comments

    Python best solution

    If you’re looking for solutions to the 3-month preparation kit in either Python or Rust, you can find them below: my solutions

    def sum_xor(n):
        # Time complexity: O(log(n))
        # Space complexity (ignoring input): O(1)
        if n == 0:
            return 1
        # count 0 in binary of number
        count = 0
        while n > 0:
            if n & 1 == 0:
                count += 1
            n = n >> 1
    
        return 2**count