A train follows a route in which it visits ‘N’ stations, and the train keeps moving back and forth on the track. The train moves from station 1 to station ‘N’, from ‘N’ to 1, and again from 1 to ‘N’. It moves on forever like this.
Train stops exactly ‘V[i]’ minutes on the ‘ith’ platform. ‘V’ is an array of ‘N’ length. The train moves very fast, so it takes no time to travel from one station to another.
You are given ‘Q’ queries in the array ‘query’. For the ‘ith’ query, you need to find the station on which the train is standing on ‘query[i]ith’ minute. The train starts its journey in the 0th minute.
Your task is to find the station where the train is present after each query.
Example :‘N’ = 4, ‘V’ = [4,3,1,2], ‘Q’ = 3, ‘query’ = [5, 12, 3].
Train visits the station in the following manner:
For the period [0, 3], the train will be at station 1. (0th, 1st, 2nd, and 3rd) for 4 minutes on station 1.
For the period [4, 6], the train will be at station 2.
For the period [7, 7], the train will be at station 3.
For the period [8, 9], the train will be at station 4.
For the period [10, 11], the train will be at station 4.
For the period [12, 12], the train will be at station 3.
First Query: In the 5th minute, the train will be at station number 2. Hence, 2 will be the answer.
Second query: In the 12th minute, the train will be at station number 3. Hence, 3 will be the answer.
Third Query: In the 3rd minute, the train will be at station number 1. Hence, 1 will be the answer.
The final output will be: [2, 3, 1]
The first line contains a single integer ‘T’ denoting the number of test cases, then the test case follows.
The first line of each test case contains an integer, denoting ‘N', length of ‘V’.
The second line contains ‘N’ single space-separated integers denoting ‘V’.
The third line contains an integer, denoting ‘Q’.
The fourth line contains ‘Q’ single space-separated integers denoting ‘query’.
Output Format :
For each test case, return an array that contains answers to all queries.
Output for each test case will be printed on a separate line.
Note :
You are not required to print anything; it has already been taken care of. Just implement the function.
1 ≤ T ≤ 10
2 ≤ N ≤ 10^5
1 ≤ V[i] ≤ 10^3
1 ≤ Q ≤ 10^5
0 ≤ query[i] ≤ 10^9
It’s guaranteed that sum of N and Q over all test cases does not exceed 10^5.
Time limit: 1 sec
2
3
1 2 3
3
5 2 11
2
1 7
3
1 0 10
3 2 1
2 1 2
For test case 1:
Train will visit the stations in following order:
Station 1:- [0,0].
Station 2:- [1,2].
Station 3:- [3,5].
Station 3:- [6,8].
Station 2:- [9,10].
Station 1:- [11,11].
So, the 5th minute train will be at station 3.
On 2rd minute the train will be at station 2.
In the 11th minute the train will be at station 1.
Hence, the output will be [3, 2, 1].
For test case 2:
The train will visit the stations in the following order:
Station 1:- [0,0].
Station 2:- [1,7].
Station 2:- [8,14].
Station 1:- [15,15].
On, 1st-minute train will be on station 2.
On, the 0th-minute train will be on station 1.
In the 10th minute, the train will be at station 2.
Hence, the answer is [2, 1, 2].
2
5
1 2 3 4 5
1
20
2
5 6
2
3 6
4
1 2
How can you calculate the number of times the train performs a complete journey (visit every station)?
Approach:
Algorithm:
O(Q * logN), where ‘Q’ is the number of queries and ‘N’ is the length of array ‘V’.
We are iterating over queries, and for each query, we perform a binary search on an array of size ‘N’.
Hence, the time complexity is O(Q * logN).
O(N), where ‘N’ is the array's length ‘V’.
We calculate prefix and suffix sum, which will take linear space to store. Hence, the space complexity is O(N).