Alex is taking part in a chocolate eating competition. There are ‘N’ boxes of chocolate numbered ‘0’ to ‘N - 1’. Each box contains some chocolates. The boxes are arranged in a line, with the box ‘0’ being the nearest to Alex. To win, Alex has to eat more than ‘X’ number of chocolates using minimum boxes. Alex can only eat chocolates from contiguous boxes. That is, he can choose some i and j (i <= j) and eat all the chocolates from box i, box i + 1, ..., box j. Alex is lazy, so if there are many optimal choices, he will choose the boxes nearest to him.
Given an array ‘choco’ containing the number of chocolates in each box, predict the boxes that Alex will choose. It is guaranteed that one such choice always exists.
The first line contains ‘T’, denoting the number of test cases.
The first line of each test case contains two integers, ‘N’ and ‘X’, denoting the number of boxes and the target, respectively.
The second line of each test case contains an array ‘choco’ of ‘N’ space separated integers, denoting the number of chocolates in each box.
Output Format:
For each test case, print an array of integers denoting the number of chocolates in the boxes that Alex will pick.
Note:
You are not required to print the expected output. It has already been taken care of. Just implement the function.
1 <= T <= 5
1 <= N <= 10^5
1 <= X <= 10^9
1<= choco[i] <= 10^4
Where ‘T’ is the number of test cases, ‘N’ is the number of boxes, ‘X’ is the target, and ‘choco[i]’ is the number of chocolates in the box ‘i’, where 0 <= i <= N - 1.
Time Limit: 1 sec
2
6 9
1 3 5 6 4 3
3 10
11 2 5
5 6
11
In the first test case, the number of chocolates is [1, 3, 5, 6, 4, 3]. We can see, Alex can eat more than 9 chocolates using a minimum of two boxes. Alex can use more boxes, but according to the question, he must use minimum boxes. So, he has the choice of picking boxes (2,3), (3,4). But since box two is nearest to him, he chooses box 2 and box 3. Thus the result is the chocolates in these boxes, this is, 5 and 6.
In the second test case, the number of chocolates is [11, 2, 5]. So Alex can eat more than 10 chocolates using one box only. He picks box 0. So the answer is 11.
2
5 12
5 7 11 3 5
3 20
20 19 21
7 11
21
Can you check all the ways to choose boxes and find the optimal one?
Since we can choose only contiguous boxes, we can check all the options and find the most optimal solution.
The steps are as follows:
O(N^2), where ‘N’ is the number of boxes.
We are checking every starting index from 0 to N - 1. And for each starting index, we are checking all the ending index from i to N - 1. So, the time needed is N + (N - 1) + … + 2 + 1 = N * (N + 1) / 2 = O(N^2). Hence, the overall complexity is O(N^2).
O(N), where ‘N’ is the number of boxes.
We are using an array to store the result. The maximum size of the array can be N. So, the overall space complexity is O(N).