Challenge: Array Index Maximum Difference

Let’s solve the array index maximum difference problem.

Problem

Given an array arr[] of integers, find the two largest differences between indices i and j in the array such that:

  1. i is less than j.
  2. The difference between j and i is the maximum among all possible pairs of indices in the array.
  3. The element at index j is greater than the element at index i i.e., arr[j] > arr[i].

Input

An integer array.

Output

Maximum distance.

Sample input

array = { 33, 9, 10, 3, 2, 60, 30, 33, 1 }

Sample output

ArrayIndexMaxDiff :  6

Let’s look at the illustration to better understand the question.

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.