-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCovert_to_permutation.java
More file actions
40 lines (36 loc) · 1.03 KB
/
Copy pathCovert_to_permutation.java
File metadata and controls
40 lines (36 loc) · 1.03 KB
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
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Scanner;
public class Covert_to_permutation {
//Every element needs to between 0 and n
//Max 2 same element's
//int(
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt();
int [] arr = new int[n];
for(int i = 0 ; i< n ; i++){
arr[i] = sc.nextInt();
}
Arrays.sort(arr);
int totalSumShouldBe = (n*(n+1))/2;
int actualSum =0;
boolean bound = true;
for(int i = 0 ; i< n; i++){
if(arr[i]>i+1){
bound = false;
break;
}
actualSum+=arr[i];
}
if(!bound){
System.out.println("-1");
}
else {
System.out.println(totalSumShouldBe-actualSum);
}
}
}
}