Author: D. Vishnu Vardhan reddy
Tester: D. Vishnu Vardhan reddy
Editorialist: D. Vishnu Vardhan reddy
Given zombies walking in left or right direction indicated by -1 and +1, find the last zombie to die if they walk towards the left and right edges respectively. When a left zombie meets with a right zombie, they change their directions only.
Count the number of zombies walking towards the left and let it be count_left. N- count_left gives the number of zombies walking towards right, that is count_right.
Whenever the zombies reach the ends, they’ll die. Counting the number of left and right zombies helps to eliminate the one on the left and one on the right simultaneously. Subtracting the total number of zombies by the larger count value gives the index of the required zombie.
If count_left < count_right , N+1 -count_right gives the index of the last zombie to die.
Else , N+1 -count_left gives the index of last zombie to die.
AUTHOR’S AND TESTER’S SOLUTIONS:
Author’s solution can be found here.