PA of Algorithms Q97

0. Given two arrays of numbers a1,..., an and b1,...,bn where each number is 0 or 1, the fastest algorithm to find the largest span (i, j) such that ai + ai+i +....+ aj = bi + bi+i +....+ bj or report that there is no such span,

  • Option : C
  • Explanation :
    a1, a2 .....................an
    b1, b2 .....................bn
    In order to find out the largest span, check the sums of
    ai +............aj and bi +..............bj at each step.
    If a1 + a2 = b1 + b2 go on, check a1 + a2 + a3 and b1 + b2 + b3.
    If not, then check a2 + a3 and b2 + b3
    Similarly a check is done at each of the n places during traversal. A separate variable has to be kept that contains the maximum span observed hitherto
    Hence fastest algorithm computes with (~) (n) time and space.
Cancel reply

Your email address will not be published. Required fields are marked *


Cancel reply

Your email address will not be published. Required fields are marked *