Given two integers N and C, representing the number of boys and candies, and an integer K, the task is to calculate the maximum and the minimum number of candies received by any boy such that the difference between them is K.
Examples:
Input: N = 4, C = 12, K = 3
Output:
Maximum = 5
Minimum = 2
Explanation:
Distribute the {2, 2, 3, 5} candies among N (= 4) boys.
Therefore, the difference between the maximum and minimum is 5 – 2 = 3 (= K).Input: N = 2, C = 8, K = 2
Output:
Maximum = 5
Minimum = 3.
Approach: The given problem can be solved based on the following observations:
- If K >= C : All the C candies can be distributed to 1st boy. Therefore, the maximum count of candies will be C and the minimum will be 0.
- Otherwise, distribute the candies to boys maintaining the maximum and minimum counts to ? K.
Illustration: Refer to the table below to observe the distribution pattern of candies.
Boy A Boy B Boy C Difference K 0 0 K K 1 1 K-1 K+1 1 1 K K+1 2 2 K-1 K+2 2 2 K Initially, distribute K candies to the 1st boy.
Now, distribute remaining C – K candies to each boy line-wise starting form 2nd boy to Nth boy and then, again from 1st to Nth and so on.
Follow the steps below to solve the problem:
- Initialize two variables, say maximum and minimum, to store the count of maximum and the minimum number of candies a boy can possess.
- If N = 1: Set maximum = C and minimum = C
- If K >= C: Set maximum = C and minimum = 0
- Otherwise, if K < C, set maximum = K and minimum = 0. Now, follow the steps below:
- Initialize a variable, say remain_candy, and set it to C – K.
- Add (remain_candy / N ) to maximum and minimum.
- If (remain_candy % N == N-1), increment minimum.
- Print minimum and maximum value.
Below is the implementation of the above approach:
C++
// C++ program for // the above approach #include <bits/stdc++.h> using namespace std; // Function to calculate the // maximum and minimum number // of candies a boy can possess int max_min( int N, int C, int K) { int maximum, minimum; if (N == 1) { // All candies will be // given to one boy maximum = minimum = C; } else if (K >= C) { // All the candies will // be given to 1 boy maximum = C; minimum = 0; } else { // Give K candies to 1st // boy initially maximum = K; minimum = 0; // Count remaining candies int remain_candy = C - K; maximum += remain_candy / N; minimum = remain_candy / N; // If the last candy of remaining candies // is given to the last boy, i.e Nth boy if (remain_candy % N == N - 1) { // Increase minimum count minimum++; } } cout << "Maximum = " << maximum << endl; cout << "Minimum = " << minimum; return 0; } // Driver Code int main() { int N = 4; int C = 12; int K = 3; max_min(N, C, K); return 0; } |
Java
// Java program for the above approach class GFG{ // Function to calculate the // maximum and minimum number // of candies a boy can possess static void max_min( int N, int C, int K) { int maximum, minimum; if (N == 1 ) { // All candies will be // given to one boy maximum = minimum = C; } else if (K >= C) { // All the candies will // be given to 1 boy maximum = C; minimum = 0 ; } else { // Give K candies to 1st // boy initially maximum = K; minimum = 0 ; // Count remaining candies int remain_candy = C - K; maximum += remain_candy / N; minimum = remain_candy / N; // If the last candy of remaining candies // is given to the last boy, i.e Nth boy if (remain_candy % N == N - 1 ) { // Increase minimum count minimum++; } } System.out.println( "Maximum = " + maximum); System.out.println( "Minimum = " + minimum); } // Driver code public static void main(String[] args) { int N = 4 ; int C = 12 ; int K = 3 ; max_min(N, C, K); } } // This code is contributed by abhinavjain194 |
Python3
# Python3 program for the above approach # Function to calculate the # maximum and minimum number # of candies a boy can possess def max_min(N, C, K): maximum = 0 minimum = 0 if (N = = 1 ): # All candies will be # given to one boy maximum = minimum = C elif (K > = C): # All the candies will # be given to 1 boy maximum = C minimum = 0 else : # Give K candies to 1st # boy initially maximum = K minimum = 0 # Count remaining candies remain_candy = C - K maximum + = remain_candy / / N minimum = remain_candy / / N # If the last candy of remaining candies # is given to the last boy, i.e Nth boy if (remain_candy % N = = N - 1 ): # Increase minimum count minimum + = 1 print ( "Maximum = {}" . format (maximum)) print ( "Minimum = {}" . format (minimum)) # Driver code N = 4 C = 12 K = 3 max_min(N, C, K) # This code is contributed by abhinavjain194 |
C#
// C# program for the above approach using System; class GFG{ // Function to calculate the // maximum and minimum number // of candies a boy can possess static void max_min( int N, int C, int K) { int maximum, minimum; if (N == 1) { // All candies will be // given to one boy maximum = minimum = C; } else if (K >= C) { // All the candies will // be given to 1 boy maximum = C; minimum = 0; } else { // Give K candies to 1st // boy initially maximum = K; minimum = 0; // Count remaining candies int remain_candy = C - K; maximum += remain_candy / N; minimum = remain_candy / N; // If the last candy of remaining candies // is given to the last boy, i.e Nth boy if (remain_candy % N == N - 1) { // Increase minimum count minimum++; } } Console.WriteLine( "Maximum = " + maximum); Console.WriteLine( "Minimum = " + minimum); } // Driver code static void Main() { int N = 4; int C = 12; int K = 3; max_min(N, C, K); } } // This code is contributed by abhinavjain194 |
Javascript
<script> // JavaScript program to implement // the above approach // Function to calculate the // maximum and minimum number // of candies a boy can possess function max_min(N, C, K) { let maximum, minimum; if (N == 1) { // All candies will be // given to one boy maximum = minimum = C; } else if (K >= C) { // All the candies will // be given to 1 boy maximum = C; minimum = 0; } else { // Give K candies to 1st // boy initially maximum = K; minimum = 0; // Count remaining candies let remain_candy = C - K; maximum += Math.floor(remain_candy / N); minimum = Math.floor(remain_candy / N); // If the last candy of remaining candies // is given to the last boy, i.e Nth boy if (remain_candy % N == N - 1) { // Increase minimum count minimum++; } } document.write( "Maximum = " + maximum + "<br/>" ); document.write( "Minimum = " + minimum); } // Driver code let N = 4; let C = 12; let K = 3; max_min(N, C, K); </script> |
Maximum = 5 Minimum = 2
Time Complexity: O(1)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!