Assign Tasks Geeks For Geeks
Example 1
Input:
N = 3
A = [6, 3, 6]
B = [2, 6, 8]
Output: 2
Explanation: Assign 1st task to 2nd person,
2nd task to 1st person and 3rd task to 3rd person.
Days taken by 1st, 2nd and 3rd person will be:
2, 1, 1.
Example 2
Input:
N = 2
A = [100, 100]
B = [1, 200]
Output: 100
Explanation: Since the work required by both
tasks is same, we can assign them in any order
// { Driver Code Starts
// Initial Template for C++
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
// User function Template for C++
class Solution
{
public:
long long solve(int n, long long A[], long long B[])
{
int C[n];
sort(A, A + n);
sort(B, B + n);
int flag = 1;
int max;
int count = 0;
while (flag > 0)
{
max = 0;
for (int i = 0; i < n; i++)
{
if (A[i] - B[i] < 0)
{
A[i] = 0;
}
else
{
A[i] = A[i] - B[i];
// count++;
}
if (A[i] > max)
{
max = A[i];
// cout<<max<<" ";
}
}
// cout<<max<<" ";
if (max <= 0)
{
flag = 0;
}
count++;
}
return count;
}
};
// { Driver Code Starts.
int main()
{
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
long long arr[n], brr[n];
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
for (int i = 0; i < n; i++)
{
cin >> brr[i];
}
Solution ob;
long long ans = ob.solve(n, arr, brr);
cout << ans << "\n";
}
return 0;
}
// } Driver Code Ends
Happy Coding 😊.