edited by
28,461 views
66 votes
66 votes

Given two arrays of numbers $a_{1},...,a_{n}$ and $b_{1},...,b_{n}$ where each number is $0$ or $1$, the fastest algorithm to find the largest span $(i, j)$ such that $ a_{i}+a_{i+1}+\dots+a_{j}=b_{i}+b_{i+1}+\dots+b_{j}$ or report that there is not such span,

  1. Takes $ O(3^{n})$ and $\Omega(2^{n})$ time if hashing is permitted
  2. Takes $ O(n^{3})$ and $\Omega(n^{2.5})$ time in the key comparison mode
  3. Takes $\Theta (n)$ time and space
  4. Takes $O(\sqrt n)$ time only if the sum of the $2n$ elements is an even number
edited by

10 Answers

1 votes
1 votes
Here we can use a prefix sum algorithm

like array a[0-n] and b[0-n] let n = 5

content of a = [1,0,0,1,0] and b = [1,0,1,1,0]

after applying prefix sum we will get

a =[1,1,1,2,2] b = [1,1,2,3,3] which will take O(n)

now we will compare a[n-1] and b[n-1] so here a[n-1] is smaller so we will find the a[n-1] in b if we found it we can return te largest span
0 votes
0 votes

I have used dynamic programming here:

Seems like running time = O(n2) and space complexity = O(n2)

Here is the code:

#include<iostream>
using namespace std;

int main(){

    int n, i, j;
    int A[n], B[n];
    int S1[n][n] = {0}, S2[n][n] = {0};
    int span = 0;

    cout<<"Enter array lengths: ";
    cin>>n;
    cout<<"Inputs: ";
    for(i = 0; i < n; i++)
        cin>>A[i]>>B[i];

    for(i = 0; i <= n-2; i++){
        for(j = i+1; j <= n-1; j++){
            S1[i][j] = S1[i][j-1] + A[j-1];
            S2[i][j] = S2[i][j-1] + B[j-1];
            if(S1[i][j] == S2[i][j]){
                if((j-i) > span)
                    span = j-i;
            }
        }
    }

    cout<<"Longest such span is "<<span;

    return 0;
}

 

0 votes
0 votes

Best solution is O(n)

First let c[i] = a[i] - b[i], then question become find ij, which sum(c[i], c[i+1], ..., c[j]) = 0, and max j - i.

Second let d[0] = 0d[i + 1] = d[i] + c[i], i >= 0, then question become find ij, which d[j + 1] == d[i], and max j - i.

The value of d is in range [-n, n], so we can use following code to find the answer

answer = 0, answer_i = 0, answer_j = 0
sumHash[2n + 1] set to -1
for (x <- 0 to n) {
  if (sumHash[d[x]] == -1) {
    sumHash[d[x]] = x
  } else {
    y = sumHash[d[x]]
    // find one answer (y, x), compare to current best
    if (x - y > answer) {
      answer = x - y
      answer_i = y
      answer_j = y
    }
  }
}
0 votes
0 votes
option(c) is correct

we can simply do this by using only one for loop that will take O(n) time and space complexity=input+extra =O(n)+O(1)=O(n).. as follows:-

#include <stdio.h>
int main() {
    int A[10]={1,0,0,0,0,0,0,0,0,0};
    int B[10]={0,1,1,1,1,1,1,1,1,1};
    int i,len=0,l=0;
    for(i=0;i<10;i++)
    {
          if (A[i]==B[i])
          {
              len++;
             
          }
        else{
            
            if(l<len)
            {
                l=len;
            }
            len=0;
        }
    }
    if(l!=0)
    printf("%d",l);
    
    else
    printf("no match");

    return 0;

}
Answer:

Related questions