# Maximum number of chocolates to be distributed equally among k students in C

CServer Side ProgrammingProgramming

#### C in Depth: The Complete C Programming Guide for Beginners

45 Lectures 4.5 hours

#### Practical C++: Learn C++ Basics Step by Step

Most Popular

50 Lectures 4.5 hours

#### Master C and Embedded C Programming- Learn as you go

Best Seller

66 Lectures 5.5 hours

We are given a number of chocolates present in consecutive boxes in the form of an array and a number k which represents the number of students among which these chocolates will be distributed. The task here is to choose consecutive boxes such that the sum of chocolates present in them can be equally distributed among k students. Also we have to make sure that the number of chocolates is maximum.

For this we will traverse the array from left to right and start adding the number of chocolates and divide the sum by k. If it is fully divided with remainder equal to 0 then store this sum in a variable. As we move further we repeat this process until we have the maximum such sum obtained.The problem is to find maximum sum subarray divisible by k.

Input

Choco[]={ 1,2,4,5,2,8,3,5 } k=3

Output −Maximum number of chocolates to be distributed equally among k students − 5

Explanation − Maximum sum subarray is { 5,2,8 }. Sum of chocolates is 15. Dividing equally, the maximum chocolates all 3 students get is 5.

Note − boxes are consecutive and indexes are { 3,4,5 }

Input

Choco[] = { 2,3,7,5,4,8,2,6 } k=5

Output −Maximum number of chocolates to be distributed equally among k students − 7

Explanation − Maximum sum subarray is { 3,7,5,4,8,2,6 }. Sum of chocolates is 35.

Dividing equally, the maximum chocolates all 5 students get is 7.

## Approach used in the below program is as follows

• We take an integer array arr[] which contains a number of chocolate in consecutive containers.

• The number of elements ‘n’ represents the number of boxes.

• Take no. of students ‘k’ as input.

• The function maxChocolate( int arr[], int n, int k ), takes three arguments − the array, its size and no. of students k.

• We will start traversing the arr[] from beginning using for loop.

• Take two variables sum and maxSum. Sum stores the sum of consecutive elements of subarray.

• maxSum is used to store the maximum sum found so far.

• Inside nested for loop keep adding the elements and check if sum%k gives remainder 0.

Also if this sum > maxSum, update maxSum.

• At-last maxSum will have a maximum no. of chocolates that can be equally divided among k students.

• Return the result as maxSum/k which is the number of chocolates each student gets.

## Example

Live Demo

#include <stdio.h>
// to find the maximum number
// of chocolates to be distributed equally among
// k students
int maxChocolates(int arr[], int n, int k){
int sum;
int maxSum = 0;
for(int i=0;i<n;i++){
sum=0;
for(int j=i;j<n;j++){
sum+=arr[j];
if(sum%k==0 && sum>maxSum)
maxSum=sum;
}
}
// distributed equally among 'k' students
return (maxSum / k);
}
int main(){
int arr[] = { 2, 7, 6, 1, 4, 5 ,5, 3 };
int n =8;
int k =3;
printf("Maximum number of chocolates to be distributed equally among k students: %d   ",maxChocolates(arr, n, k));
return 0;
}

## Output

If we run the above code it will generate the following output −

Maximum number of chocolates to be distributed equally among k students − 11
Updated on 14-Aug-2020 08:00:34