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 = 3Output:

Maximum = 5

Minimum = 2Explanation:

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 = 2Output:

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 1^{st}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

DifferenceK

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

Kcandies to the1^{st}boy.

Now, distribute remainingC – Kcandies to each boy line-wise starting form2nd boy toNth boy and then, again from 1^{st}toNth 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**.

- Initialize a variable, say
- 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.