
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
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 Articles
- If 5 slices of pizza are to be distributed equally among 5 boys, how many slices will each one get?
- Maximize the maximum among minimum of K consecutive sub-arrays in C++
- Minimize the total number of teddies to be distributed in C++
- C++ program to find maximum how many chocolates we can buy with at most k rupees
- Fruits collected from a farmhouse were $\frac{1028}{5}$. This is to be distributed equally among 17 houses. How much fruit will each household receive?
- Maximum Number of Events That Can Be Attended in C++
- Maximum number of candies that can be bought in C
- Find prime number K in an array such that (A[i] % K) is maximum in C++
- Maximum segment value after putting k breakpoints in a number in C++
- Maximum elements that can be made equal with k updates in C++
- Maximum number of trailing zeros in the product of the subsets of size k in C++
- Find Maximum number possible by doing at-most K swaps in C++
- There are 312, 260 and 156 students in class 6,7 and 8 respectively. Buses are to be hired to take these students to a picnic. Find the maximum number of students who can sit in a bus if each bus takes equal number of students. Find also the number of buses required.
- K-Concatenation Maximum Sum in C++
- What is the maximum number of electrons which can be accommodated in the K shell of an atom?
