My issue
- I have initialized numbers to array
- I Calculater sum of every subArray
- I checked whether sum is Divisible by (n+1) or not
- If divisible printed -1 and if not I printed array
My code
/* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0)
{
int n = sc.nextInt();
int a[] = new int[n];
boolean found = true;
for(int i=0;i<n;i++){
a[i] = i+1;
}
int sum = 0;
for(int i=0;i<n;i++){
for(int j=i;j<n;j++){
sum = sum + a[j];
}
if(sum%(n+1) == 0){
found = false;
}
}
if(found){
for(int i=1;i<=n;i++){
System.out.print(i+" ");
}
System.out.println();
}else{
System.out.println("-1");
}
}
}
}
Problem Link: Construct Permutation Practice Coding Problem