Company: Amazon
Question: In a given array A[] find the maximum value of (A[i] – i) - (A[j] – j) where i is not equal to j.
Question: In a given array A[] find the maximum value of (A[i] – i) - (A[j] – j) where i is not equal to j.
i and j vary from 0 to n-1 and N is size of input array A[]. Value of N is always greater than 1.
Input:
The first line of input contains a single integer T denoting the number of test cases. Then T test cases follow. Each test case consist of two lines. The first line of each test case consists of an integer N, where N is the size of array.The second line of each test case contains N space separated integers denoting array elements.
Output:
Corresponding to each test case, in a new line, print the maximum value.
Constraints:
1 ≤ T ≤ 100
2 ≤ N ≤ 200
1 ≤ A[i] ≤ 1000
Example:
2 ≤ N ≤ 200
1 ≤ A[i] ≤ 1000
Example:
Input
1
5
9 15 4 12 13
1
5
9 15 4 12 13
Output
12
Explanation
(a[1]-1) - (a[2]-2) = (15-1)-(4-2) = 12.
12
Explanation
(a[1]-1) - (a[2]-2) = (15-1)-(4-2) = 12.
CODE:
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int m = sc.nextInt();
for(int mi=0;mi<m;mi++){
int n = sc.nextInt();
int [] ar= new int[n];
for(int i=0;i<n;i++){
ar[i]=sc.nextInt();
}
int max=0;
for(int j=0;j<n;j++){
for(int k=0;k<n;k++){
max= Math.max(max,(ar[j]-j-ar[k]+k));
}
}
System.out.println(max);
}
}
}
EXECUTION TIME: 0.23s
Comments
Post a Comment