Company: Google

Difficulty: medium

Problem Statement

Maximum Inversion Distance Given an integer array arr , find the maximum value of j - i over all pairs of indices satisfying 0 <= i < j < N and arr[i] > arr[j] . If no such pair exists, print 0 . Input Format The first line contains N , the length of the array. The second line contains N space-separated integers. Output Format Print the maximum valid distance j - i , or 0 if no valid pair exists. Constraints 1 <= N <= 200000 -10^9 <= arr[i] <= 10^9 Equal values do not satisfy the strict inequality. The answer fits in a 32-bit signed integer. Examples Example 1 Input: 5 5 1 4 2 3 Output: 4 Choosing i = 0 and j = 4 gives 5 > 3 and distance 4 . Example 2 Input: 4 1 2 2 5 Output: 0 The array has no earlier element that is strictly greater than a later one. Notes Indices are zero-based when defining i and j ; only the distance is printed. The required running time is better than quadratic.

More Google OA questionsInterview experiences