Maximum elements that can be removed from front of two arrays such that their sum is at most K
Last Updated :
11 Oct, 2023
Given an integer K and two arrays A[] and B[] consisting of N and M integers, the task is to maximize the number of elements that can be removed from the front of either array according to the following rules:
- Remove an element from the front of either array A[] and B[] such that the value of the removed element must be at most K.
- Decrease the value of K by the value of the element removed.
Examples:
Input: K = 7, A[] = {2, 4, 7, 3}, B[] = {1, 9, 3, 4, 5}
Output: 3
Explanation:
Operation 1: Choose element from the array A[] and decrease K by A[0](=2), then value of K becomes = (7 – 2) = 5.
Operation 2: Choose element from the array B[] and decrease K by B[0](=1), then value of K becomes = (5 – 1) = 4.
Operation 3: Choose element from the array A[] and decrease K by A[1](=4), then value of K becomes = (4 – 4) = 4.
After the above operations, no more element can be removed. Therefore, print 3.
Input: K = 9, A[] = {12, 10, 1, 2, 3}, B[] = {15, 19, 3, 4, 5}
Output: 0
Approach: The given problem can be solved by using the Prefix Sum and Binary Search to find the total items possible j to take from stack B after taking i items from stack A and return the result as the maximum value of (i + j). Follow the below steps to solve the given problem:
- Find prefix sum of the arrays A[] and B[].
- Initialize a variable, say count as 0, that stores the maximum items that can be taken.
- Traverse the array, A[] over the range [0, N] using the variable i and perform the following steps:
- If the value of A[i] is greater than K, then break out of the loop.
- Store the remaining amount after taking i items from stack A in a variable, rem as K – A[i].
- Perform a binary search on the array B, to find the maximum items say, j that can be taken in rem amount from stack B (after taking i elements from stack A).
- Store the maximum value of i + j in the variable count.
- After completing the above steps, print the value of count as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void maxItems( int n, int m, int a[],
int b[], int K)
{
int count = 0;
int A[n + 1];
int B[m + 1];
A[0] = 0;
B[0] = 0;
for ( int i = 1; i <= n; i++) {
A[i] = a[i - 1] + A[i - 1];
}
for ( int i = 1; i <= m; i++) {
B[i] = b[i - 1] + B[i - 1];
}
for ( int i = 0; i <= n; i++) {
if (A[i] > K)
break ;
int rem = K - A[i];
int j = 0;
int lo = 0, hi = m;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (B[mid] <= rem) {
j = mid;
lo = mid + 1;
}
else {
hi = mid - 1;
}
}
count = max(j + i, count);
}
cout << count;
}
int main()
{
int n = 4, m = 5, K = 7;
int A[n] = { 2, 4, 7, 3 };
int B[m] = { 1, 9, 3, 4, 5 };
maxItems(n, m, A, B, K);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
static void maxItems( int n, int m, int a[],
int b[], int K)
{
int count = 0 ;
int A[] = new int [n + 1 ];
int B[] = new int [m + 1 ];
A[ 0 ] = 0 ;
B[ 0 ] = 0 ;
for ( int i = 1 ; i <= n; i++)
{
A[i] = a[i - 1 ] + A[i - 1 ];
}
for ( int i = 1 ; i <= m; i++)
{
B[i] = b[i - 1 ] + B[i - 1 ];
}
for ( int i = 0 ; i <= n; i++)
{
if (A[i] > K)
break ;
int rem = K - A[i];
int j = 0 ;
int lo = 0 , hi = m;
while (lo <= hi)
{
int mid = (lo + hi) / 2 ;
if (B[mid] <= rem)
{
j = mid;
lo = mid + 1 ;
}
else
{
hi = mid - 1 ;
}
}
count = Math.max(j + i, count);
}
System.out.print(count);
}
public static void main (String[] args)
{
int n = 4 , m = 5 , K = 7 ;
int A[] = { 2 , 4 , 7 , 3 };
int B[] = { 1 , 9 , 3 , 4 , 5 };
maxItems(n, m, A, B, K);
}
}
|
Python3
def maxItems(n, m, a, b, K):
count = 0
A = [ 0 for i in range (n + 1 )]
B = [ 0 for i in range (m + 1 )]
for i in range ( 1 , n + 1 , 1 ):
A[i] = a[i - 1 ] + A[i - 1 ]
for i in range ( 1 , m + 1 , 1 ):
B[i] = b[i - 1 ] + B[i - 1 ]
for i in range (n + 1 ):
if (A[i] > K):
break
rem = K - A[i]
j = 0
lo = 0
hi = m
while (lo < = hi):
mid = (lo + hi) / / 2
if (B[mid] < = rem):
j = mid
lo = mid + 1
else :
hi = mid - 1
count = max (j + i, count)
print (count)
if __name__ = = '__main__' :
n = 4
m = 5
K = 7
A = [ 2 , 4 , 7 , 3 ]
B = [ 1 , 9 , 3 , 4 , 5 ]
maxItems(n, m, A, B, K)
|
C#
using System;
class GFG
{
static void maxItems( int n, int m, int [] a,
int [] b, int K)
{
int count = 0;
int [] A = new int [n + 1];
int [] B= new int [m + 1];
A[0] = 0;
B[0] = 0;
for ( int i = 1; i <= n; i++)
{
A[i] = a[i - 1] + A[i - 1];
}
for ( int i = 1; i <= m; i++)
{
B[i] = b[i - 1] + B[i - 1];
}
for ( int i = 0; i <= n; i++)
{
if (A[i] > K)
break ;
int rem = K - A[i];
int j = 0;
int lo = 0, hi = m;
while (lo <= hi)
{
int mid = (lo + hi) / 2;
if (B[mid] <= rem)
{
j = mid;
lo = mid + 1;
}
else
{
hi = mid - 1;
}
}
count = Math.Max(j + i, count);
}
Console.Write(count);
}
public static void Main(String []args)
{
int n = 4, m = 5, K = 7;
int [] A = { 2, 4, 7, 3 };
int [] B = { 1, 9, 3, 4, 5 };
maxItems(n, m, A, B, K);
}
}
|
Javascript
<script>
function maxItems(n, m, a, b, K)
{
var count = 0;
var A = new Array(n + 1);
var B = new Array(m + 1);
A[0] = 0;
B[0] = 0;
var i;
for (i = 1; i <= n; i++) {
A[i] = a[i - 1] + A[i - 1];
}
for (i = 1; i <= m; i++) {
B[i] = b[i - 1] + B[i - 1];
}
for (i = 0; i <= n; i++) {
if (A[i] > K)
break ;
var rem = K - A[i];
var j = 0;
var lo = 0, hi = m;
while (lo <= hi) {
var mid = parseInt((lo + hi) / 2);
if (B[mid] <= rem) {
j = mid;
lo = mid + 1;
}
else {
hi = mid - 1;
}
}
count = Math.max(j + i, count);
}
document.write(count);
}
var n = 4, m = 5, K = 7;
var A = [2, 4, 7, 3];
var B = [1, 9, 3, 4, 5];
maxItems(n, m, A, B, K);
</script>
|
Time Complexity: O(N * log(M))
Auxiliary Space: O(N + M)
Approach(Space Optimization): This approach sorts both arrays in ascending order and then uses two pointers to traverse the arrays from both ends. The idea is to pair the smallest element of array A with the largest element of array B that has not yet been paired.
Step-by-step algorithm:
- Sort both arrays a[] and b[] in non-decreasing order.
- Initialize two variables i and j to 0 and m-1 respectively.
- Initialize a count variable to 0.
- Repeat the following until either i >= n or j < 0:
a. If a[i]+b[j] <= K, then increment the count variable and increment i and decrement j.
b. Else if a[i]+b[j] > K and j > 0, then decrement j.
c. Else, increment i.
- Print the value of the count variable.
C++
#include <bits/stdc++.h>
using namespace std;
void maxItems( int n, int m, int a[], int b[], int K) {
sort(a, a + n);
sort(b, b + m);
int i = 0, j = m - 1, count = 0;
while (i < n && j >= 0) {
if (a[i] + b[j] <= K) {
count++;
i++;
j--;
} else if (a[i] + b[j] > K && j > 0) {
j--;
} else {
i++;
}
}
cout << count << endl;
}
int main() {
int n = 4, m = 5, K = 7;
int A[n] = {2, 4, 7, 3};
int B[m] = {1, 9, 3, 4, 5};
maxItems(n, m, A, B, K);
return 0;
}
|
Java
import java.util.Arrays;
public class MaxItems {
public static void maxItems( int n, int m, int [] a, int [] b, int K) {
Arrays.sort(a);
Arrays.sort(b);
int i = 0 , j = m - 1 , count = 0 ;
while (i < n && j >= 0 ) {
if (a[i] + b[j] <= K) {
count++;
i++;
j--;
} else if (a[i] + b[j] > K && j > 0 ) {
j--;
} else {
i++;
}
}
System.out.println(count);
}
public static void main(String[] args) {
int n = 4 , m = 5 , K = 7 ;
int [] A = { 2 , 4 , 7 , 3 };
int [] B = { 1 , 9 , 3 , 4 , 5 };
maxItems(n, m, A, B, K);
}
}
|
Python3
def max_items(n, m, a, b, K):
a.sort()
b.sort()
i = 0
j = m - 1
count = 0
while i < n and j > = 0 :
if a[i] + b[j] < = K:
count + = 1
i + = 1
j - = 1
elif a[i] + b[j] > K and j > 0 :
j - = 1
else :
i + = 1
print (count)
if __name__ = = "__main__" :
n = 4
m = 5
K = 7
A = [ 2 , 4 , 7 , 3 ]
B = [ 1 , 9 , 3 , 4 , 5 ]
max_items(n, m, A, B, K)
|
C#
using System;
public class MaxItems
{
public static void FindMaxItems( int n, int m, int [] a, int [] b, int K)
{
Array.Sort(a);
Array.Sort(b);
int i = 0, j = m - 1, count = 0;
while (i < n && j >= 0)
{
if (a[i] + b[j] <= K)
{
count++;
i++;
j--;
}
else if (a[i] + b[j] > K && j > 0)
{
j--;
}
else
{
i++;
}
}
Console.WriteLine(count);
}
public static void Main( string [] args)
{
int n = 4, m = 5, K = 7;
int [] A = { 2, 4, 7, 3 };
int [] B = { 1, 9, 3, 4, 5 };
FindMaxItems(n, m, A, B, K);
}
}
|
Javascript
function maxItems(n, m, a, b, K) {
a.sort((x, y) => x - y);
b.sort((x, y) => x - y);
let i = 0;
let j = m - 1;
let count = 0;
while (i < n && j >= 0) {
if (a[i] + b[j] <= K) {
count++;
i++;
j--;
} else if (a[i] + b[j] > K && j > 0) {
j--;
} else {
i++;
}
}
console.log(count);
}
const n = 4;
const m = 5;
const K = 7;
const A = [2, 4, 7, 3];
const B = [1, 9, 3, 4, 5];
maxItems(n, m, A, B, K);
|
Time Complexity: O(N log N + M log M)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...