Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
[ad_1]
Two gamers, Participant 1 and Participant 2, are given an integer N to play a sport. The foundations of the sport are as follows :
Examples:
Enter: N = 8
Output: 1
Rationalization: N = 8
N = 1000 (binary)
Participant 1 takes the bit.
The remaining bits are all zero.
Participant 2 can not make a transfer,
so Participant 1 wins.Enter: N = 3
Output: 2
Method: The given drawback might be solved by following the beneath concept:
Calculate the variety of set bits in N. If the variety of set bits is odd then participant 1 will all the time win [because he will take the following turns – 1st, 3rd, 5th, . . . and any odd turn]. In any other case, participant 2 will win the sport.
Comply with the steps talked about beneath to implement the:
Beneath is the implementation of the above strategy.
|
Time Complexity: O(log N)
Auxiliary House: O(1)
[ad_2]