Complement of Base 10 Integer
Real-world version: Flipping Every Bit of a ReadingThe statement here is our own: the same inputs, outputs and technique.
A meter reading n is held in binary with no leading zeros, so the reading 5 is held as 101.
Flipping the reading turns every 0 into a 1 and every 1 into a 0, across exactly the bits the reading is held in. Flipping 101 gives 010, which is the reading 2.
Return the reading that flipping produces. The reading 0 is held as the single bit 0, so flipping it gives 1.
Examples
Example 1
- Input
- n = 2
- Output
- 1
The reading 2 is held as 10. Flipping every bit gives 01, which is the reading 1.
Example 2
- Input
- n = 8
- Output
- 7
The reading 8 is held as 1000, and flipping every bit gives 0111.
Example 3
- Input
- n = 0
- Output
- 1
The reading 0 is held as the single bit 0, and flipping that bit gives 1.
Constraints
- 0 <= n <= 999999999
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def flip_reading(n: int) -> int:public int flipReading(int n)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 12 test cases: 12 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's