Author: Nishchith Shetty
Tester: Neel Shah
Editorialist: Rushang Gajjal
Given a number X , take each digit in turn from left to right . For each digit follow that many black arrows in a row and then follow one white arrow , initial position being point A . We have to find the node on which BOND will land after performing the given operations on X.
We can Solve this problem in two ways : 1. straight off Implementation and 2. Observation.
Method 1: Implementation
Each element is the list [A,B,C,D,E,F,G] is denoted by its index i ranging from 0 to 6. So A -> 0, B -> 1 and so on.
For each node let’s store the next node it will reach if we follow the black arrow or the white arrow respectively.
l = [[1, 0], [3, 2], [4, 3], [2, 5], [6, 6], [0, 4], [5, 1]]
i.e. A -> l = [1,0] -> [ B , A ]. From A if we move along black arrow we reach B or if we move along white arrow we reach A.
Starting from A i.e 0 (start = 0), Now for each digit n in the integer X we perform n moves along black arrow and then move along a white arrow. We can do this by
for i in range(n): start = l[start] #move along black arrow start = l[start] #move along white arrow
Value of start is initially 0.
After the above operations we just print the element denoted by the start variable.
- Time Complexity: O(|X|)
Method 2: Observation
Similarly if we perform X moves along black arrows and then a move along white arrow we can notice that each node can be represented by the value obtained by X mod 7.
A -> (X mod 7 = 0)
C -> (X mod 7 = 1)
F -> (X mod 7 = 2)
D -> (X mod 7 = 3)
G -> (X mod 7 = 4)
B -> (X mod 7 = 5)
E -> (X mod 7 = 6)
- Time Complexity: O(1)
AUTHOR’S AND TESTER’S SOLUTIONS:
Author’s solution can be found here
Tester’s solution can be found here