Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
3
//'main' method must be in a class 'Rextester'. //openjdk version '11.0.5' import java.util.*; import java.lang.*; class Rextester { static class Helper { int x; int y; public Helper(int x,int y) { this.x=x; this.y=y; } } static class Compare { static void compare(Helper b[], int n) { Arrays.sort(b, new Comparator<Helper>() { public int compare(Helper p1, Helper p2) { if(p1.x == p2.x) return p1.y - p2.y; else return p1.x - p2.x; } } ); } } static int minhelp(Helper b[], int n) { int ans=1,i,max,j; int range=b[0].y; if(b[0].x != 0) return -1; if(range == n) return ans; for( i=0;i <n;i++) // yaha i<n-1 ? { if(range == n) break; max=range; for(j=i+1;j<n;j++) { if(b[j].x <=range ) max=Math.max(max,b[j].y); else break; } ans++; i=j; i=i-1; range=max; } if(range == n) return ans; else return -1; } public static void main(String args[]) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int a[]=new int[n]; for(int i=0;i<n;i++) a[i]=sc.nextInt(); Helper b[]=new Helper[n]; int i,j,x,y; for(i=0;i<n;i++) { //j=0; x=(i-a[i]); y=(i + a[i]); if( x < 0 || x > n) { // System.out.print(x+ " "); x=0; } else { // System.out.print(x+ " "); x=i-a[i]; } if( y > n || y < 0) { // System.out.print(y+ " "); y=0; } else { // System.out.print(y+ " "); y=i + a[i]; } b[i]=new Helper(x,y); // System.out.println(); } /* System.out.println(); System.out.println(); for(i=0;i<n;i++) { System.out.print(b[i].x + " " +b[i].y); System.out.println(); } */ Compare z= new Compare(); z.compare(b,n); System.out.println("After sorting"); for(i=0;i<n;i++) { System.out.print(b[i].x + " " +b[i].y); System.out.println(); } int ans=minhelp(b,n); System.out.println(ans); } }
run
|
edit
|
history
|
help
0
rta
updated
Prime or Not
Java - SimpleDateFormat(ting) for XML
Circular array Loop?
Increment
Tech No.
Switch case example
loding
AllStar