Maximum length of subarray with same sum at corresponding indices from two Arrays
Last Updated :
04 Sep, 2022
Given two arrays A[] and B[] both consisting of N integers, the task is to find the maximum length of subarray [i, j] such that the sum of A[i… j] is equal to B[i… j].
Examples:
Input: A[] = {1, 1, 0, 1}, B[] = {0, 1, 1, 0}
Output: 3
Explanation: For (i, j) = (0, 2), sum of A[0… 2] = sum of B[0… 2] (i.e, A[0]+A[1]+A[2] = B[0]+B[1]+B[2] => 1+1+0 = 0+1+1 => 2 = 2). Similarly, for (i, j) = (1, 3), sum of A[1… 3] = B[1… 3]. Therefore, the length of the subarray with equal sum is 3 which is the maximum possible.
Input: A[] = {1, 2, 3, 4}, B[] = {4, 3, 2, 1}
Output: 4
Approach: The given problem can be solved by using a Greedy Approach with the help of unordered maps. It can be observed that for a pair (i, j), if the sum of A[i… j] = sum of B[i… j], then must hold true. Therefore, a prefix sum array of the difference (A[x] – B[x]) can be created. It can be observed that the repeated values in the prefix sum array represent that the sum of a subarray between the two repeated values must be 0. Hence, keep a track of the maximum size of such subarrays in a variable maxSize which will be the required answer.
Below is the implementation of the above approach:
C++
#include "bits/stdc++.h"
using namespace std;
int maxLength(vector< int >& A, vector< int >& B)
{
int n = A.size();
int maxSize = 0;
unordered_map< int , int > pre;
int diff = 0;
pre[0] = 0;
for ( int i = 0; i < n; i++) {
diff += (A[i] - B[i]);
if (pre.find(diff) == pre.end()) {
pre = i + 1;
}
else {
maxSize = max(maxSize, i - pre + 1);
}
}
return maxSize;
}
int main()
{
vector< int > A = { 1, 2, 3, 4 };
vector< int > B = { 4, 3, 2, 1 };
cout << maxLength(A, B);
return 0;
}
|
Java
import java.util.HashMap;
class GFG {
public static int maxLength( int [] A, int [] B) {
int n = A.length;
int maxSize = 0 ;
HashMap<Integer, Integer> pre = new HashMap<Integer, Integer>();
int diff = 0 ;
pre.put( 0 , 0 );
for ( int i = 0 ; i < n; i++) {
diff += (A[i] - B[i]);
if (!pre.containsKey(diff)) {
pre.put(diff, i + 1 );
}
else {
maxSize = Math.max(maxSize, i - pre.get(diff) + 1 );
}
}
return maxSize;
}
public static void main(String args[]) {
int [] A = { 1 , 2 , 3 , 4 };
int [] B = { 4 , 3 , 2 , 1 };
System.out.println(maxLength(A, B));
}
}
|
Python3
def maxLength(A, B):
n = len (A)
maxSize = 0
pre = {}
diff = 0
pre[ 0 ] = 0
for i in range ( 0 , n):
diff + = (A[i] - B[i])
if ( not (diff in pre)):
pre = i + 1
else :
maxSize = max (maxSize, i - pre + 1 )
return maxSize
if __name__ = = "__main__" :
A = [ 1 , 2 , 3 , 4 ]
B = [ 4 , 3 , 2 , 1 ]
print (maxLength(A, B))
|
C#
using System;
using System.Collections.Generic;
class GFG{
public static int maxLength( int [] A, int [] B) {
int n = A.Length;
int maxSize = 0;
Dictionary< int , int > pre =
new Dictionary< int , int >();
int diff = 0;
pre.Add(0, 0);
for ( int i = 0; i < n; i++) {
diff += (A[i] - B[i]);
if (!pre.ContainsKey(diff)) {
pre.Add(diff, i + 1);
}
else {
maxSize = Math.Max(maxSize, i - pre[(diff)] + 1);
}
}
return maxSize;
}
public static void Main()
{
int [] A = { 1, 2, 3, 4 };
int [] B = { 4, 3, 2, 1 };
Console.Write(maxLength(A, B));
}
}
|
Javascript
<script>
const maxLength = (A, B) => {
let n = A.length;
let maxSize = 0;
let pre = {};
let diff = 0;
pre[0] = 0;
for (let i = 0; i < n; i++) {
diff += (A[i] - B[i]);
if (!(diff in pre)) {
pre = i + 1;
}
else {
maxSize = Math.max(maxSize, i - pre + 1);
}
}
return maxSize;
}
let A = [1, 2, 3, 4];
let B = [4, 3, 2, 1];
document.write(maxLength(A, B));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...