Chocolate Fest

Moderate
0/80
Average time to solve is 25m
profile
Contributed by
33 upvotes
Asked in companies
AmazonOYOZoho Corporation

Problem statement

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.

Detailed explanation ( Input/output format, Notes, Images )
Input Format:
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.
Constraints:
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
Sample Input 1:
2
6 9
1 3 5 6 4 3
3 10
11 2 5
Sample Output 1:
5 6
11
Explanation for Sample Input 1:
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.
Sample Input 2:
2
5 12
5 7 11 3 5
3 20
20 19 21
Sample Output 2:
7 11
21
Hint

Can you check all the ways to choose boxes and find the optimal one?

Approaches (2)
Brute force

Since we can choose only contiguous boxes, we can check all the options and find the most optimal solution.


 

The steps are as follows:

  • Initialize min_length as ‘N’, denoting the minimum boxes to get the sum of chocolates more than ‘X’.
  • Initialize starting_index as 0.
  • Traverse the array from i = 0 to i = N - 1, denoting the starting position of the choice.
    • Initialize curr_sum = 0, denoting the number of chocolates in this choice.
    • Traverse the array from j = i to j = N - 1, denoting the ending position of the choice.
    • Add arr[j] to curr_sum, so that it contains the sum of chocolates from box i to box j.
    • If the sum of chocolates is more than X and the number of boxes is less than min_length, update min_length and store starting_index as i.
  • Now we have a starting index signifying the starting index for the optimal choice and min_length. So, we insert the number of chocolates from box starting_index to starting_index + min_length and return the array.
Time Complexity

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).


 

Space Complexity

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).


 

Code Solution
(100% EXP penalty)
Chocolate Fest
Full screen
Console