- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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

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

#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

- Related Questions & Answers
- Minimize the total number of teddies to be distributed in C++
- Maximize the maximum among minimum of K consecutive sub-arrays in C++
- C++ program to find maximum how many chocolates we can buy with at most k rupees
- Maximum Number of Events That Can Be Attended in C++
- Maximum number of candies that can be bought in C
- Maximum value of XOR among all triplets of an array in C++
- C++ code to find minimum k to get more votes from students
- Minimum number of elements to be removed to make XOR maximum using C++.
- Maximum elements that can be made equal with k updates in C++
- Maximum product of subsequence of size k in C++
- Maximum number of partitions that can be sorted individually to make sorted in C++
- Maximum number of trailing zeros in the product of the subsets of size k in C++
- Find maximum number that can be formed using digits of a given number in C++
- K-Concatenation Maximum Sum in C++
- Maximum array sum that can be obtained after exactly k changes in C++