Showing posts with label greedy. Show all posts
Showing posts with label greedy. Show all posts

Monday, 11 November 2019

Mark and Toys,Priyanka and Toys, Jim and the Orders Hacker Rank Solution in C++

Mark and Toys
#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n,k;
    scanf("%d%d",&n,&k);
    int a[n],i,c=0,bill=0;
    for(i=0;i<n;i++)
        scanf("%d",&a[i]);
    sort(a,a+n);
    for(i=0;i<n;i++)
    {
        bill=bill+a[i];
        if(bill>k)
            break;
        else
            c++;
    }
    printf("%d",c);
}

Priyanka and Toys
#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    scanf("%d",&n);
    int a[n],i;
    for(i=0;i<n;i++)
        scanf("%d",&a[i]);
    sort(a,a+n);
    int minwt=a[0];
    int noc=1;
    for(i=0;i<n;i++)
    {
        if(a[i]<=4+minwt)
            continue;
        noc++;
        minwt=a[i];
    }
    printf("%d",noc);
    return 0;
}




Jim and the Orders
#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    scanf("%d",&n);
    int i;
    vector<pair<int,int>> v;
    int ono, ptime;
    for(i=0;i<n;i++)
    {
        scanf("%d%d",&ono,&ptime);
        v.push_back(make_pair(ono+ptime,i+1));
    }
    sort(v.begin(),v.end());
    for(i=0;i<n;i++)
        printf("%d ",v[i].second);

}