Maximum Difference in Positions

Moderate
0/80
Average time to solve is 30m
profile
Contributed by
3 upvotes
Asked in companies
SamsungeBayIBM

Problem statement

There are ‘N’ students sitting in a row each having some marks that they have obtained in the previous examinations. You have to find the maximum difference in the position of two students such that the second student has marks greater or equal to the first student. Formerly, You have to find the maximum value of ‘j’ - ‘i’ such that ‘i’ < ‘j’ and arr[ i ] <= arr[ j ].

Note:

Return 0, if there are no such two students present.
Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line contains a single integer ‘T’ representing the number of test cases. 
The first line of each test case will contain a single integer ‘N’ which denotes the number of students in the row.
The second line of each test case contains the ‘N’ space-separated integers in which the “i-th” number represents the marks of the “i-th” student.
Output Format:
For each test case, print the maximum difference between the two students described above. If, there are no such students then print 0.

Output for every test case will be printed in a separate line.
Note:
You don’t need to print anything; It has already been taken care of.
Constraints:
1 <= T <= 50
2 <= N <= 10000
0 <= arr[ i ] <= 10^5

Where ‘T’ is the number of test cases.
Where 'N' is the number of students in the row and “arr[ i ]” denotes the marks of “i-th” student.

Time limit: 1 sec
Sample Input 1:
2
3
1 2 3
8
4 3 2 8 1 3 3 3
Sample Output 1:
2
6
Explanation of sample input 1:
In the first test case, the students with marks 1 and 3 ( 1<= 3) have the maximum difference between their positions.

In the second test case, the students with marks 3 and 3 ( 3<= 3) have the maximum difference between their positions, 7 - 1 = 6.
Sample Input 2:
2
2
5 6
2
6 5
Sample Output 2:
1
0
Explanation for sample input 2:
In the first test case, the students with marks 5 and 6 ( 5 <= 6) have the maximum difference between their positions.

In the second test case, there are no such students present.
Hint

Can you think of going through each pair of students?

Approaches (3)
Brute Force

The basic idea is to iterate through all the pairs of students and find the maximum difference between two students such that the first one has marks less than or equal to the second one.

 

The steps are as follows:

 

  1. Create a variable to store the answer. (say, “ans”)
  2. Iterate through the “arr”. (say, iterator = ‘i’)
    • Iterate again through the “arr” but from ‘i’ + 1. (say, iterator = ‘j’)
      • Check if arr[ i ] is less than or equal to arr[ j ] then store the maximum between the “ans” and ‘j’ - ‘i’ in the “ans”.
  3. Return “ans”.
Time Complexity

O(N^2), where ‘N’ is the total number of students in the row.

 

Since we are iterating through every pair of students using a nested loop and so the overall time complexity will be O(N^2).

Space Complexity

O(1)

 

Since we are not using any extra space and so, the overall space complexity will be O(1).

Code Solution
(100% EXP penalty)
Maximum Difference in Positions
Full screen
Console