Company: Wipro_9july

Difficulty: medium

Problem Statement

Everyone has a favorite number. Jacob's favorite number is X and Jayden's favorite number is Y. A non-empty array A consisting of N integers is given. Jacob and Jayden are interested in occurrences of their favorite numbers X and Y in an array A. They are looking for the longest leading fragment ( prefix ) of array A in which there is an equal number of occurrences of X and Y. More formally, they are looking for the largest P, such that 0 ≤ P < N and the number of occurrences of X equals the number of occurrences of Y in the sequence A[0], A[1], ..., A[P]. For example, consider X = 7, Y = 42 and the following array A: A[0] = 6 A[1] = 42 A[2] = 11 A[3] = 7 A[4] = 1 A[5] = 42 There are three prefixes of array A containing the same number of occurrences of X and Y: P = 0: A[0..0] = [6] contains neither 7 nor 42; P = 3: A[0..3] = [6, 42, 11, 7] contains one 7 and one 42; P = 4: A[0..4] = [6, 42, 11, 7, 1] also contains one 7 and one 42. The largest value of P we are looking for is 4,

More Wipro_9july OA questionsInterview experiences