SpaceArr March long challenge

package codechef;

import java.util.;
import java.lang.
;
import java.io.*;

public class My_sol_spacearr {

	/* package codechef; // don't place package name! */

	

	/* Name of the class has to be "Main" only if the class is public. */
	
	
		public static void main (String[] args) 
		{
			// your code goes here
			
			 Scanner sc =new Scanner(System.in);
			 int n= sc.nextInt();
			
			int a[] = new int[n];
			int b[] = new int[n];
			
			for(int i=1;i<=n;i++){
			    a[i] = sc.nextInt();
			}
			
			for(int i=1;i<=n;i++){
			    b[i] = i;
			}
			
			for(int i=0;i<n;i++){
			    for(int j=i+1;j<=n;j++){
			      
			      if(a[i]>a[j])  {
			          int temp = a[i];
			          a[i] = a[j];
			          a[j] = temp;
			      }
			      
			    }
			    System.out.println(a[i]);
			}
			int count = 0;
			for(int j=1;j<=n;j++){
			    if(a[j] == j+1){
			    count = count+0;
			}
			else{
			    count = count + (a[j] - (j+1));
			    }
			}
			if(count%2==0)
				System.out.println("Second");
				else
				System.out.println("First");
			}
			
		}

Can Anyone Please tell me what is wrong with this code.

Dude, 0 based indexing!!!

What are these things doing :roll_eyes:

Thank you my it was my carelessness…

System.out.println(a[i]);

can you please tell me why this line is not printing the array…

The input format is different. There are T Testcases. For each test case, you are supposed to perform the above thing. So, it would look something like this:

int t = scan.nextInt();
for(; t > 0; t--) {
    int n = scan.nextInt();
    int a[] = new int[n];
    for(int i = 0; i < n; i++) {
        a[i] = scan.nextInt();
    }
    // Some logic here.
}

Ok, I got . Thank you once again

1 Like