Maximum Distance Problem Statement

Given an integer array 'A' of size N, your task is to find the maximum value of j - i, with the restriction that A[i] <= A[j], where 'i' and 'j' are indices of the array.

Input:

The input begins with an integer 'T', indicating the number of test cases. Each test case consists of two lines:
The first line contains an integer 'N' representing the size of the array.
The second line contains 'N' space-separated integers denoting the elements of the array.

Output:

For each test case, output a single integer on a new line, representing the maximum possible value of j - i meeting the condition A[i] <= A[j].

Example:

Input:
A = [3, 5, 4, 1]
Output:
2
Explanation:

The maximum distance occurs for the pair (3, 4), giving a distance of 2.

Constraints:

  • 1 <= T <= 100
  • 1 <= N <= 10^4
  • -10^5 <= A[i] <= 10^5

Time limit: 1 second per test case.

Note:

There is no need to print anything explicitly. Implement the required function to generate the output.

Follow-Up:

Consider solving this problem with O(N) time complexity.

Be the first one to answer
Add answer anonymously...
MakeMyTrip Full Stack Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter