Given two arrays S1 and S2 containing N and M integers, and an integer K, the task is to find the maximum number of integers that can be removed from end of either given arrays, such that the sum of removed elements is less than or equal to K.
Example:
Input: S1[] = {1, 2, 3}, S2[] = {1, 2, 4}, K = 10
Output: 5
Explanation: All the elements of S1 i.e, {1, 2, 3} and two elements of S2 i.e, {1, 2} can be removed from the given arrays as their sum is 9 which is less than 10. Hence, the number of integers that can be removes is 5 which is the maximum possible.
Input: S1[] = {12, 21, 102}, S2[] = {167, 244, 377, 56, 235, 269, 23}, K = 1000
Output: 7
Approach: The above approach can be solved with the help of a greedy approach. The idea is to consider the given arrays as stacks (as only the endmost element can be removed from any array). Check over all possible valid sequences of operations using a two-pointer approach. Below are the steps to follow:
- First, check how many numbers of S1 can be obtained considering S2 does not exist.
- Now, the current answer obtained in step 1 is stored as the final answer.
- Now start traversing S2 and keep inserting the elements in the set of removed integers. If the sum of removed elements exceeds K at any point, remove the integers of S1 that are included from the last until the sum is less than or equal to K.
- Update the final answer according to the current answer in each step which is actually giving all possible valid sequences of operations.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int Max_profit_maximisation( int Stack1[], int Stack2[],
int n, int m, int K)
{
int maximum_result = 0;
int index_for_Stack1 = 0;
int index_for_Stack2;
int curr_sum = 0;
while (index_for_Stack1 < n
&& curr_sum + Stack1[index_for_Stack1]
< K) {
curr_sum += Stack1[index_for_Stack1];
index_for_Stack1++;
}
maximum_result = index_for_Stack1;
while (index_for_Stack2 < m) {
curr_sum += Stack2[index_for_Stack2];
index_for_Stack2++;
while (curr_sum > K
&& index_for_Stack1 > 0) {
index_for_Stack1--;
curr_sum -= Stack1[index_for_Stack1];
}
maximum_result
= max(maximum_result,
index_for_Stack1
+ index_for_Stack2);
}
return maximum_result;
}
int main()
{
int S1[] = { 12, 21, 102 };
int S2[] = { 167, 244, 377, 56,
235, 269, 23 };
int N = 3;
int M = 7;
int K = 1000;
cout << Max_profit_maximisation(S1, S2, N, M, K);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int Max_profit_maximisation( int Stack1[], int Stack2[],
int n, int m, int K)
{
int maximum_result = 0 ;
int index_for_Stack1 = 0 ;
int index_for_Stack2=Integer.MAX_VALUE;
int curr_sum = 0 ;
while (index_for_Stack1 < n
&& curr_sum + Stack1[index_for_Stack1]
< K) {
curr_sum += Stack1[index_for_Stack1];
index_for_Stack1++;
}
maximum_result = index_for_Stack1;
while (index_for_Stack2 < m) {
curr_sum += Stack2[index_for_Stack2];
index_for_Stack2++;
while (curr_sum > K
&& index_for_Stack1 > 0 ) {
index_for_Stack1--;
curr_sum -= Stack1[index_for_Stack1];
}
maximum_result
= Math.max(maximum_result,
index_for_Stack1
+ index_for_Stack2);
}
return maximum_result;
}
public static void main(String[] args)
{
int S1[] = { 12 , 21 , 102 };
int S2[] = { 167 , 244 , 377 , 56 ,
235 , 269 , 23 };
int N = 3 ;
int M = 7 ;
int K = 1000 ;
System.out.print(Max_profit_maximisation(S1, S2, N, M, K));
}
}
|
Python
def Max_profit_maximisation(Stack1, Stack2, n, m, K):
maximum_result = 0
index_for_Stack1 = 0
index_for_Stack2 = 100 * * 100
curr_sum = 0
while (index_for_Stack1 < n
and curr_sum + Stack1[index_for_Stack1]
< K):
curr_sum + = Stack1[index_for_Stack1]
index_for_Stack1 + = 1
maximum_result = index_for_Stack1
while (index_for_Stack2 < m) :
curr_sum + = Stack2[index_for_Stack2]
index_for_Stack2 + = 1
while (curr_sum > K
and index_for_Stack1 > 0 ):
index_for_Stack1 - = 1
curr_sum - = Stack1[index_for_Stack1]
maximum_result = max (maximum_result,
index_for_Stack1
+ index_for_Stack2)
return maximum_result
if __name__ = = '__main__' :
S1 = [ 12 , 21 , 102 ];
S2 = [ 167 , 244 , 377 , 56 ,
235 , 269 , 23 ];
N = 3 ;
M = 7 ;
K = 1000 ;
print (Max_profit_maximisation(S1, S2, N, M, K))
|
C#
using System;
class GFG{
static int Max_profit_maximisation( int []Stack1, int []Stack2,
int n, int m, int K)
{
int maximum_result = 0;
int index_for_Stack1 =0;
int index_for_Stack2= int .MaxValue;
int curr_sum = 0;
while (index_for_Stack1 < n &&
curr_sum + Stack1[index_for_Stack1] < K)
{
curr_sum += Stack1[index_for_Stack1];
index_for_Stack1++;
}
maximum_result = index_for_Stack1;
while (index_for_Stack2 < m)
{
curr_sum += Stack2[index_for_Stack2];
index_for_Stack2++;
while (curr_sum > K && index_for_Stack1 > 0)
{
index_for_Stack1--;
curr_sum -= Stack1[index_for_Stack1];
}
maximum_result = Math.Max(maximum_result,
index_for_Stack1 +
index_for_Stack2);
}
return maximum_result;
}
public static void Main(String[] args)
{
int []S1 = { 12, 21, 102 };
int []S2 = { 167, 244, 377, 56,
235, 269, 23 };
int N = 3;
int M = 7;
int K = 1000;
Console.Write(Max_profit_maximisation(S1, S2, N, M, K));
}
}
|
Javascript
<script>
function Max_profit_maximisation(Stack1, Stack2,
n, m, K) {
let maximum_result = 0;
let index_for_Stack1 = 0;
let index_for_Stack2;
let curr_sum = 0;
while (index_for_Stack1 < n
&& curr_sum + Stack1[index_for_Stack1]
< K) {
curr_sum += Stack1[index_for_Stack1];
index_for_Stack1++;
}
maximum_result = index_for_Stack1;
while (index_for_Stack2 < m) {
curr_sum += Stack2[index_for_Stack2];
index_for_Stack2++;
while (curr_sum > K
&& index_for_Stack1 > 0) {
index_for_Stack1--;
curr_sum -= Stack1[index_for_Stack1];
}
maximum_result
= Math.max(maximum_result,
index_for_Stack1
+ index_for_Stack2);
}
return maximum_result;
}
let S1 = [12, 21, 102];
let S2 = [167, 244, 377, 56,
235, 269, 23];
let N = 3;
let M = 7;
let K = 1000;
document.write(Max_profit_maximisation(S1, S2, N, M, K));
</script>
|
Time Complexity: O(N+M)
Auxiliary Space: O(1)