Skip to content
Related Articles

Related Articles

Distributed C candies among N boys such that difference between maximum and minimum candies received is K
  • Last Updated : 08 Apr, 2021
GeeksforGeeks - Summer Carnival Banner

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:

  1. 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.
  2. 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:

  1. Initialize two variables, say maximum and minimum, to store the count of maximum and the minimum number of candies a boy can possess.
  2. If N = 1: Set maximum = C and minimum = C
  3. If K >= C: Set maximum = C and minimum = 0
  4. Otherwise, if K < C, set maximum = K and  minimum = 0. Now, follow the steps below:
    1. Initialize a variable, say remain_candy, and set it to C – K.
    2. Add (remain_candy / N ) to maximum and minimum.
    3. If (remain_candy % N == N-1), increment minimum.
  5. 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;
}
Output:
Maximum = 5
Minimum = 2

Time Complexity: O(1)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :