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
3d
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!
Add answer anonymously...
Top MakeMyTrip Full Stack Developer interview questions & answers
Popular interview questions of Full Stack Developer
Stay ahead in your career. Get AmbitionBox app
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
Get AmbitionBox app