Maximize sum by shifting from one Array to other at most once
Last Updated :
01 Jun, 2023
Given two arrays X[] and Y[] of length N each. You can shift from X[] to Y[] at most once. The task is to output the maximum sum possible sum.
Examples:
Input: N = 4, X[] = {7, 5, 3, 4}, Y[] = {2, 3, 1, 3}
Output: 19
Explanation: It is optimal to not shift from X[] to Y[]. The total maximum possible sum will be 7 + 5 + 3 + 4 = 19.
Input: N = 2, X[] = {25, 5}, Y[] = {3, 30}
Output: 55
Explanation: It is optimal to shift after the first index.
Sum at first index= 25
After Shifted from X[] to Y[] after first index sum at second index = 25+Y[2] = 25 + 30 = 55.
It can be verified that sum more than 55 is not possible.
Approach: Implement the idea below to solve the problem
The problem can be solved by using prefix and suffix sum technique. For each index check the prefix sum and suffix sum. If we shift after index i, the sum (say Xi) will be prefix[i] + suffix[i+1].
The maximum value among all the Xi is the maximum possible sum by performing at most 1 shift.
Follow the steps mentioned below to implement the idea:
- Initiate the suffix array of Y[].
- Iterate from i=N-1 to 0 to generate the suffix array.
- Traverse from i = 0 to N:
- Check if the sum of elements in X[] till i and the suffix sum of Y[] from i+1 is greater than the maximum possible sum.
- If it is greater, update the maximum possible.
- Return the maximum possible after the iteration is over.
Code to implement the approach:
C++
#include <iostream>
using namespace std;
int max( int a, int b) { return a >= b ? a : b; }
int maximiseSum( int n, int X[], int Y[])
{
int suf[n];
suf[n - 1] = Y[n - 1];
int sum = 0, ans = 0;
for ( int i = n - 2; i >= 0; i--) {
sum += Y[i];
suf[i] += suf[i + 1];
}
ans = sum;
sum = 0;
for ( int i = 0; i < n - 1; i++) {
sum += X[i];
ans = max(ans, sum + suf[i + 1]);
}
sum += X[n - 1];
ans = max(ans, sum);
return ans;
}
int main()
{
int N = 4;
int X[] = { 7, 5, 3, 4 };
int Y[] = { 2, 3, 1, 3 };
cout << maximiseSum(N, X, Y);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
int N = 4 ;
int [] X = { 7 , 5 , 3 , 4 };
int [] Y = { 2 , 3 , 1 , 3 };
System.out.println(maximiseSum(N, X, Y));
}
static long max( long a, long b)
{
return a >= b ? a : b;
}
static long maximiseSum( int n, int X[], int Y[])
{
long [] suf = new long [n];
suf[n - 1 ] = Y[n - 1 ];
long sum = 0 , ans = 0 ;
for ( int i = n - 2 ; i >= 0 ; i--) {
sum += Y[i];
suf[i] += suf[i + 1 ];
}
ans = sum;
sum = 0 ;
for ( int i = 0 ; i < n - 1 ; i++) {
sum += X[i];
ans = max(ans, sum + suf[i + 1 ]);
}
sum += X[n - 1 ];
ans = max(ans, sum);
return ans;
}
}
|
Python3
def maximiseSum(n, X, Y):
suf = [ 0 ] * n
suf[n - 1 ] = Y[n - 1 ]
sum = 0
ans = 0
for i in range (n - 2 , - 1 , - 1 ):
sum + = Y[i]
suf[i] + = suf[i + 1 ]
ans = sum
sum = 0
for i in range (n - 1 ):
sum + = X[i]
ans = max (ans, sum + suf[i + 1 ])
sum + = X[n - 1 ]
ans = max (ans, sum )
return ans
N = 4
X = [ 7 , 5 , 3 , 4 ]
Y = [ 2 , 3 , 1 , 3 ]
print (maximiseSum(N, X, Y))
|
C#
using System;
public class GFG {
static long maximiseSum( int n, int [] X, int [] Y)
{
long [] suf = new long [n];
suf[n - 1] = Y[n - 1];
long sum = 0, ans = 0;
for ( int i = n - 2; i >= 0; i--) {
sum += Y[i];
suf[i] += suf[i + 1];
}
ans = sum;
sum = 0;
for ( int i = 0; i < n - 1; i++) {
sum += X[i];
ans = max(ans, sum + suf[i + 1]);
}
sum += X[n - 1];
ans = max(ans, sum);
return ans;
}
static long max( long a, long b)
{
return a >= b ? a : b;
}
static public void Main()
{
int N = 4;
int [] X = { 7, 5, 3, 4 };
int [] Y = { 2, 3, 1, 3 };
Console.WriteLine(maximiseSum(N, X, Y));
}
}
|
Javascript
function max(a, b){
return a >= b ? a : b;
}
function maximizeSum(n, X, Y){
let suf = new Array();
suf[n-1] = Y[n-1];
let sum = 0, ans = 0;
for (let i = n - 2; i >= 0; i--) {
sum += Y[i];
suf[i] += suf[i + 1];
}
ans = sum;
sum = 0;
for (let i = 0; i < n - 1; i++) {
sum += X[i];
ans = max(ans, sum + suf[i + 1]);
}
sum += X[n - 1];
ans = max(ans, sum);
return ans;
}
let N = 4;
let X = [ 7, 5, 3, 4 ];
let Y = [ 2, 3, 1, 3 ];
console.log(maximizeSum(N, X, Y));
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Related Articles:
Share your thoughts in the comments
Please Login to comment...