Donor Vicky

Vicky being a noble person, decided to donate X amount of money to the poor. He has many coins of value from 1 to infinity. He decided to donate money to N poor people numbered 1 to N  and give one coin to each poor in increasing order of the value of the coin. after giving the coin to the Nth poor he starts the same process from 1st poor person till he donates all his money. 

If he is left with some amount whose coin he had already used he give a cheque of leftover money to the next poor in the sequence.
Help Vicky in finding the person who got the last coin or cheque.

Example:

Input:  n = 5, x = 4
Output: 3

Approach

C++

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

int donorVicky(int nint x)
{
    int i = 1;
    int ans;
    int j = 1;
    while (x > 0)
    {
        x = x - j;
        if (x <= 0)
        {
            ans = i;
            break;
        }
        if (i == n)
            i = 1;
        else
            i++;
        j++;
    }
    return ans;
}
int main()
{
    int n = 5x = 4;

    cout << donorVicky(nx<< "\n";

    return 0;
}


No comments:

Post a Comment