Hackerrank Mark and Toys Solution

This is a solution of Mark and Toys under Greedy sub-domain in Hackerrank. For question you can refer Here

Output :

Code :

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

int main()
{
    int k,s=0;int n;
    cin>>n>>k;
    //cout<<n<<" "<<k;
    vector<int> a;
    for(int i=0;i<n;i++)
    {
        int x;
        cin>>x;
        a.push_back(x);
    }
    sort(a.begin(),a.end());
    int c=0;
    for(int i=0;i<n;i++)
    {

        if(s<=k)
        {

            s=s+a[i];
            c++;

        }
        else
            break;
    }
    cout<<c-1;
    return 0;
}

Comments

Popular posts from this blog

Non Restoring Division Algorithm Implementation in C

Hackerrank Modified Kaprekar Numbers Solution

Bit Stuffing Code Implementation in Java