Longest unique subarray of an Array with maximum sum in another Array
Last Updated :
11 Feb, 2022
Given two arrays X[] and Y[] of size N, the task is to find the longest subarray in X[] containing only unique values such that a subarray with similar indices in Y[] should have a maximum sum. The value of array elements is in the range [0, 1000].
Examples:
Input: N = 5,
X[] = {0, 1, 2, 0, 2},
Y[] = {5, 6, 7, 8, 22}
Output: 21
Explanation: The largest unique subarray in X[] with maximum sum in Y[] is {1, 2, 0}.
So, the subarray with same indices in Y[] is {6, 7, 8}.
Therefore maximum sum is 21.
Input: N = 3,
X[] = {1, 1, 1},
Y[] = {2, 6, 7}
Output: 7
Naive Approach: The task can be solved by generating all the subarrays of the array X[], checking for each subarray if it is valid, and then calculating the sum in the array for corresponding indices in Y.
Time Complexity: O(N3)
Auxiliary Space: O(N)
Efficient Approach: The task can be solved using the concept of the sliding window. Follow the below steps to solve the problem:
- Create an array m of size 1001 and initialize all elements as -1. For index i, m[i] stores the index at which i is present in the subarray. If m[i] is -1, it means the element doesn’t exist in the subarray.
- Initialize low = 0, high = 0, these two pointers will define the indices of the current subarray.
- currSum and maxSum, define the sum of the current subarray and the maximum sum in the array.
- Iterate over a loop and check if the current element at index high exists in the subarray already, if it does find the sum of elements in the subarray, update maxSum (if needed) and update low. Now, finally, move to the next element by incrementing high.
- Note that you will be encountering a corner case which can result in wrong answer, for instance, let’s consider our first Input, then the subarray {8, 2} is the right choice and 30 is our right answer. So handle that corner case separately by summing all elements from the previous low to high.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findMaxSumSubarray( int X[], int Y[],
int N)
{
int m[1001];
for ( int i = 0; i < 1001; i++)
m[i] = -1;
int low = 0, high = 0;
int currSum = 0, maxSum = 0;
while (high <= N) {
if (high==N){
currSum=0;
for ( int i = low; i <= high - 1;
i++)
currSum += Y[i];
maxSum = max(maxSum, currSum);
break ;
}
if (m[X[high]] != -1
&& m[X[high]] >= low) {
currSum = 0;
for ( int i = low; i <= high - 1;
i++)
currSum += Y[i];
maxSum = max(maxSum, currSum);
low = m[X[high]] + 1;
}
m[X[high]] = high;
high++;
}
return maxSum;
}
int main()
{
int X[] = { 0, 1, 2, 0, 2 };
int Y[] = { 5, 6, 7, 8, 22 };
int N = sizeof (X) / sizeof (X[0]);
int maxSum = findMaxSumSubarray(X, Y, N);
cout << maxSum << endl;
return 0;
}
|
Java
import java.util.*;
public class GFG
{
static int findMaxSumSubarray( int X[], int Y[],
int N)
{
int m[] = new int [ 1001 ];
for ( int i = 0 ; i < 1001 ; i++)
m[i] = - 1 ;
int low = 0 , high = 0 ;
int currSum = 0 , maxSum = 0 ;
while (high <= N) {
if (high==N){
currSum = 0 ;
for ( int i = low; i <= high - 1 ;i++)
currSum += Y[i];
maxSum = Math.max(maxSum, currSum);
break ;
}
if (m[X[high]] != - 1
&& m[X[high]] >= low) {
currSum = 0 ;
for ( int i = low; i <= high - 1 ;
i++)
currSum += Y[i];
maxSum = Math.max(maxSum, currSum);
low = m[X[high]] + 1 ;
}
m[X[high]] = high;
high++;
}
return maxSum;
}
public static void main(String args[])
{
int X[] = { 0 , 1 , 2 , 0 , 2 };
int Y[] = { 5 , 6 , 7 , 8 , 22 };
int N = X.length;
int maxSum = findMaxSumSubarray(X, Y, N);
System.out.println(maxSum);
}
}
|
Python3
def findMaxSumSubarray(X, Y, N):
m = [ 0 ] * ( 1001 );
for i in range ( 1001 ):
m[i] = - 1 ;
low = 0
high = 0
currSum = 0
maxSum = 0 ;
while (high < = N):
if (high = = N):
currSum = 0 ;
for i in range (low, high):
currSum + = Y[i];
maxSum = max (maxSum, currSum);
break ;
if (m[X[high]] ! = - 1 and m[X[high]] > = low):
currSum = 0 ;
for i in range (low, high):
currSum + = Y[i];
maxSum = max (maxSum, currSum);
low = m[X[high]] + 1 ;
m[X[high]] = high;
high + = 1
return maxSum;
X = [ 0 , 1 , 2 , 0 , 2 ];
Y = [ 5 , 6 , 7 , 8 , 22 ];
N = len (X)
maxSum = findMaxSumSubarray(X, Y, N);
print (maxSum, end = "")
|
C#
using System;
using System.Collections.Generic;
public class GFG{
static int findMaxSumSubarray( int [] X, int [] Y, int N)
{
int [] m = new int [1001];
for ( int i = 0; i < 1001; i++)
m[i] = -1;
int low = 0, high = 0;
int currSum = 0, maxSum = 0;
while (high <= N)
{
if (high==N){
currSum=0;
for ( int i = low; i <= high - 1;
i++)
currSum += Y[i];
maxSum = Math.Max(maxSum, currSum);
break ;
}
if (m[X[high]] != -1
&& m[X[high]] >= low) {
currSum = 0;
for ( int i = low; i <= high - 1;
i++)
currSum += Y[i];
maxSum = Math.Max(maxSum, currSum);
low = m[X[high]] + 1;
}
m[X[high]] = high;
high++;
}
return maxSum;
}
static public void Main ()
{
int [] X = { 0, 1, 2, 0, 2 };
int [] Y = { 5, 6, 7, 8, 22 };
int N = X.Length;
int maxSum = findMaxSumSubarray(X, Y, N);
Console.WriteLine(maxSum);
}
}
|
Javascript
<script>
function findMaxSumSubarray(X, Y, N)
{
let m = new Array(1001);
for (let i = 0; i < 1001; i++)
m[i] = -1;
let low = 0, high = 0;
let currSum = 0, maxSum = 0;
while (high <= N)
{
if (high==N){
currSum=0;
for (let i = low; i <= high - 1;
i++)
currSum += Y[i];
maxSum = Math.max(maxSum, currSum);
break ;
}
if (m[X[high]] != -1
&& m[X[high]] >= low) {
currSum = 0;
for (let i = low; i <= high - 1;
i++)
currSum += Y[i];
maxSum = Math.max(maxSum, currSum);
low = m[X[high]] + 1;
}
m[X[high]] = high;
high++;
}
return maxSum;
}
let X = [0, 1, 2, 0, 2];
let Y = [5, 6, 7, 8, 22];
let N = X.length
let maxSum = findMaxSumSubarray(X, Y, N);
document.write(maxSum + '<br>' )
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...