⭐Max Distance

Given an array A of integers, find the maximum of j - i subjected to the constraint of A[i] <= A[j].

Bruteforce Solution

For every value i check every value j = i + 1 onwards.

Time Complexity: O(n2)O(n^2)

Space Complexity: O(1)O(1)

Using DP/Recursion/Memoization

Time Complexity: O(n)O(n)​

Space Complexity: O(n)+O(n)O(n)+O(n) for DP and stack

leftMin and rightMax

Time Complexity: O(n)O(n)​

Space Complexity :O(n)O(n)​

Last updated