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 n, int k, int a[]){sort(a, a + n);int cnt = 0;for (int i = 0; i < n; i++){k = k - a[i];if (k >= 0)cnt++;elsebreak;}return cnt;}int main(){int n = 4, k = 10;int a[n] = {5, 4, 2, 4};cout << maximumSamosa(n, k, a) << "\n";return 0;}
No comments:
Post a Comment