In search of Samosa

Shiro is an avid lover of Samosas. He went down to Samosa street to have some. But he only has K units of money with him. There are N shops on the street and unfortunately, all of them have only one samosa remaining. You are also given an array A[ ] , where Ai is the cost of a samosa on the i'th shop.

Find the maximum samosas that Shiro can eat.

Example:

Input:  n = 4, k = 10, a = [5,4,2,4]
Output: 3

Approach

C++

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

int maximumSamosa(int nint kint a[])
{
    sort(aa + n);
    int cnt = 0;
    for (int i = 0i < ni++)
    {
        k = k - a[i];
        if (k >= 0)
            cnt++;
        else
            break;
    }
    return cnt;
}
int main()
{
    int n = 4k = 10;

    int a[n] = {5424};

    cout << maximumSamosa(nka<< "\n";

    return 0;
}


No comments:

Post a Comment