P19154 [GeeksforGeeks] Longest span with same sum in two binary arrays5142
普及
题目描述

Given two binary arrays arr1[] and arr2[] of same size N. Find length of the longest common span [i, j] where j>=i such that arr1[i] + arr1[i+1] + …. + arr1[j] = arr2[i] + arr2[i+1] + …. + arr2[j].

Example 1:
Input:
N = 6
Arr1[] = {0, 1, 0, 0, 0, 0}
Arr2[] = {1, 0, 1, 0, 0, 1}
Output: 4
Explanation: The longest span with same
sum is from index 1 to 4 following zero
based indexing.

Your Task:
You don't need to read input or print anything. Complete the function longestCommonSum() which takes two arrays arr1, arr2 and integer n as input parameters and returns the length of the longest common span.

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)

Constraints:
1 <= N <= 10^{5}
0 <= Arr1[i], Arr2[i] <= 1

输入格式

见题面

输出格式

见题面

样例输入
N = 6 Arr1[] = {0, 1, 0, 0, 0, 0} Arr2[] = {1, 0, 1, 0, 0, 1}
样例输出
4