Distribution

Prashant's father came to hostel to meet his roommate with N number of gifts.

In his room, they are a total of 3 members.

Ankit, Manish and Prashant

Manish is elder than both so he will take more gifts than Ankit and Prashant, And Ankit will get more than Prashant now your work is to find the ways of distribution of gifts.

Example:

Input: n = 10
Output: 8

Approach

C++

#include <bits/stdc++.h>
using namespace std;

int distribution(int n)
{
    int ans = 0;
    for (int i = n - 1i > n / 2i--)
    {
        ans += (n - i + 1) / 2;
    }
    for (int i = n / 2i > n / 3i--)
    {
        for (int j = i - 1j > i / 2j--)
        {
            int k = n - i - j;
            int cnt = 0;
            while (k < j)
            {
                cnt++;
                k++;
                j--;
            }
            ans += cnt;
        }
    }
    return ans;
}
int main()
{
    int n = 10;

    cout << distribution(n<< "\n";

    return 0;
}


No comments:

Post a Comment