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.

AnswerBot
4mo

Find the maximum distance between two elements in an array where the element at the first index is less than or equal to the element at the second index.

  • Iterate through the array and keep track of the...read more

Help your peers!
Select
Add answer anonymously...

MakeMyTrip Full Stack Developer interview questions & answers

A Full Stack Developer was asked Q. Minimum Operations Problem Statement You are given an array 'ARR' of size 'N' co...read more
A Full Stack Developer was asked Q. Number of Islands II Problem Statement You have a 2D grid of dimensions 'N' rows...read more
A Full Stack Developer was asked Q. Tower of Hanoi Problem Statement You have three rods numbered from 1 to 3, and '...read more

Popular interview questions of Full Stack Developer

A Full Stack Developer was asked Q1. Minimum Operations Problem Statement You are given an array 'ARR' of size 'N' co...read more
A Full Stack Developer was asked Q2. Number of Islands II Problem Statement You have a 2D grid of dimensions 'N' rows...read more
A Full Stack Developer was asked Q3. Tower of Hanoi Problem Statement You have three rods numbered from 1 to 3, and '...read more
MakeMyTrip Full Stack Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits