forked from Nikky01/GeekForGeeks
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Maximum_Index.java
70 lines (62 loc) · 1.25 KB
/
Maximum_Index.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package com.geek;
import java.util.HashMap;
import java.util.Scanner;
public class Maximum_Index {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc =new Scanner(System.in);
int t=sc.nextInt();
for(int i=0;i<t;i++)
{
int n=sc.nextInt();
int a[]=new int[n];
for(int j=0;j<n;j++)
{
a[j]=sc.nextInt();
}
getMaxIndex(a,n);
}
}
private static void getMaxIndex(int[] a, int n) {
// TODO Auto-generated method stub
// int diff=-1;
// for(int i=0;i<n;i++)
// {
//
// for(int j=n-1;j>i;j--)
// {
// int temp=j-i;
// if(a[j]>a[i]&&temp>diff)
// {
//// System.out.println("fjsj");
//
//
// diff=temp;
//
// }
// }
// }
// System.out.println(diff);
int[] rMax=new int[n];
int[] lMin=new int[n];
lMin[0]=a[0];
for(int i=1;i<n;i++)
lMin[i]=Math.min(a[i], lMin[i-1]);
rMax[n-1]=a[n-1];
for(int i=n-2;i>=0;i--)
rMax[i]=Math.max(a[i], rMax[i+1]);
int i=0,j=0;
int maxDiff=-1;
while( i<n&& j<n)
{
if(lMin[i]<=rMax[j])
{
maxDiff=Math.max(maxDiff, j-i);
j=j+1;
}
else
i=i+1;
}
System.out.println(maxDiff);
}
}