Solution to the problem 'Electronics Shop' (HackerRank)

Hi all! Here's my solution to the problem 'Electronics Shop' from HackerRank.

#include <bits/stdc++.h>
#define int long long
#define pb push_back
#define fi first
#define se second
#define pii pair<int,int>
using namespace std;

int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int b,n,m;
    cin>>b>>n>>m;
    vector<int> keyboard,usb;
    for(int i=0;i<n;i++){
        int x;
        cin>>x;
        keyboard.pb(x);
    }
    for(int i=0;i<m;i++){
        int x;
        cin>>x;
        usb.pb(x);
    }
    sort(keyboard.begin(),keyboard.end());
    sort(usb.begin(),usb.end());
    int ans=0,flg=0;
    if(keyboard[0]+usb[0]>b) cout<<-1;
    else{
        for(int i=n-1;i>=0;i--){
            for(int j=m-1;j>=0;j--){
                if(keyboard[i]+usb[j]<=b){
                    ans=max(ans,(keyboard[i]+usb[j]));
                }
            }
        }
        cout<<ans;
    }
    return 0;
}

Comments

Popular posts from this blog

Solution of 'Tom Riddle's Diary' (Codeforces)

Solution to the problem 'Red Light, Green Light' from Codechef

Solution to the problem 'The Squid Game' form Codechef