PROBLEM LINK:
Contest Division 1
Contest Division 2
Contest Division 3
Contest Division 4
Setter: Utkarsh Gupta
Tester: Nishank Suresh, Utkarsh Gupta
Editorialist: Pratiyush Mishra
DIFFICULTY:
2060
PREREQUISITES:
Bitwise Operations
PROBLEM:
You are given 3 numbers A, B and C.
You want to make all these 3 numbers equal. To do this, you can perform any finite number of operations:
- In the i^{th} operation, you must add 2^{(i-1)} to any one of these 3 numbers.
Find whether you can make these 3 numbers equal in finite number of moves.
EXPLANATION:
Here in each operation we are adding a set bit as we move along from right to left. When we are on the i_{th} operation, i.e adding the i_{th} set bit, we look for all possibilities:
- In case all three numbers are already equal, we can end the operation and conclude that it is possible to make all three numbers equal.
- Else if any two of them have a set bit, we can simply add set bit to the number having the unset bit at i_{th} position, this way all three numbers will have same bit at the i_{th} position and we can move to the next bit position.
- Else if any two of them have unset bit, then we can add set bit to the number having the set bit at i_{th} position, to make it unset at that position. This way all three numbers will have unset bit at the i_{th} position and we can move to the next bit position.
- In any other case, i.e case when we have no set bits or all set bits, there is no way we can make all three numbers to have same bit at the i_{th} position so it won’t be possible to make the three numbers equal.
TIME COMPLEXITY:
O(log(N)), for each test case.
SOLUTION:
Editorialist’s Solution
Tester1’s Solution
Tester2’s Solution