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 : 05 May, 2021

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;
}

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 contibuted 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>
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 mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :