626. Widest Pair of Indices With Equal Range Sum

You are given two 0-indexed binary arrays nums1 and nums2. Find the widest pair of indices (i, j) such that i <= j and nums1[i] + nums1[i+1] + ... + nums1[j] == nums2[i] + nums2[i+1] + ... + nums2[j].

The widest pair of indices is the pair with the largest distance between i and j. The distance between a pair of indices is defined as j - i + 1.

Return the distance of the widest pair of indices. If no pair of indices meets the conditions, return 0.

Example 1:

Input: nums1 = [1,1,0,1], nums2 = [0,1,1,0]

Output: 3

Explanation:

If i = 1 and j = 3:

nums1[1] + nums1[2] + nums1[3] = 1 + 0 + 1 = 2.

nums2[1] + nums2[2] + nums2[3] = 1 + 1 + 0 = 2.

The distance between i and j is j - i + 1 = 3 - 1 + 1 = 3.

Example 2:

Input: nums1 = [0,1], nums2 = [1,1]

Output: 1

Explanation:

If i = 1 and j = 1:

nums1[1] = 1.

nums2[1] = 1.

The distance between i and j is j - i + 1 = 1 - 1 + 1 = 1.

Now Your Turn!

Pick the correct output for the given input

Consider the two arrays nums1 = [1,0,1,1,0,1] and nums2 = [0,1,0,1,1,0]. What is the widest pair of indices satisfying the condition?

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • n == nums1.length == nums2.length
  • 1 <= n <= 105
  • nums1[i] is either 0 or 1.
  • nums2[i] is either 0 or 1.

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution {
public:
int widestPairOfIndices(vector<int>& nums1, vector<int>& nums2) {
// Your code goes here
}
};
 
Test Case

Input:

Nums1
Nums2