Kevin And His Fruits

Moderate
0/80
Average time to solve is 25m
profile
Contributed by
4 upvotes
Asked in companies
AmazonAppleOLX Group

Problem statement

Kevin has ‘N’ buckets each consisting of some fruits. Kevin wants to eat at least ‘M’ fruits and so, he decided to set a marker (integer) as maximum as possible such that if he eats “number of fruits in the i-th bucket - marker” fruits then it must be at least ‘M’.

Example:

Let’s assume, we have 5 buckets each having 10, 40, 30, and 20 fruits respectively. And, Kevin wants to eat at least 30 fruits. 

Now, if we set our marker at 20 then Kevin can eat: (10 - 20) = -10 => 0 (fruit’s count can’t be negative) fruits from 1st bucket, (40 - 20) = 20 fruits from 2nd bucket, (30 - 20) = 10 fruits from the third bucket, and (20 - 20) = 0 fruits from the last bucket.

So, he can eat 0 + 20 + 10 + 0 = 30 fruits in total.
Note:
Each bucket may not have the same amount of fruits. It is guaranteed that the sum of all fruits (including all the buckets) will be greater than M. Kevin can only eat fruits from a particular bucket if and only if the bucket has more fruits than the marker.
Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line contains a single integer ‘T’ representing the number of test cases. 

The first line of each test case will contain two space-separated integers ‘N’ and ‘M’ where ‘N’ is the total number of buckets, and ‘M’ is the minimum number of fruits that Kevin wants to eat.

The second line of each test case will contain ‘N’ space-separated integers which denote the number of fruits in the i-th bucket.
Output Format:
For each test case, print the required marker.

Output for every test case will be printed in a separate line.
Constraints:
1 <= T <= 10
1 <= N <= 10^4
1 <= M <= 10^6
0 <= ARR[i] <= 10^4

Where ‘T’ is the number of test cases.

Where 'N' is the number of buckets and ‘M’ is the integer representing the minimum number of fruits Kevin wants to eat. And, ARR[i] denotes the number of fruits in the i-th bucket.

Time limit: 1 sec
Sample Input 1:
2
4 30
10 40 30 20
4 16
5 8 20 1   
Sample Output 1:
20
6
Explanation of sample input 1:
In the first test case, if we set the marker at 20 then Kevin can eat following fruits from each bucket: 0 (1st bucket) + 20 (2nd bucket) + 10 (3rd bucket) + 0 (4th bucket) = 30 (K)

In the second test case, if we set the marker at 6 then Kevin can eat following fruits from each bucket: 0 (1st bucket) + 2 (2nd bucket) + 14 (bucket) + 0 (4th bucket) = 16 (K)
Sample Input 2:
3
3 2
2 2 2
4 1
2 0 1 0
5 15
1 2 3 4 5
Sample Output 2:
1
1
0
Explanation for sample input 2:
In the first test case, for the marker at 1 Kevin has to eat,  1 + 1 + 1 = 3 fruits.

In the second test case, for the marker at 1 Kevin has to eat, 1 + 0 + 0 + 0 = 1 fruit.

In the third test case, for the marker at 0 Kevin has to eat, 1 + 2 + 3 + 4 + 5 = 15 fruits.
Hint

The marker will be anywhere in between 0 and maximum fruits in any particular bucket.

Approaches (2)
Brute Force

First, we should know that the marker can’t be negative and also it will not be greater than the maximum number of fruits in a particular bucket because if this happens then Kevin are not able to eat any of these fruits.

 

 Therefore, the marker must be in the range of 0 to a maximum number of fruits in a particular bucket. So, the idea is to first calculate the maximum number of fruits in any particular bucket (say, maxx). After that, iterate over the range “maxx” to 0 and search for the maximum value of marker at which Kevin has to eat at least M fruits.

Algorithm:

  1. Iterate over the array to find the max element, let’s name it “maxx”.
  2. Now, iterate over the range of “maxx” to 0 (assume ‘i’ be the iterator)
  3. Create a counter variable and initialize it with 0
  4. Now, iterate over the given array (assume ‘j’ be the iterator)
    • If, arr[j] - i > 0 then count += arr[j] - i
  5. If, count >= M (minimum fruits that Kevin wants to eat) then return count.
Time Complexity

O(N * X), where ‘N’ is the total number of buckets and ‘X’ is the maximum number of fruits in any particular bucket.

 

Since we are just searching for the marker in range “maxx” to 0 and running a loop on all the buckets, so the overall time complexity will be O(N * X).

Space Complexity

O(1).

 

Since we are not using any extra space, the overall space complexity will be O(1).

Code Solution
(100% EXP penalty)
Kevin And His Fruits
Full screen
Console