http://www.practice.geeksforgeeks.org/problem-page.php?pid=129
Maximum Index
Given an array A of integers, find the maximum of j - i subjected to the constraint of A[i] <= A[j].
Example :
A : [3 5 4 2]
Output : 2
for the pair (3, 4)
Input:
The first line contains an integer T, depicting total number of test cases.
Then following T lines contains an integer N depicting the size of array and next line followed by the value of array.
Output:
Print the maximum difference of the indexes i and j in a separtate line.
Constraints:
1 ≤ T ≤ 30
1 ≤ N ≤ 1000
0 ≤ A[i] ≤ 100
Example:
Input
1
2
1 10
Output
1
import java.util.*;
import java.lang.*;
import java.io.*; class GFG { public static int func(int[] arr) { int n = arr.length;
int[] dp = new int[n];
int rs = 0; dp[0] = 0;
for(int i=1; i<n; ++i) {
for(int pre=0; pre<i; ++pre) {
if(arr[i] >= arr[pre]) {
dp[i] = Math.max(dp[i], dp[pre] + i - pre);
rs = Math.max(rs, dp[i]);
}
}
}
return rs;
} public static void main (String[] args) {
Scanner in = new Scanner(System.in);
int times = in.nextInt(); while(times > 0) {
--times; int n = in.nextInt();
int[] arr = new int[n];
for(int i=0; i<n; ++i) {
arr[i] = in.nextInt();
} System.out.println(func(arr));
}
}
}