Gift Shop and Coupon Solution || Codechef Starters - 30

             

Gift Shop and Coupon Solution 

Codechef Starters - 30
Problem Code : GFTSHP

Solution :

#include <bits/stdc++.h>
#define ll long long int
#define ull unsigned long long int
using namespace std;

void solve()
{
    ll n, k;
    cin >> n >> k;
    ll a[n];
    for (ll i = 0; i < n; i++)
    {
        cin >> a[i];
    }
    sort(a, a + n);
    ll ans = 0, i = 0, temp = 0;
    while (temp <= k && i < n)
    {
        if (a[i] + temp <= k)
        {
            ans++;
            temp += a[i];
            i++;
        }
        else if (temp + ((a[i] + 1) / 2) <= k)
        {
            temp += (a[i] + 1) / 2;
            ans++;
            i++;
            break;
        }
        else
        {
            break;
        }
    }
    cout << ans << "\n";
}

int main()
{
    ll t;
    cin >> t;
    while (t--)
    {
        solve();
    }
}

Comments