In this HackerEarth A plane journey problem solution A flight company has to schedule a journey of N groups of people from the same source to the same destination. Here, A1, A2, ..., AN represents the number of people in each group. All groups are present at the source. The flight company has M planes where B1, B2, ..., Bm represent the capacity of each plane.

You are required to send all groups to destination with the following conditions:
  1. Each plane can travel from the Source to Destination with only one group at a time such that capacity of a plane is enough to accommodate all people in that group.
  2. All people belonging to the same group travel together.
  3. Every plane can make multiple journeys between source and destination.
  4. It costs 1 unit of time to travel between source to destination and vice versa.


HackerEarth A plane journey problem solution


HackerEarth A plane journey problem solution.

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

int main(){
    int n,m;
    cin>>n>>m;

    ll a[n];
    for(int i = 0 ; i < n ; i++) cin>>a[i];

    ll b[m];
    for(int i = 0 ; i < m ; i++) cin>>b[i];

    sort(a,a+n);
    sort(b,b+m);

    ll s = 1;
    ll e = 1000000000000000000;

    ll ans = -1;
    while(s <= e)
    {
        ll mm = (s+e)/2;
        ll available = mm / 2;

        if(mm&1)
            available++;

        ll left = available;
        int j = n - 1;

        int flag = 0;
        for(int i = m - 1 ; i >= 0 ; i--)
        {
            if(j == -1) break;
            left = available;
            while(j >= 0 && left > 0)
            {
                if(b[i] >= a[j])
                {
                    j--;
                    left--;
                }
                else
                {
                    flag = 1;
                    break;
                }
            }
        }   

        if(j == -1 && flag == 0)
        {
            ans = mm;
            e = mm - 1;
        }
        else
        {
            s = mm + 1;
        }
    }

    cout<<ans<<endl;
}

Second solution

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 14;
int n, m, a[maxn], b[maxn];
bool check(int mid){
    int ptr = 0, cnt = 0;
    for(int i = 0; i < n; i++){
        while(ptr < m && (cnt == mid || b[ptr] < a[i]))
            ptr++, cnt = 0;
        if(ptr == m)
            return 0;
        cnt++;
    }
    return 1;
}
int main(){
    ios::sync_with_stdio(0), cin.tie(0);
    cin >> n >> m;
    for(int i = 0; i < n; i++)
        cin >> a[i];
    for(int i = 0; i < m; i++)
        cin >> b[i];
    sort(a, a + n);
    sort(b, b + m);
    if(b[m - 1] < a[n - 1])
        return cout << "-1\n", 0;
    int lo = 0, hi = n;
    while(hi - lo > 1){
        int mid = (lo + hi) / 2;
        (check(mid) ? hi : lo) = mid;
    }
    cout << hi * 2 - 1 << '\n';
}