Find the total number of ways to distribute N items among three people such that :
Each person gets at least one item.
Exactly one person among all the three people gets the maximum number of items.
The first and the only line of input will contain an integer N, denoting the total number of items.
0 <= N <= 10^9
Time Limit: 1sec
Output Format:
The only line of output prints the total number of ways to distribute the items.
5
3
We can distribute 5 items among three people in total 3 ways i.e. (1, 1, 3) (1, 3, 1) (3, 1, 1)
NOTE: (1, 2, 2) is not a valid distribution of items since two people have a maximum number of items. Also, (0, 2, 3) is not a valid distribution because 1st person is not having at least one item.
7
12
Explore all possible ways one by one iteratively by choosing all possible values for each of 3 persons.
As we need to divide N items into 3 parts satisfying the given conditions, each part can have values lying between [1, N - 2] as a minimum each should get at least 1, at max can get (N-2)(when the other two have minimum values i.e 1). So we will explore all possible ways to divide and satisfying the given conditions,
O(N*N), where N is the total number of candies.
Fixing two values by two nested loops(one inside another).
O(1)
Constant space required