Boxes Of Chocolates
This is the time when Dr. Stephen wants to distribute chocolates. He has N number of boxes in a row, and each box contains some chocolates. Now, He wants to distribute chocolates to K children keeping in mind that distribution should be as fair as possible. To fairly distribute the gift boxes, he decided to give the maximum number of chocolates equally to K children.
Formally, There are N boxes with a different number of chocolates in them. The task is to divide the chocolates equally among K children where you can only choose consecutive boxes(subarray) to distribute chocolates. You need to print the maximum number of chocolates each child can get.
For Example: for the given boxes of chocolates [3,2,2,5,4,1] and 5 students.
Output: 2
If there is no restriction on choosing the boxes then the maximum number of chocolates the 5 students equally can have is 3 by picking all boxes except the box at index 2(0-based indexing). So total candies will be 3+2+5+4+1 = 15 and each of the 5 students will get 15/5=3 candies.
But we are allowed to choose only consecutive boxes. So if we choose boxes [0,1] then 3+2=5 then each student will have only 1 chocolate and when we choose boxes[4,6] as 5+4+1=10 then each student will have 2 chocolates. So the maximum number of chocolates each student can get is 2.
Input Format:
The first line of input contains a single integer T, representing the number of test cases or queries to be run.
Then the T test cases follow.
The first line of each test case contains two single space-separated integers N, and K, denoting the total number of boxes and the total number of students respectively.
The second line of each test case contains 'N' single space-separated integers representing the count of chocolates in each of the N boxes.
Output Format:
For each test case, print an integer denoting the maximum number of chocolates each child can get in a single line
Constraints:
1 <= T <= 10
1 <= N <= 10^5
1 <= K <= 10^5
0 <= boxes[i] <= 10^5
Time Limit: 1 sec
CodingNinjas
author
2y
Step 1- Sorted the array.
Step 2 - applied two pointer approach i.e took two integers i and j, point i to first element while pointing j to the last element of the array.
Step 3 - in every step do count...read more
CodingNinjas
author
2y
Brute Force
- The naive solution is to check whether each subarray sum is divisible by K or not. And then find the maximum of the sum/K from all subarray sum.
- To find the subarray we will run two loops in...read more
CodingNinjas
author
2y
Hash Map
The better idea is to create the sum array in which sum[i] stores the sum from (arr[0]....+arr[i]) and a hash table storing remainder as key and values as an index which represents the first i...read more
Add answer anonymously...
Top Samsung Research R&D Intern interview questions & answers
Top HR questions asked in Samsung Research R&D Intern
>
Samsung Research R&D Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app