-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConstrustTheSum.java
More file actions
34 lines (32 loc) · 956 Bytes
/
ConstrustTheSum.java
File metadata and controls
34 lines (32 loc) · 956 Bytes
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
import java.util.*;
public class ConstrustTheSum {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for (int j = 0; j < t; j++) {
int n = sc.nextInt();
int s = sc.nextInt();
long maxSum = (long) n * (n + 1) / 2;
if (s > maxSum) {
System.out.println("-1");
}
List<Integer> result = new ArrayList<>();
for (int i = n; i > 0; i--) {
if (s >= i) {
result.add(i);
s -= i;
}
if (s == 0) break;
}
if (s == 0) {
for (int num : result) {
System.out.print(num + " ");
}
System.out.println();
} else {
System.out.println(-1);
}
}
sc.close();
}
}