commit 146f58d5d91133e43045df719b3f0ae7ad1266f0
parent 3331ee948ededea78a774eb53facec06035af4c8
Author: Jack Sangdahl <jack@pngu.org>
Date: Thu, 13 Feb 2025 15:10:58 -0700
1092. two sets
Diffstat:
2 files changed, 34 insertions(+), 0 deletions(-)
diff --git a/introductory_problems/readme.md b/introductory_problems/readme.md
@@ -48,3 +48,10 @@ Your task is to count for `k = 1, 2, ..., n` the number of ways two knights can
**Input**: The only input line contains an integer `n`. (Example: 8)
**Output**: Print `n` integers: the results. (Example: 0 6 28 96 252 550 1056 1848)
+
+### Two Sets
+Your task is to divide the numbers `1, 2, ..., n` into two sets of equal sum.
+
+**Input**: The only input line contains an integer `n`. (Example: 7)
+
+**Output**: Print "YES", if the division is possible, and "NO" otherwise. After this, print if the division is possible, print an example of how to create the sets.
diff --git a/introductory_problems/two_sets.cpp b/introductory_problems/two_sets.cpp
@@ -0,0 +1,27 @@
+// 1092. Two Sets
+#include <bits/stdc++.h>
+using namespace std;
+
+int main() {
+ static int n;
+ scanf("%d", &n);
+ if (n % 4 == 1 || n % 4 == 2)
+ printf("NO\n");
+ else if (n % 4 == 3) {
+ printf("YES\n");
+ printf("%d\n", n / 2);
+ for (int i = 2; i <= n / 2; i += 2)
+ printf("%d %d ", i, n - i);
+ printf("%d\n%d\n", n, n / 2 + 1);
+ for (int i = 1; i <= n / 2; i += 2)
+ printf("%d %d ", i, n - i);
+ } else {
+ printf("YES\n");
+ printf("%d\n", n / 2);
+ for (int i = 2; i <= n / 2; i += 2)
+ printf("%d %d ", i, n - i + 1);
+ printf("\n%d\n", n / 2);
+ for (int i = 1; i <= n / 2; i += 2)
+ printf("%d %d ", i, n - i + 1);
+ }
+}