Count of ways to distribute N items among 3 people with one person receiving maximum
Given an integer N, the task is to find the total number of ways to distribute N among 3 people such that:
- Exactly one person gets the maximum number of items among all the 3 people.
- Each person gets at least 1 item.
Examples:
Input: N = 5
Output: 3
Explanation:
3 ways to distribute the item among 3 people are {1, 1, 3}, {1, 3, 1} and {3, 1, 1}.
Distributions like {1, 2, 2} or {2, 1, 2} are not valid as two persons are getting the maximum.
Input: N = 10
Output: 33
Explanation:
For the Input N = 10 there are 33 ways of distribution.
Approach:
To solve the problem mentioned above, we have to observe that if N < 4, then such a distribution is not possible.
For all values of N ? 4, follow the steps to solve the problem:
- Total no of ways to distribute N items among 3 people is given by (N – 1) * (N – 2) / 2.
- Initialize a variable s = 0 which stores the count of ways the distribution is not possible.
- Iterate two nested loops, where i ranges between [2, N – 3] and j ranging upto i:
- For each iteration, check if N = 2 * i + j, that is 2 persons can receive the maximum number of elements
- If so, then increment s by 1. If N is divisible by 3, update s by 3 * s + 1. Otherwise, update to 3 * s.
- Finally, return ans – s as the total number of ways to distribute N items among three people.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countWays( int N)
{
if (N < 4)
return 0;
int ans = ((N - 1) * (N
- 2))
/ 2;
int s = 0;
for ( int i = 2; i <= N - 3;
i++) {
for ( int j = 1; j < i;
j++) {
if (N == 2 * i + j)
s++;
}
}
if (N % 3 == 0)
s = 3 * s + 1;
else
s = 3 * s;
return ans - s;
}
int main()
{
int N = 10;
cout << countWays(N);
return 0;
}
|
Java
class GFG{
static int countWays( int N)
{
if (N < 4 )
return 0 ;
int ans = ((N - 1 ) * (N - 2 )) / 2 ;
int s = 0 ;
for ( int i = 2 ; i <= N - 3 ; i++)
{
for ( int j = 1 ; j < i; j++)
{
if (N == 2 * i + j)
s++;
}
}
if (N % 3 == 0 )
s = 3 * s + 1 ;
else
s = 3 * s;
return ans - s;
}
public static void main(String[] args)
{
int N = 10 ;
System.out.println(countWays(N));
}
}
|
Python3
def countWays(N):
if (N < 4 ):
return 0
ans = ((N - 1 ) * (N - 2 )) / / 2
s = 0
for i in range ( 2 , N - 2 , 1 ):
for j in range ( 1 , i, 1 ):
if (N = = 2 * i + j):
s + = 1
if (N % 3 = = 0 ):
s = 3 * s + 1
else :
s = 3 * s
return ans - s
N = 10
print (countWays(N))
|
C#
using System;
class GFG{
static int countWays( int N)
{
if (N < 4)
return 0;
int ans = ((N - 1) * (N - 2)) / 2;
int s = 0;
for ( int i = 2; i <= N - 3; i++)
{
for ( int j = 1; j < i; j++)
{
if (N == 2 * i + j)
s++;
}
}
if (N % 3 == 0)
s = 3 * s + 1;
else
s = 3 * s;
return ans - s;
}
public static void Main()
{
int N = 10;
Console.Write(countWays(N));
}
}
|
Javascript
<script>
function countWays(N)
{
if (N < 4)
return 0;
let ans = ((N - 1) * (N - 2)) / 2;
let s = 0;
for (let i = 2; i <= N - 3; i++) {
for (let j = 1; j < i; j++) {
if (N == 2 * i + j)
s++;
}
}
if (N % 3 == 0)
s = 3 * s + 1;
else
s = 3 * s;
return ans - s;
}
let N = 10;
document.write(countWays(N));
</script>
|
Time complexity: O(N2)
Auxiliary Space: O(1)
Last Updated :
29 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...