Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Maximum number of chocolates to be distributed equally among k students in C
We are given chocolates in consecutive boxes represented as an array and k students among which these chocolates will be distributed. The task is to find consecutive boxes such that the sum of chocolates can be equally distributed among k students with maximum chocolates per student.
This problem requires finding the maximum sum subarray whose sum is divisible by k. We traverse all possible subarrays, check if their sum is divisible by k, and track the maximum such sum.
Syntax
int maxChocolates(int arr[], int n, int k);
Algorithm
- Iterate through all possible starting positions of subarrays
- For each starting position, extend the subarray and calculate running sum
- Check if sum is divisible by k (sum % k == 0)
- Track the maximum sum found that is divisible by k
- Return maximum chocolates per student (maxSum / k)
Example
Let's implement the solution to find maximum chocolates that can be distributed equally −
#include <stdio.h>
// Function to find maximum chocolates per student
int maxChocolates(int arr[], int n, int k) {
int sum;
int maxSum = 0;
// Check all possible subarrays
for(int i = 0; i < n; i++) {
sum = 0;
for(int j = i; j < n; j++) {
sum += arr[j];
// Check if sum is divisible by k and greater than current max
if(sum % k == 0 && sum > maxSum) {
maxSum = sum;
}
}
}
// Return chocolates per student
return (maxSum / k);
}
int main() {
int arr[] = {2, 7, 6, 1, 4, 5, 5, 3};
int n = 8;
int k = 3;
int result = maxChocolates(arr, n, k);
printf("Maximum chocolates per student: %d<br>", result);
return 0;
}
Maximum chocolates per student: 5
Example with Different Input
Let's test with the example from the problem description −
#include <stdio.h>
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;
}
}
}
return (maxSum / k);
}
int main() {
int arr[] = {1, 2, 4, 5, 2, 8, 3, 5};
int n = 8;
int k = 3;
printf("Array: ");
for(int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\nStudents: %d<br>", k);
printf("Maximum chocolates per student: %d<br>", maxChocolates(arr, n, k));
return 0;
}
Array: 1 2 4 5 2 8 3 5 Students: 3 Maximum chocolates per student: 5
Key Points
- Time complexity: O(n²) where n is the number of boxes
- Space complexity: O(1) as we use only constant extra space
- The solution finds the subarray {5, 2, 8} with sum 15, giving 5 chocolates per student
- Only consecutive boxes can be selected for distribution
Conclusion
This approach efficiently finds the maximum number of chocolates that can be distributed equally among k students by checking all possible consecutive subarrays and selecting the one with maximum sum divisible by k.
