output
stringlengths
52
181k
instruction
stringlengths
296
182k
#include <bits/stdc++.h> int main() { int a[1000]; int b, i, j, c, m, m1, k; scanf("%d", &b); for (i = 0; i < b; i++) { scanf("%d", &c); for (j = 0; j < c; j++) { scanf("%d", &a[j]); } m = abs(a[0] - a[1]); for (j = 0; j < c - 1; j++) { for (k = j + 1; k < c; k++) { m1 = abs(a[j] - a[k]); if (m > m1) { m = m1; } } } printf("%d\n", m); } }
### Prompt Please formulate a Cpp solution to the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> int main() { int a[1000]; int b, i, j, c, m, m1, k; scanf("%d", &b); for (i = 0; i < b; i++) { scanf("%d", &c); for (j = 0; j < c; j++) { scanf("%d", &a[j]); } m = abs(a[0] - a[1]); for (j = 0; j < c - 1; j++) { for (k = j + 1; k < c; k++) { m1 = abs(a[j] - a[k]); if (m > m1) { m = m1; } } } printf("%d\n", m); } } ```
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; vector<int> v(n); for (int i = 0; i < n; i++) { cin >> v[i]; } sort(v.begin(), v.end()); int mx = v[1] - v[0]; for (int i = 0; i < n - 1; i++) { mx = min(mx, v[i + 1] - v[i]); } cout << mx << '\n'; } }
### Prompt Your task is to create a cpp solution to the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; vector<int> v(n); for (int i = 0; i < n; i++) { cin >> v[i]; } sort(v.begin(), v.end()); int mx = v[1] - v[0]; for (int i = 0; i < n - 1; i++) { mx = min(mx, v[i + 1] - v[i]); } cout << mx << '\n'; } } ```
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t-- > 0) { int len; cin >> len; int ar[len]; int min = INT_MAX; for (int i = 0; i < len; i++) cin >> ar[i]; sort(ar, ar + len); for (int i = 0; i < len - 1; i++) { if (ar[i + 1] - ar[i] < min) min = ar[i + 1] - ar[i]; } cout << min << "\n"; } }
### Prompt Generate a CPP solution to the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t-- > 0) { int len; cin >> len; int ar[len]; int min = INT_MAX; for (int i = 0; i < len; i++) cin >> ar[i]; sort(ar, ar + len); for (int i = 0; i < len - 1; i++) { if (ar[i + 1] - ar[i] < min) min = ar[i + 1] - ar[i]; } cout << min << "\n"; } } ```
#include <bits/stdc++.h> using namespace std; int diff(int arr[], int n) { sort(arr, arr + n); int diff = INT_MAX; for (int i = 0; i < n - 1; i++) if (arr[i + 1] - arr[i] < diff) diff = arr[i + 1] - arr[i]; return diff; } int main() { int t; cin >> t; while (t--) { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; cout << diff(a, n) << endl; } }
### Prompt Generate a Cpp solution to the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int diff(int arr[], int n) { sort(arr, arr + n); int diff = INT_MAX; for (int i = 0; i < n - 1; i++) if (arr[i + 1] - arr[i] < diff) diff = arr[i + 1] - arr[i]; return diff; } int main() { int t; cin >> t; while (t--) { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; cout << diff(a, n) << endl; } } ```
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; using vi = vector<int>; using vl = vector<ll>; using vvi = vector<vector<int>>; using vp = vector<pii>; using vs = vector<string>; using qi = queue<int>; using qp = queue<pii>; using pqi = priority_queue<int>; using pqim = priority_queue<pii, vector<pii>, greater<pii>>; using ppp = pair<int, pair<int, int>>; const ll mod = 998244353; int n; vi seq; void input() { cin >> n; seq = vi(n); for (int i = 0; i < n; ++i) cin >> seq[i]; sort(seq.begin(), seq.end()); } void solve() { int mn = seq[1] - seq[0]; for (int i = 2; i < n; ++i) { mn = min(mn, seq[i] - seq[i - 1]); } cout << mn << '\n'; return; } int main() { cin.tie(NULL); ios_base::sync_with_stdio(false); int tc; cin >> tc; while (tc--) { input(); solve(); } }
### Prompt Develop a solution in Cpp to the problem described below: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; using vi = vector<int>; using vl = vector<ll>; using vvi = vector<vector<int>>; using vp = vector<pii>; using vs = vector<string>; using qi = queue<int>; using qp = queue<pii>; using pqi = priority_queue<int>; using pqim = priority_queue<pii, vector<pii>, greater<pii>>; using ppp = pair<int, pair<int, int>>; const ll mod = 998244353; int n; vi seq; void input() { cin >> n; seq = vi(n); for (int i = 0; i < n; ++i) cin >> seq[i]; sort(seq.begin(), seq.end()); } void solve() { int mn = seq[1] - seq[0]; for (int i = 2; i < n; ++i) { mn = min(mn, seq[i] - seq[i - 1]); } cout << mn << '\n'; return; } int main() { cin.tie(NULL); ios_base::sync_with_stdio(false); int tc; cin >> tc; while (tc--) { input(); solve(); } } ```
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; vector<int> arr; cin >> n; while (n--) { int input; cin >> input; arr.push_back(input); } sort(arr.begin(), arr.end()); int max = arr[arr.size() - 1]; for (int i = 0; i < arr.size() - 1; i++) { if (arr[i + 1] - arr[i] < max) max = arr[i + 1] - arr[i]; } cout << max << endl; } return 0; }
### Prompt Please create a solution in Cpp to the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; vector<int> arr; cin >> n; while (n--) { int input; cin >> input; arr.push_back(input); } sort(arr.begin(), arr.end()); int max = arr[arr.size() - 1]; for (int i = 0; i < arr.size() - 1; i++) { if (arr[i + 1] - arr[i] < max) max = arr[i + 1] - arr[i]; } cout << max << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int T, n, ar[100]; cin >> T; while (T--) { int flag = 0; cin >> n; for (int i = 0; i < n; i++) cin >> ar[i]; sort(ar, ar + n); flag = ar[1] - ar[0]; for (int i = 1; i < n - 1; i++) { if (flag > ar[i + 1] - ar[i]) { flag = ar[i + 1] - ar[i]; } } cout << flag << "\n"; } }
### Prompt In CPP, your task is to solve the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int T, n, ar[100]; cin >> T; while (T--) { int flag = 0; cin >> n; for (int i = 0; i < n; i++) cin >> ar[i]; sort(ar, ar + n); flag = ar[1] - ar[0]; for (int i = 1; i < n - 1; i++) { if (flag > ar[i + 1] - ar[i]) { flag = ar[i + 1] - ar[i]; } } cout << flag << "\n"; } } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; int inf = 0x3f3f3f3f; int main() { int t; cin >> t; while (t--) { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); int m = inf; for (int i = 0; i < n - 1; i++) { m = min(m, a[i + 1] - a[i]); } cout << m << endl; } }
### Prompt In cpp, your task is to solve the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; int inf = 0x3f3f3f3f; int main() { int t; cin >> t; while (t--) { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); int m = inf; for (int i = 0; i < n - 1; i++) { m = min(m, a[i + 1] - a[i]); } cout << m << endl; } } ```
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { long long n; cin >> n; int ar[n]; for (int i = 0; i < n; i++) cin >> ar[i]; sort(ar, ar + n); int best = 100000; for (int i = 0; i < n - 1; i++) { best = min(best, abs(ar[i] - ar[i + 1])); } cout << best << "\n"; } return 0; }
### Prompt Generate a cpp solution to the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { long long n; cin >> n; int ar[n]; for (int i = 0; i < n; i++) cin >> ar[i]; sort(ar, ar + n); int best = 100000; for (int i = 0; i < n - 1; i++) { best = min(best, abs(ar[i] - ar[i + 1])); } cout << best << "\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; int s[55]; for (int i = 0; i < n; i++) { cin >> s[i]; } sort(s, s + n, greater<int>()); int min = INT_MAX; for (int i = 0; i < n - 1; i++) { int temp = s[i] - s[i + 1]; if (temp < min) min = temp; } cout << min << endl; } }
### Prompt Your task is to create a Cpp solution to the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; int s[55]; for (int i = 0; i < n; i++) { cin >> s[i]; } sort(s, s + n, greater<int>()); int min = INT_MAX; for (int i = 0; i < n - 1; i++) { int temp = s[i] - s[i + 1]; if (temp < min) min = temp; } cout << min << endl; } } ```
#include <bits/stdc++.h> using namespace std; int t; int a[1001]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> t; while (t--) { int n; int ans = 1e8; cin >> n; memset(a, 0, sizeof(a)); for (int i = 1; i <= n; i++) { cin >> a[i]; } for (int i = 1; i <= n; i++) { for (int j = i + 1; j <= n; j++) { ans = min(ans, abs(a[i] - a[j])); } } cout << ans << endl; } return 0; }
### Prompt Generate a cpp solution to the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int t; int a[1001]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> t; while (t--) { int n; int ans = 1e8; cin >> n; memset(a, 0, sizeof(a)); for (int i = 1; i <= n; i++) { cin >> a[i]; } for (int i = 1; i <= n; i++) { for (int j = i + 1; j <= n; j++) { ans = min(ans, abs(a[i] - a[j])); } } cout << ans << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int arvore[100006]; int n; long long binpow(long long a, long long b) { long long res = 1; while (b > 0) { if (b & 1) res = res * a; a = a * a; b >>= 1; } return res; } int soma(int x) { int s = 0; while (x > 0) { s += arvore[x]; x -= (x & -x); } return s; } void atualiza(int x, int v) { while (x <= n) { arvore[x] += v; x += (x & -x); } } int vis[100005], ax; int flavin(int np, int vp[]) { int ans = 0; for (int i = 1; i <= np; i++) { ans = (ans + vp[i]) % i; vis[ans] = 1; ax = ans; } return ans + 1; } int main(int argc, char const *argv[]) { int n; scanf("%d", &n); while (n--) { int a; scanf("%d", &a); int v[a + 5]; for (int i = 0; i < a; i++) scanf("%d", &v[i]); sort(v, v + a); int ans = 0x3f3f3f3f; for (int i = 0; i < a - 1; i++) ans = min(ans, abs(v[i] - v[i + 1])); printf("%d\n", ans); } return 0; }
### Prompt Your challenge is to write a CPP solution to the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int arvore[100006]; int n; long long binpow(long long a, long long b) { long long res = 1; while (b > 0) { if (b & 1) res = res * a; a = a * a; b >>= 1; } return res; } int soma(int x) { int s = 0; while (x > 0) { s += arvore[x]; x -= (x & -x); } return s; } void atualiza(int x, int v) { while (x <= n) { arvore[x] += v; x += (x & -x); } } int vis[100005], ax; int flavin(int np, int vp[]) { int ans = 0; for (int i = 1; i <= np; i++) { ans = (ans + vp[i]) % i; vis[ans] = 1; ax = ans; } return ans + 1; } int main(int argc, char const *argv[]) { int n; scanf("%d", &n); while (n--) { int a; scanf("%d", &a); int v[a + 5]; for (int i = 0; i < a; i++) scanf("%d", &v[i]); sort(v, v + a); int ans = 0x3f3f3f3f; for (int i = 0; i < a - 1; i++) ans = min(ans, abs(v[i] - v[i + 1])); printf("%d\n", ans); } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; for (int k = 0; k < n; k++) { int s, m = 1000; cin >> s; int a[s]; for (int i = 0; i < s; i++) { cin >> a[i]; } sort(a, a + s); for (int i = 0; i < s - 1; i++) { m = min(m, (a[i + 1] - a[i])); } cout << m << endl; } }
### Prompt Your challenge is to write a CPP solution to the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; for (int k = 0; k < n; k++) { int s, m = 1000; cin >> s; int a[s]; for (int i = 0; i < s; i++) { cin >> a[i]; } sort(a, a + s); for (int i = 0; i < s - 1; i++) { m = min(m, (a[i + 1] - a[i])); } cout << m << endl; } } ```
#include <bits/stdc++.h> using namespace std; void err(istream_iterator<string> it) {} template <typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << '\n'; err(++it, args...); } long long powMod(long long x, long long y) { long long p = 1; while (y) { if (y % 2) { p = (p * x) % ((long long)1e9 + 7); } y /= 2; x = (x * x) % ((long long)1e9 + 7); } return p; } long long invMod(long long x) { return powMod(x, ((long long)1e9 + 7) - 2); } long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } long long CpowMod(long long x, long long y, long long w) { long long p = 1; while (y) { if (y % 2) { p = (p * x) % w; } y /= 2; x = (x * x) % w; } return p; } long long exGcd(long long a, long long b, long long &x, long long &y) { if (b == 0) { y = 0; x = 1; return a; } long long xtmp, ytmp; long long g = exGcd(b, a % b, xtmp, ytmp); x = ytmp; y = xtmp - (a / b) * ytmp; return g; } void solve() { long long t; cin >> t; while (t--) { long long n; cin >> n; vector<long long> a(n); for (auto &it : a) { cin >> it; }; sort(a.begin(), a.end()); long long mn = ((long long)1e18); for (long long i = 0; i <= n - 2; i++) { mn = min(mn, a[i + 1] - a[i]); } cout << mn << '\n'; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); solve(); return 0; }
### Prompt Please create a solution in cpp to the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; void err(istream_iterator<string> it) {} template <typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << '\n'; err(++it, args...); } long long powMod(long long x, long long y) { long long p = 1; while (y) { if (y % 2) { p = (p * x) % ((long long)1e9 + 7); } y /= 2; x = (x * x) % ((long long)1e9 + 7); } return p; } long long invMod(long long x) { return powMod(x, ((long long)1e9 + 7) - 2); } long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } long long CpowMod(long long x, long long y, long long w) { long long p = 1; while (y) { if (y % 2) { p = (p * x) % w; } y /= 2; x = (x * x) % w; } return p; } long long exGcd(long long a, long long b, long long &x, long long &y) { if (b == 0) { y = 0; x = 1; return a; } long long xtmp, ytmp; long long g = exGcd(b, a % b, xtmp, ytmp); x = ytmp; y = xtmp - (a / b) * ytmp; return g; } void solve() { long long t; cin >> t; while (t--) { long long n; cin >> n; vector<long long> a(n); for (auto &it : a) { cin >> it; }; sort(a.begin(), a.end()); long long mn = ((long long)1e18); for (long long i = 0; i <= n - 2; i++) { mn = min(mn, a[i + 1] - a[i]); } cout << mn << '\n'; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); solve(); return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int t, n, i, min, b; scanf("%d", &t); while (t) { scanf("%d", &n); int a[n]; for (i = 0; i < n; i++) scanf("%d", &a[i]); sort(a, a + n); min = 1001; for (i = 0; i < n - 1; i++) { b = a[i + 1] - a[i]; if (b < min) min = b; } printf("%d\n", min); t--; } }
### Prompt Generate a CPP solution to the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int t, n, i, min, b; scanf("%d", &t); while (t) { scanf("%d", &n); int a[n]; for (i = 0; i < n; i++) scanf("%d", &a[i]); sort(a, a + n); min = 1001; for (i = 0; i < n - 1; i++) { b = a[i + 1] - a[i]; if (b < min) min = b; } printf("%d\n", min); t--; } } ```
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a, a + n); int m = a[1] - a[0]; for (int i = n - 1; i >= 2; i--) { int k = a[i] - a[i - 1]; if (k < m) m = k; } cout << m << "\n"; } return 0; }
### Prompt Develop a solution in cpp to the problem described below: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a, a + n); int m = a[1] - a[0]; for (int i = n - 1; i >= 2; i--) { int k = a[i] - a[i - 1]; if (k < m) m = k; } cout << m << "\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; void FAST() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); } int Rselect(vector<int>&, int, int, int); int partition(vector<int>&, int, int); template <typename T> ostream& operator<<(ostream& stream, const vector<T>& vec) { for (auto& i : vec) { stream << i << ' '; } stream << '\n'; return stream; } template <class T> istream& operator>>(istream& stream, vector<T>& vec) { for (auto& i : vec) { stream >> i; } return stream; } void solve(); void scanN(vector<long long>& v, long long n) { for (long long i = 0; i < n; i++) { int num; cin >> num; v.push_back(num); } } int main() { FAST(); int test; cin >> test; while (test--) { solve(); } return 0; } void solve() { long long n; cin >> n; vector<long long> s; scanN(s, n); sort(begin(s), end(s)); vector<long long> diff(n - 1); for (long long i = 0; i < n - 1; i++) { diff[i] = s[i + 1] - s[i]; } sort(begin(diff), end(diff)); cout << diff[0] << endl; } int Rselect(vector<int>& v, int i, int l, int r) { if (l == r) return v[l]; int pivot = partition(v, l, r); if (pivot == i) return v[pivot - 1]; else if (pivot < i) { return Rselect(v, i, pivot, r); } else { return Rselect(v, i, l, pivot - 2); } } int partition(vector<int>& v, int l, int r) { int pivot_index = rand() % (r - l + 1) + l; swap(v[pivot_index], v[l]); int i = l + 1, j = l + 1; while (j <= r) { if (v[j] < v[l]) { swap(v[j], v[i]); i++; } j++; } swap(v[l], v[i - 1]); return i; }
### Prompt Develop a solution in cpp to the problem described below: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; void FAST() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); } int Rselect(vector<int>&, int, int, int); int partition(vector<int>&, int, int); template <typename T> ostream& operator<<(ostream& stream, const vector<T>& vec) { for (auto& i : vec) { stream << i << ' '; } stream << '\n'; return stream; } template <class T> istream& operator>>(istream& stream, vector<T>& vec) { for (auto& i : vec) { stream >> i; } return stream; } void solve(); void scanN(vector<long long>& v, long long n) { for (long long i = 0; i < n; i++) { int num; cin >> num; v.push_back(num); } } int main() { FAST(); int test; cin >> test; while (test--) { solve(); } return 0; } void solve() { long long n; cin >> n; vector<long long> s; scanN(s, n); sort(begin(s), end(s)); vector<long long> diff(n - 1); for (long long i = 0; i < n - 1; i++) { diff[i] = s[i + 1] - s[i]; } sort(begin(diff), end(diff)); cout << diff[0] << endl; } int Rselect(vector<int>& v, int i, int l, int r) { if (l == r) return v[l]; int pivot = partition(v, l, r); if (pivot == i) return v[pivot - 1]; else if (pivot < i) { return Rselect(v, i, pivot, r); } else { return Rselect(v, i, l, pivot - 2); } } int partition(vector<int>& v, int l, int r) { int pivot_index = rand() % (r - l + 1) + l; swap(v[pivot_index], v[l]); int i = l + 1, j = l + 1; while (j <= r) { if (v[j] < v[l]) { swap(v[j], v[i]); i++; } j++; } swap(v[l], v[i - 1]); return i; } ```
#include <bits/stdc++.h> using namespace std; template <typename T> T fact(T n) { return (n == 1 || n == 0) ? 1 : n * fact(n - 1); } bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) { return (a.second < b.second); } int power(int x, unsigned int y) { int res = 1; while (y > 0) { if (y & 1) res = res * x; y = y >> 1; x = x * x; } return res; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while (t--) { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); int m = INT_MAX; for (int i = 0; i < n - 1; i++) { m = min(m, a[i + 1] - a[i]); } cout << m << endl; } }
### Prompt Please create a solution in cpp to the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; template <typename T> T fact(T n) { return (n == 1 || n == 0) ? 1 : n * fact(n - 1); } bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) { return (a.second < b.second); } int power(int x, unsigned int y) { int res = 1; while (y > 0) { if (y & 1) res = res * x; y = y >> 1; x = x * x; } return res; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while (t--) { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); int m = INT_MAX; for (int i = 0; i < n - 1; i++) { m = min(m, a[i + 1] - a[i]); } cout << m << endl; } } ```
#include <bits/stdc++.h> using namespace std; void solve() { long long n, i; cin >> n; vector<long long> vec(n); for (i = 0; i < n; i++) { cin >> vec[i]; } sort(vec.begin(), vec.end()); long long ans = INT_MAX; for (i = 1; i < n; i++) { ans = min(ans, vec[i] - vec[i - 1]); } cout << ans << '\n'; } int main() { clock_t t1 = clock(); ios_base::sync_with_stdio(false), cin.tie(0); long long test = 1; cin >> test; while (test--) { solve(); } clock_t t2 = clock(); cerr << "Time elapsed:- " << (double)(t2 - t1) / CLOCKS_PER_SEC << '\n'; }
### Prompt Generate a CPP solution to the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; void solve() { long long n, i; cin >> n; vector<long long> vec(n); for (i = 0; i < n; i++) { cin >> vec[i]; } sort(vec.begin(), vec.end()); long long ans = INT_MAX; for (i = 1; i < n; i++) { ans = min(ans, vec[i] - vec[i - 1]); } cout << ans << '\n'; } int main() { clock_t t1 = clock(); ios_base::sync_with_stdio(false), cin.tie(0); long long test = 1; cin >> test; while (test--) { solve(); } clock_t t2 = clock(); cerr << "Time elapsed:- " << (double)(t2 - t1) / CLOCKS_PER_SEC << '\n'; } ```
#include <bits/stdc++.h> using namespace std; vector<int> v; int main() { int t; cin >> t; while (t--) { int ans = 1e9; v.clear(); int n; cin >> n; for (int i = 0; i < n; i++) { int x; cin >> x; v.push_back(x); } sort(v.begin(), v.end()); for (int i = 1; i < v.size(); i++) { ans = min(ans, abs(v[i] - v[i - 1])); } cout << ans << endl; } return 0; }
### Prompt Create a solution in cpp for the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; vector<int> v; int main() { int t; cin >> t; while (t--) { int ans = 1e9; v.clear(); int n; cin >> n; for (int i = 0; i < n; i++) { int x; cin >> x; v.push_back(x); } sort(v.begin(), v.end()); for (int i = 1; i < v.size(); i++) { ans = min(ans, abs(v[i] - v[i - 1])); } cout << ans << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int i, j, count1 = 0, count2 = 0; void A(void); void B(void); void C(void); void D(void); void E(void); void F(void); int main(void) { B(); } void B() { int l, t, n, minv, d; cin >> t; for (l = 0; l < t; l++) { cin >> n; vector<int> vec(n); for (i = 0; i < n; i++) { cin >> vec[i]; } sort(vec.begin(), vec.end()); for (i = 0; i < n - 1; i++) { d = vec[i + 1] - vec[i]; if (i == 0) minv = d; minv = min(minv, d); } cout << minv << endl; } } void A() { int t, a, b; cin >> t; for (j = 0; j < t; j++) { cin >> a >> b; if (max(a, b) > 2 * min(a, b)) { cout << max(a, b) * max(a, b) << endl; } else { cout << 4 * min(a, b) * min(a, b) << endl; } } }
### Prompt In Cpp, your task is to solve the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int i, j, count1 = 0, count2 = 0; void A(void); void B(void); void C(void); void D(void); void E(void); void F(void); int main(void) { B(); } void B() { int l, t, n, minv, d; cin >> t; for (l = 0; l < t; l++) { cin >> n; vector<int> vec(n); for (i = 0; i < n; i++) { cin >> vec[i]; } sort(vec.begin(), vec.end()); for (i = 0; i < n - 1; i++) { d = vec[i + 1] - vec[i]; if (i == 0) minv = d; minv = min(minv, d); } cout << minv << endl; } } void A() { int t, a, b; cin >> t; for (j = 0; j < t; j++) { cin >> a >> b; if (max(a, b) > 2 * min(a, b)) { cout << max(a, b) * max(a, b) << endl; } else { cout << 4 * min(a, b) * min(a, b) << endl; } } } ```
#include <bits/stdc++.h> using namespace std; void c_p_c() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } bool isPrime(long long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (long long i = 5; i * i <= n; i += 6) { if (n % i == 0 || n % (i + 2) == 0) return false; } return true; } vector<bool> Sieve(long long n) { bool prime[n + 1]; memset(prime, true, sizeof(prime)); for (long long i = 2; i * i <= n; i++) { if (prime[i]) { for (long long j = i * i; j <= n; j += i) { prime[j] = false; } } } vector<bool> primes(n + 1); primes[0] = false; primes[1] = false; for (long long i = 2; i <= n; i++) { if (prime[i]) primes[i] = true; } return primes; } set<long long> primeFactors(long long n) { set<long long> factors; while (n % 2 == 0) { n /= 2; factors.insert(2); } for (long long i = 3; i <= sqrt(n); i += 2) { while (n % i == 0) { factors.insert(i); n /= i; } } if (n > 2) factors.insert(n); return factors; } long long func(long long n) { long long digit = 0; while (n > 0) { digit = max(digit, n % 10); n /= 10; } return digit; } int32_t main() { c_p_c(); long long t; cin >> t; while (t--) { long long n; cin >> n; long long *arr = new long long[n]; for (long long i = 0; i < n; i++) cin >> arr[i]; sort(arr, arr + n); long long ans = arr[n - 1] - arr[0]; for (long long i = 0; i < n; i++) { for (long long j = i + 1; j < n; j++) { ans = min(ans, arr[j] - arr[i]); } } cout << ans << '\n'; } }
### Prompt Create a solution in CPP for the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; void c_p_c() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } bool isPrime(long long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (long long i = 5; i * i <= n; i += 6) { if (n % i == 0 || n % (i + 2) == 0) return false; } return true; } vector<bool> Sieve(long long n) { bool prime[n + 1]; memset(prime, true, sizeof(prime)); for (long long i = 2; i * i <= n; i++) { if (prime[i]) { for (long long j = i * i; j <= n; j += i) { prime[j] = false; } } } vector<bool> primes(n + 1); primes[0] = false; primes[1] = false; for (long long i = 2; i <= n; i++) { if (prime[i]) primes[i] = true; } return primes; } set<long long> primeFactors(long long n) { set<long long> factors; while (n % 2 == 0) { n /= 2; factors.insert(2); } for (long long i = 3; i <= sqrt(n); i += 2) { while (n % i == 0) { factors.insert(i); n /= i; } } if (n > 2) factors.insert(n); return factors; } long long func(long long n) { long long digit = 0; while (n > 0) { digit = max(digit, n % 10); n /= 10; } return digit; } int32_t main() { c_p_c(); long long t; cin >> t; while (t--) { long long n; cin >> n; long long *arr = new long long[n]; for (long long i = 0; i < n; i++) cin >> arr[i]; sort(arr, arr + n); long long ans = arr[n - 1] - arr[0]; for (long long i = 0; i < n; i++) { for (long long j = i + 1; j < n; j++) { ans = min(ans, arr[j] - arr[i]); } } cout << ans << '\n'; } } ```
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; int a[n], i, j; for (i = 0; i < n; i++) { cin >> a[i]; } sort(a, a + n); int min = abs(a[0] - a[1]), x; for (i = 0; i < n - 1; i++) { x = abs(a[i] - a[i + 1]); if (min > x) min = x; } cout << min; cout << '\n'; } }
### Prompt Develop a solution in Cpp to the problem described below: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; int a[n], i, j; for (i = 0; i < n; i++) { cin >> a[i]; } sort(a, a + n); int min = abs(a[0] - a[1]), x; for (i = 0; i < n - 1; i++) { x = abs(a[i] - a[i + 1]); if (min > x) min = x; } cout << min; cout << '\n'; } } ```
#include <bits/stdc++.h> using namespace std; int main() { int a, b; cin >> a; for (b = 0; b <= a - 1; b++) { int n, i, j, x, min; min = 1000; cin >> n; int s[n]; for (i = 0; i <= n - 1; i++) { cin >> s[i]; } for (i = 0; i <= n - 1; i++) { for (j = i + 1; j < n; j++) { if (s[i] > s[j]) { x = s[i]; s[i] = s[j]; s[j] = x; } } } for (i = n - 1; i >= 1; i--) { if (s[i] - s[i - 1] < min) { min = s[i] - s[i - 1]; } } cout << min << endl; } }
### Prompt Please provide a cpp coded solution to the problem described below: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int a, b; cin >> a; for (b = 0; b <= a - 1; b++) { int n, i, j, x, min; min = 1000; cin >> n; int s[n]; for (i = 0; i <= n - 1; i++) { cin >> s[i]; } for (i = 0; i <= n - 1; i++) { for (j = i + 1; j < n; j++) { if (s[i] > s[j]) { x = s[i]; s[i] = s[j]; s[j] = x; } } } for (i = n - 1; i >= 1; i--) { if (s[i] - s[i - 1] < min) { min = s[i] - s[i - 1]; } } cout << min << endl; } } ```
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a, a + n); int mini = INT_MAX; int k = 0; for (int i = 0; i < n - 1; i++) { if ((a[i + 1] - a[i]) < mini) { mini = a[i + 1] - a[i]; if (mini == 0) { cout << "0\n"; k = 1; break; } } } if (k == 0) cout << mini << "\n"; } return 0; }
### Prompt Create a solution in Cpp for the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a, a + n); int mini = INT_MAX; int k = 0; for (int i = 0; i < n - 1; i++) { if ((a[i + 1] - a[i]) < mini) { mini = a[i + 1] - a[i]; if (mini == 0) { cout << "0\n"; k = 1; break; } } } if (k == 0) cout << mini << "\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; int arr[n], counter = 1000; for (int i = 0; i < n; i++) cin >> arr[i]; sort(arr, arr + n); for (int i = 1; i < n; i++) counter = min(counter, abs(arr[i] - arr[i - 1])); cout << counter << endl; } }
### Prompt Develop a solution in cpp to the problem described below: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; int arr[n], counter = 1000; for (int i = 0; i < n; i++) cin >> arr[i]; sort(arr, arr + n); for (int i = 1; i < n; i++) counter = min(counter, abs(arr[i] - arr[i - 1])); cout << counter << endl; } } ```
#include <bits/stdc++.h> using namespace std; int main() { int t, n, a[50], aux = 0; cin >> t; while (t--) { cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - i - 1; j++) { if (a[j] > a[j + 1]) { aux = a[j]; a[j] = a[j + 1]; a[j + 1] = aux; } } } int result = a[n - 1] - a[0]; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { result = min(result, a[j] - a[i]); } } cout << result << endl; } }
### Prompt Your challenge is to write a CPP solution to the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int t, n, a[50], aux = 0; cin >> t; while (t--) { cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - i - 1; j++) { if (a[j] > a[j + 1]) { aux = a[j]; a[j] = a[j + 1]; a[j + 1] = aux; } } } int result = a[n - 1] - a[0]; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { result = min(result, a[j] - a[i]); } } cout << result << endl; } } ```
#include <bits/stdc++.h> using namespace std; int main() { long t, n; cin >> t; while (t--) { cin >> n; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); int ans = a[1] - a[0]; for (int i = 2; i < n; i++) if (a[i] - a[i - 1] < ans) ans = a[i] - a[i - 1]; cout << ans << '\n'; } }
### Prompt Please formulate a cpp solution to the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { long t, n; cin >> t; while (t--) { cin >> n; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); int ans = a[1] - a[0]; for (int i = 2; i < n; i++) if (a[i] - a[i - 1] < ans) ans = a[i] - a[i - 1]; cout << ans << '\n'; } } ```
#include <bits/stdc++.h> using namespace std; int min(int a, int b) { if (a <= b) { return a; } return b; } int max(int a, int b) { if (a <= b) { return b; } return a; } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); int t; cin >> t; while (t) { int n, i; cin >> n; int ar[n]; for (i = 0; i < n; i++) { cin >> ar[i]; } sort(ar, ar + n); int r = 1000000; for (i = 1; i < n; i++) { if (ar[i] - ar[i - 1] <= r) { r = ar[i] - ar[i - 1]; } } cout << r << "\n"; t--; } return 0; }
### Prompt Your task is to create a CPP solution to the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int min(int a, int b) { if (a <= b) { return a; } return b; } int max(int a, int b) { if (a <= b) { return b; } return a; } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); int t; cin >> t; while (t) { int n, i; cin >> n; int ar[n]; for (i = 0; i < n; i++) { cin >> ar[i]; } sort(ar, ar + n); int r = 1000000; for (i = 1; i < n; i++) { if (ar[i] - ar[i - 1] <= r) { r = ar[i] - ar[i - 1]; } } cout << r << "\n"; t--; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { long long int t; cin >> t; while (t--) { long long int n; cin >> n; long long int arr[n]; for (long long int i = 0; i < n; i++) { cin >> arr[i]; } sort(arr, arr + n); long long int a = 1e9; for (long long int i = 0; i < n - 1; i++) { a = min(a, arr[i + 1] - arr[i]); } cout << a << endl; } }
### Prompt Please create a solution in cpp to the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { long long int t; cin >> t; while (t--) { long long int n; cin >> n; long long int arr[n]; for (long long int i = 0; i < n; i++) { cin >> arr[i]; } sort(arr, arr + n); long long int a = 1e9; for (long long int i = 0; i < n - 1; i++) { a = min(a, arr[i + 1] - arr[i]); } cout << a << endl; } } ```
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; int ara[n]; for (int i = 0; i < n; i++) { cin >> ara[i]; } int min = ara[0], c; for (int i = 0; i < n - 1; i++) { for (int j = i + 1; j < n; j++) { if (ara[j] < ara[i]) { int temp = ara[i]; ara[i] = ara[j]; ara[j] = temp; } } } c = ara[n - 1] - ara[n - 2]; for (int i = n - 1; i > 0; i--) { if (c > ara[i] - ara[i - 1]) c = ara[i] - ara[i - 1]; } cout << c << endl; } }
### Prompt Develop a solution in cpp to the problem described below: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; int ara[n]; for (int i = 0; i < n; i++) { cin >> ara[i]; } int min = ara[0], c; for (int i = 0; i < n - 1; i++) { for (int j = i + 1; j < n; j++) { if (ara[j] < ara[i]) { int temp = ara[i]; ara[i] = ara[j]; ara[j] = temp; } } } c = ara[n - 1] - ara[n - 2]; for (int i = n - 1; i > 0; i--) { if (c > ara[i] - ara[i - 1]) c = ara[i] - ara[i - 1]; } cout << c << endl; } } ```
#include <bits/stdc++.h> using namespace std; int n; int col[5005]; vector<int> adj[5005]; bool vis[5005]; void solve() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) cin >> a[i]; sort(a.begin(), a.end()); int mn = 1000000000; for (int i = 1; i < n; i++) { mn = min(mn, a[i] - a[i - 1]); } cout << mn << endl; } void querySolve() { int t; cin >> t; while (t--) { solve(); } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); querySolve(); }
### Prompt Generate a CPP solution to the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n; int col[5005]; vector<int> adj[5005]; bool vis[5005]; void solve() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) cin >> a[i]; sort(a.begin(), a.end()); int mn = 1000000000; for (int i = 1; i < n; i++) { mn = min(mn, a[i] - a[i - 1]); } cout << mn << endl; } void querySolve() { int t; cin >> t; while (t--) { solve(); } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); querySolve(); } ```
#include <bits/stdc++.h> using namespace std; int main() { long long int t; cin >> t; while (t--) { long long int n, x; cin >> n; vector<long long int> a; a.clear(); for (int i = 0; i < n; i++) { cin >> x; a.push_back(x); } sort(a.begin(), a.end()); long long int mx = 1e10; for (int i = 0; i < a.size() - 1; i++) { mx = min(abs(a[i + 1] - a[i]), mx); } cout << mx << endl; } return 0; }
### Prompt Please create a solution in cpp to the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { long long int t; cin >> t; while (t--) { long long int n, x; cin >> n; vector<long long int> a; a.clear(); for (int i = 0; i < n; i++) { cin >> x; a.push_back(x); } sort(a.begin(), a.end()); long long int mx = 1e10; for (int i = 0; i < a.size() - 1; i++) { mx = min(abs(a[i + 1] - a[i]), mx); } cout << mx << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int tests; cin >> tests; for (int test = 0; test < tests; ++test) { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; ++i) { cin >> a[i]; } sort(a.begin(), a.end()); int minn = 2000; for (int i = 0; i < n - 1; ++i) { if (a[i + 1] - a[i] < minn) { minn = a[i + 1] - a[i]; } } cout << minn << '\n'; } }
### Prompt Your task is to create a Cpp solution to the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int tests; cin >> tests; for (int test = 0; test < tests; ++test) { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; ++i) { cin >> a[i]; } sort(a.begin(), a.end()); int minn = 2000; for (int i = 0; i < n - 1; ++i) { if (a[i + 1] - a[i] < minn) { minn = a[i + 1] - a[i]; } } cout << minn << '\n'; } } ```
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; std::vector<int> v(n); for (int i = 0; i < n; i++) { cin >> v[i]; } int min = 100000; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (abs(v[i] - v[j]) < min) { min = abs(v[i] - v[j]); } } } cout << min << endl; } }
### Prompt Please formulate a Cpp solution to the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; std::vector<int> v(n); for (int i = 0; i < n; i++) { cin >> v[i]; } int min = 100000; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (abs(v[i] - v[j]) < min) { min = abs(v[i] - v[j]); } } } cout << min << endl; } } ```
#include <bits/stdc++.h> using namespace std; int main() { int test; cin >> test; for (int tt = 0; tt < test; tt++) { int n; cin >> n; vector<int> a(n); for (int &x : a) { cin >> x; } sort(a.begin(), a.end()); int result = a[n - 1] - a[0]; for (int i = 0; i < n - 1; i++) { result = min(result, a[i + 1] - a[i]); } cout << result << endl; } return 0; }
### Prompt Your challenge is to write a Cpp solution to the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int test; cin >> test; for (int tt = 0; tt < test; tt++) { int n; cin >> n; vector<int> a(n); for (int &x : a) { cin >> x; } sort(a.begin(), a.end()); int result = a[n - 1] - a[0]; for (int i = 0; i < n - 1; i++) { result = min(result, a[i + 1] - a[i]); } cout << result << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int tc, n; int minDiff; cin >> tc; while (tc--) { cin >> n; int arr[n]; for (int i = 0; i < n; i++) cin >> arr[i]; sort(arr, arr + n); minDiff = 100000; for (int i = 0; i < n - 1; i++) minDiff = min(minDiff, abs(arr[i] - arr[i + 1])); cout << minDiff << "\n"; } }
### Prompt In CPP, your task is to solve the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int tc, n; int minDiff; cin >> tc; while (tc--) { cin >> n; int arr[n]; for (int i = 0; i < n; i++) cin >> arr[i]; sort(arr, arr + n); minDiff = 100000; for (int i = 0; i < n - 1; i++) minDiff = min(minDiff, abs(arr[i] - arr[i + 1])); cout << minDiff << "\n"; } } ```
#include <bits/stdc++.h> using namespace std; int main() { int t, test; scanf("%d", &test); for (t = 0; t < test; t++) { int i, j, n, m, result, arr[50]; scanf("%d", &n); for (m = 0; m < n; m++) { scanf("%d", &arr[m]); } sort(arr, arr + n); i = INT_MAX; for (m = 0; m < n - 1; m++) { result = arr[m + 1] - arr[m]; i = (min(result, i)); } printf("%d\n", i); } return 0; }
### Prompt Your challenge is to write a Cpp solution to the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int t, test; scanf("%d", &test); for (t = 0; t < test; t++) { int i, j, n, m, result, arr[50]; scanf("%d", &n); for (m = 0; m < n; m++) { scanf("%d", &arr[m]); } sort(arr, arr + n); i = INT_MAX; for (m = 0; m < n - 1; m++) { result = arr[m + 1] - arr[m]; i = (min(result, i)); } printf("%d\n", i); } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { long long t, i; cin >> t; while (t--) { long long n, m = 100000, k, j; cin >> n; long long a[n + 5]; for (i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); for (i = 1; i < n; i++) { k = a[i] - a[i - 1]; m = min(k, m); } cout << m << endl; } return 0; }
### Prompt Please provide a Cpp coded solution to the problem described below: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { long long t, i; cin >> t; while (t--) { long long n, m = 100000, k, j; cin >> n; long long a[n + 5]; for (i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); for (i = 1; i < n; i++) { k = a[i] - a[i - 1]; m = min(k, m); } cout << m << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; void solve() { int n; cin >> n; int mas[n]; for (int i = 0; i < n; i++) cin >> mas[i]; sort(mas, mas + n); int min = 999999; int a = 0, b = 0; for (int i = 0; i < n - 1; i++) { if (mas[i + 1] - mas[i] < min) { min = mas[i + 1] - mas[i]; a = mas[i + 1]; b = mas[i]; } } cout << a - b << endl; return; } int main() { int t; cin >> t; for (int o = 0; o < t; o++) { solve(); } return 0; }
### Prompt Please create a solution in CPP to the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; void solve() { int n; cin >> n; int mas[n]; for (int i = 0; i < n; i++) cin >> mas[i]; sort(mas, mas + n); int min = 999999; int a = 0, b = 0; for (int i = 0; i < n - 1; i++) { if (mas[i + 1] - mas[i] < min) { min = mas[i + 1] - mas[i]; a = mas[i + 1]; b = mas[i]; } } cout << a - b << endl; return; } int main() { int t; cin >> t; for (int o = 0; o < t; o++) { solve(); } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int t, n, mini, i; cin >> t; while (t) { mini = INT_MAX; cin >> n; int a[n]; for (i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); for (i = 0; i < n - 1; i++) { mini = min(mini, (a[i + 1] - a[i])); } cout << mini << endl; t--; } return 0; }
### Prompt Please provide a Cpp coded solution to the problem described below: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int t, n, mini, i; cin >> t; while (t) { mini = INT_MAX; cin >> n; int a[n]; for (i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); for (i = 0; i < n - 1; i++) { mini = min(mini, (a[i + 1] - a[i])); } cout << mini << endl; t--; } return 0; } ```
#include <bits/stdc++.h> #pragma GCC optimize("O0") #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #pragma GCC optimize("-ffloat-store") using namespace std; long long n, m, a, b, c, k, temp, x, y; const int MAXN = 100000 + 11; inline void read(vector<long long> &v); inline long long max(long long a, long long b); inline long long min(long long a, long long b); inline long long gcd(long long a, long long b); void solveforthiscase(const int &test) { cin >> n; vector<long long> v(n); read(v); sort(v.begin(), v.end()); long long mi = 1000000000000000000; for (int i = 0; i < n - 1; i++) mi = min(mi, v[i + 1] - v[i]); cout << mi << '\n'; return; } int main() { ios_base::sync_with_stdio(false); cout << fixed << setprecision(10); cin.tie(NULL); cout.tie(NULL); clock_t start, end; start = clock(); int test = 1; cin >> test; for (int i = 1; i <= test; i++) { solveforthiscase(i); } end = clock(); long double time_taken = (long double)(end - start) / (long double)(CLOCKS_PER_SEC); cerr << "Time taken by program is : " << (long double)time_taken * 1000 << " milisec "; } inline long long max(long long a, long long b) { return ((a > b) ? a : b); } inline long long min(long long a, long long b) { return ((a > b) ? b : a); } inline long long gcd(long long a, long long b) { if (b == 0) return a; a %= b; return gcd(b, a); } inline void read(vector<long long> &v) { for (int i = 0; i < v.size(); i++) cin >> v[i]; }
### Prompt Generate a Cpp solution to the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> #pragma GCC optimize("O0") #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #pragma GCC optimize("-ffloat-store") using namespace std; long long n, m, a, b, c, k, temp, x, y; const int MAXN = 100000 + 11; inline void read(vector<long long> &v); inline long long max(long long a, long long b); inline long long min(long long a, long long b); inline long long gcd(long long a, long long b); void solveforthiscase(const int &test) { cin >> n; vector<long long> v(n); read(v); sort(v.begin(), v.end()); long long mi = 1000000000000000000; for (int i = 0; i < n - 1; i++) mi = min(mi, v[i + 1] - v[i]); cout << mi << '\n'; return; } int main() { ios_base::sync_with_stdio(false); cout << fixed << setprecision(10); cin.tie(NULL); cout.tie(NULL); clock_t start, end; start = clock(); int test = 1; cin >> test; for (int i = 1; i <= test; i++) { solveforthiscase(i); } end = clock(); long double time_taken = (long double)(end - start) / (long double)(CLOCKS_PER_SEC); cerr << "Time taken by program is : " << (long double)time_taken * 1000 << " milisec "; } inline long long max(long long a, long long b) { return ((a > b) ? a : b); } inline long long min(long long a, long long b) { return ((a > b) ? b : a); } inline long long gcd(long long a, long long b) { if (b == 0) return a; a %= b; return gcd(b, a); } inline void read(vector<long long> &v) { for (int i = 0; i < v.size(); i++) cin >> v[i]; } ```
#include <bits/stdc++.h> using namespace std; int main() { int t; scanf("%d", &t); int n; int i; for (i = 0; i < t; i++) { scanf("%d", &n); int arr[n]; int min = 1005; for (int j = 0; j < n; j++) { scanf("%d", &arr[j]); } sort(arr, arr + n); for (int k = 0; k < n - 1; k++) { if (abs(arr[k] - arr[k + 1]) < min) min = abs(arr[k] - arr[k + 1]); } printf("%d\n", min); } return 0; }
### Prompt Please formulate a cpp solution to the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int t; scanf("%d", &t); int n; int i; for (i = 0; i < t; i++) { scanf("%d", &n); int arr[n]; int min = 1005; for (int j = 0; j < n; j++) { scanf("%d", &arr[j]); } sort(arr, arr + n); for (int k = 0; k < n - 1; k++) { if (abs(arr[k] - arr[k + 1]) < min) min = abs(arr[k] - arr[k + 1]); } printf("%d\n", min); } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main(void) { int t, n; cin >> t; while (t--) { cin >> n; vector<int> v(n); map<int, int> mp; bool flag = false; for (auto &x : v) { cin >> x; mp[x]++; if (mp[x] >= 2) { flag = true; } } if (flag) { cout << 0 << endl; continue; } sort(v.begin(), v.end()); int ans = 1010; for (int i = 1; i < n; i++) { ans = min(ans, abs(v[i] - v[i - 1])); } cout << ans << endl; } return 0; }
### Prompt Generate a cpp solution to the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main(void) { int t, n; cin >> t; while (t--) { cin >> n; vector<int> v(n); map<int, int> mp; bool flag = false; for (auto &x : v) { cin >> x; mp[x]++; if (mp[x] >= 2) { flag = true; } } if (flag) { cout << 0 << endl; continue; } sort(v.begin(), v.end()); int ans = 1010; for (int i = 1; i < n; i++) { ans = min(ans, abs(v[i] - v[i - 1])); } cout << ans << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main(void) { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t; cin >> t; while (t--) { int n; cin >> n; vector<int> s(n); for (int i = 0; i < n; i++) cin >> s[i]; sort(s.begin(), s.end()); int mi = INT_MAX; for (int i = 1; i < s.size(); i++) { mi = min(mi, s[i] - s[i - 1]); } cout << mi << "\n"; } return 0; }
### Prompt Your challenge is to write a CPP solution to the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main(void) { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t; cin >> t; while (t--) { int n; cin >> n; vector<int> s(n); for (int i = 0; i < n; i++) cin >> s[i]; sort(s.begin(), s.end()); int mi = INT_MAX; for (int i = 1; i < s.size(); i++) { mi = min(mi, s[i] - s[i - 1]); } cout << mi << "\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int n; int a[55]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while (t--) { cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a, a + n); int result = INT_MAX; for (int i = 0; i < n - 1; i++) { result = min(result, abs(a[i] - a[i + 1])); } cout << result << endl; } return 0; }
### Prompt In Cpp, your task is to solve the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n; int a[55]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while (t--) { cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a, a + n); int result = INT_MAX; for (int i = 0; i < n - 1; i++) { result = min(result, abs(a[i] - a[i + 1])); } cout << result << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; void getans() { int x = INT_MAX, n; cin >> n; vector<int> v(n); for (int i = 0; i < n; i++) cin >> v[i]; sort(v.begin(), v.end()); for (int i = 1; i < n; i++) { x = min(v[i] - v[i - 1], x); } cout << x << endl; } int main() { std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int c = 1; int t; cin >> t; while (t--) { getans(); c++; } }
### Prompt Create a solution in CPP for the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; void getans() { int x = INT_MAX, n; cin >> n; vector<int> v(n); for (int i = 0; i < n; i++) cin >> v[i]; sort(v.begin(), v.end()); for (int i = 1; i < n; i++) { x = min(v[i] - v[i - 1], x); } cout << x << endl; } int main() { std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int c = 1; int t; cin >> t; while (t--) { getans(); c++; } } ```
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n, dif = 1001; cin >> n; vector<int> s(n); for (int i = 0; i < n; i++) cin >> s[i]; sort(s.begin(), s.end()); for (int i = 0; i < n - 1; i++) dif = min(dif, s[i + 1] - s[i]); cout << dif << endl; } return 0; }
### Prompt Develop a solution in cpp to the problem described below: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n, dif = 1001; cin >> n; vector<int> s(n); for (int i = 0; i < n; i++) cin >> s[i]; sort(s.begin(), s.end()); for (int i = 0; i < n - 1; i++) dif = min(dif, s[i + 1] - s[i]); cout << dif << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int t; scanf("%d", &t); while (t--) { int n; scanf("%d", &n); vector<int> nums(n); for (auto &x : nums) { scanf("%d", &x); } sort(nums.begin(), nums.end()); int result = 0x3f3f3f3f; for (int i = 1; i < n; i++) { result = min(result, abs(nums[i] - nums[i - 1])); } printf("%d\n", result); } }
### Prompt Please create a solution in Cpp to the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int t; scanf("%d", &t); while (t--) { int n; scanf("%d", &n); vector<int> nums(n); for (auto &x : nums) { scanf("%d", &x); } sort(nums.begin(), nums.end()); int result = 0x3f3f3f3f; for (int i = 1; i < n; i++) { result = min(result, abs(nums[i] - nums[i - 1])); } printf("%d\n", result); } } ```
#include <bits/stdc++.h> using namespace std; int x[53]; int main(int argc, char** argv) { int t, n, min; cin >> t; for (int i = 0; i < t; i++) { cin >> n; for (int j = 0; j < n; j++) { cin >> x[j]; } sort(x, x + n); min = x[1] - x[0]; for (int j = 0; j < n - 1; j++) { if (x[j + 1] - x[j] < min) { min = x[j + 1] - x[j]; } } cout << min << endl; } return 0; }
### Prompt In CPP, your task is to solve the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int x[53]; int main(int argc, char** argv) { int t, n, min; cin >> t; for (int i = 0; i < t; i++) { cin >> n; for (int j = 0; j < n; j++) { cin >> x[j]; } sort(x, x + n); min = x[1] - x[0]; for (int j = 0; j < n - 1; j++) { if (x[j + 1] - x[j] < min) { min = x[j + 1] - x[j]; } } cout << min << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long t; cin >> t; while (t--) { long long n; cin >> n; long long arr[n]; for (long long i = 0; i < n; ++i) { cin >> arr[i]; } sort(arr, arr + n); long long ans = 1000000000; for (long long i = 0; i < n - 1; ++i) { ans = min(ans, arr[i + 1] - arr[i]); } cout << ans << endl; } return 0; }
### Prompt Create a solution in CPP for the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long t; cin >> t; while (t--) { long long n; cin >> n; long long arr[n]; for (long long i = 0; i < n; ++i) { cin >> arr[i]; } sort(arr, arr + n); long long ans = 1000000000; for (long long i = 0; i < n - 1; ++i) { ans = min(ans, arr[i + 1] - arr[i]); } cout << ans << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; int t; cin >> t; while (t--) { int n, flag = 0; cin >> n; int arr[n]; for (int i = 0; i < n; i++) cin >> arr[i]; sort(arr, arr + n); int min = arr[1] - arr[0]; for (int i = 1; i < n; i++) { if (arr[i] == arr[i - 1]) { flag = 1; break; } if (arr[i] - arr[i - 1] < min) min = arr[i] - arr[i - 1]; } if (flag == 1) cout << "0" << '\n'; else cout << min << '\n'; } }
### Prompt Construct a Cpp code solution to the problem outlined: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; int t; cin >> t; while (t--) { int n, flag = 0; cin >> n; int arr[n]; for (int i = 0; i < n; i++) cin >> arr[i]; sort(arr, arr + n); int min = arr[1] - arr[0]; for (int i = 1; i < n; i++) { if (arr[i] == arr[i - 1]) { flag = 1; break; } if (arr[i] - arr[i - 1] < min) min = arr[i] - arr[i - 1]; } if (flag == 1) cout << "0" << '\n'; else cout << min << '\n'; } } ```
#include <bits/stdc++.h> using namespace std; const int MOD = (int)1e9 + 7; const int MAX = 1e6; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { int n, ans = INT_MAX; cin >> n; int arr[n + 5]; for (int i = 0; i < n; i++) cin >> arr[i]; sort(arr, arr + n); for (int i = 1; i < n; i++) { if (arr[i] - arr[i - 1] < ans) ans = arr[i] - arr[i - 1]; } cout << ans << endl; } return 0; }
### Prompt Please formulate a Cpp solution to the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MOD = (int)1e9 + 7; const int MAX = 1e6; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { int n, ans = INT_MAX; cin >> n; int arr[n + 5]; for (int i = 0; i < n; i++) cin >> arr[i]; sort(arr, arr + n); for (int i = 1; i < n; i++) { if (arr[i] - arr[i - 1] < ans) ans = arr[i] - arr[i - 1]; } cout << ans << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; void test() { int mi = INT_MAX; int s; cin >> s; vector<int> v(s); while (s) cin >> v.at(v.size() - s--); sort(v.begin(), v.end()); for (int i = 0; i < v.size() - 1; ++i) { mi = min(abs(v.at(i) - v.at(i + 1)), mi); } cout << mi << endl; } int main() { int t{}; cin >> t; while (t--) test(); return 0; }
### Prompt Develop a solution in Cpp to the problem described below: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; void test() { int mi = INT_MAX; int s; cin >> s; vector<int> v(s); while (s) cin >> v.at(v.size() - s--); sort(v.begin(), v.end()); for (int i = 0; i < v.size() - 1; ++i) { mi = min(abs(v.at(i) - v.at(i + 1)), mi); } cout << mi << endl; } int main() { int t{}; cin >> t; while (t--) test(); return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int T; cin >> T; while (T--) { int n; cin >> n; set<int> st; int t; for (int i = 0; i < n; i++) { cin >> t; st.insert(t); } if (st.size() < n) cout << 0 << "\n"; else { vector<int> s; for (auto x : st) s.push_back(x); s.resize(s.size()); int ans = s[n - 1] - s[0]; for (int i = 1; i < n; i++) ans = min(ans, s[i] - s[i - 1]); cout << ans << "\n"; } } return 0; }
### Prompt Please formulate a cpp solution to the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int T; cin >> T; while (T--) { int n; cin >> n; set<int> st; int t; for (int i = 0; i < n; i++) { cin >> t; st.insert(t); } if (st.size() < n) cout << 0 << "\n"; else { vector<int> s; for (auto x : st) s.push_back(x); s.resize(s.size()); int ans = s[n - 1] - s[0]; for (int i = 1; i < n; i++) ans = min(ans, s[i] - s[i - 1]); cout << ans << "\n"; } } return 0; } ```
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; const int N = 1e5 + 5; const int MOD = 1e9 + 7; int main() { int t; cin >> t; while (t--) { int n; cin >> n; std::vector<int> v(n); for (int i = 0; i < n; ++i) scanf("%d", &v[i]); sort(v.begin(), v.end()); int ans = v[1] - v[0]; for (int i = 2; i < n; i++) ans = min(ans, v[i] - v[i - 1]); cout << ans << endl; } return 0; }
### Prompt Construct a Cpp code solution to the problem outlined: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; const int N = 1e5 + 5; const int MOD = 1e9 + 7; int main() { int t; cin >> t; while (t--) { int n; cin >> n; std::vector<int> v(n); for (int i = 0; i < n; ++i) scanf("%d", &v[i]); sort(v.begin(), v.end()); int ans = v[1] - v[0]; for (int i = 2; i < n; i++) ans = min(ans, v[i] - v[i - 1]); cout << ans << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const unsigned int MOD = 1e9 + 7; vector<bool> prime(1000001, true); void seive(int n) { for (int i = 2; i * i <= n; i++) { if (prime[i] == true) { for (int p = i * i; p <= n; p += i) { prime[p] = false; } } } } void solve() { int N; cin >> N; int ans = INT_MAX; vector<int> v(N); for (int &x : v) cin >> x; for (int i = 0; i < N - 1; i++) { for (int j = i + 1; j < N; j++) { ans = min(ans, abs(v[i] - v[j])); } } cout << ans << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while (t--) { solve(); } return 0; }
### Prompt Develop a solution in cpp to the problem described below: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const unsigned int MOD = 1e9 + 7; vector<bool> prime(1000001, true); void seive(int n) { for (int i = 2; i * i <= n; i++) { if (prime[i] == true) { for (int p = i * i; p <= n; p += i) { prime[p] = false; } } } } void solve() { int N; cin >> N; int ans = INT_MAX; vector<int> v(N); for (int &x : v) cin >> x; for (int i = 0; i < N - 1; i++) { for (int j = i + 1; j < N; j++) { ans = min(ans, abs(v[i] - v[j])); } } cout << ans << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while (t--) { solve(); } return 0; } ```
#include <bits/stdc++.h> using namespace std; using ll = long long; const long long inf = 1e9; ll n, m, k; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { cin >> n; vector<int> v(n); for (auto &x : v) cin >> x; sort(v.begin(), v.end()); int ans = 1000; for (int i = 1; i < n; i++) { ans = min(ans, v[i] - v[i - 1]); } cout << ans << "\n"; } return 0; }
### Prompt Generate a cpp solution to the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; using ll = long long; const long long inf = 1e9; ll n, m, k; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { cin >> n; vector<int> v(n); for (auto &x : v) cin >> x; sort(v.begin(), v.end()); int ans = 1000; for (int i = 1; i < n; i++) { ans = min(ans, v[i] - v[i - 1]); } cout << ans << "\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; for (int a = 0; a < t; a++) { int n; cin >> n; int arr[n]; int diff = INT_MAX; for (int i = 0; i < n; i++) { cin >> arr[i]; } sort(arr, arr + n); for (int x = 1; x < n; x++) { if (arr[x] - arr[x - 1] < diff) diff = arr[x] - arr[x - 1]; } cout << diff << "\n"; } return 0; }
### Prompt Please create a solution in cpp to the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; for (int a = 0; a < t; a++) { int n; cin >> n; int arr[n]; int diff = INT_MAX; for (int i = 0; i < n; i++) { cin >> arr[i]; } sort(arr, arr + n); for (int x = 1; x < n; x++) { if (arr[x] - arr[x - 1] < diff) diff = arr[x] - arr[x - 1]; } cout << diff << "\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long t; cin >> t; while (t--) { long long n; cin >> n; long long a[n]; for (long long i = 0; i < n; ++i) { cin >> a[i]; } long long minDiff = 0; sort(a, a + n); minDiff = abs(a[0] - a[1]); for (long long i = 1; i - 1 < n; i++) { if (abs(a[i + 1] - a[i]) < minDiff) minDiff = a[i + 1] - a[i]; } cout << minDiff << "\n"; } return 0; }
### Prompt Develop a solution in Cpp to the problem described below: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long t; cin >> t; while (t--) { long long n; cin >> n; long long a[n]; for (long long i = 0; i < n; ++i) { cin >> a[i]; } long long minDiff = 0; sort(a, a + n); minDiff = abs(a[0] - a[1]); for (long long i = 1; i - 1 < n; i++) { if (abs(a[i + 1] - a[i]) < minDiff) minDiff = a[i + 1] - a[i]; } cout << minDiff << "\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { long long t; cin >> t; while (t--) { long long n; cin >> n; long long a[n + 1]; for (long long i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); long long diff = INT_MAX; for (long long i = 0; i < n - 1; i++) { diff = min(a[i + 1] - a[i], diff); } cout << diff << endl; } }
### Prompt Develop a solution in Cpp to the problem described below: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { long long t; cin >> t; while (t--) { long long n; cin >> n; long long a[n + 1]; for (long long i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); long long diff = INT_MAX; for (long long i = 0; i < n - 1; i++) { diff = min(a[i + 1] - a[i], diff); } cout << diff << endl; } } ```
#include <bits/stdc++.h> using namespace std; int main() { int no; cin >> no; while (no--) { int size; cin >> size; int arr[size]; int i = 0; while (i < size) { cin >> arr[i++]; } sort(arr, arr + size); int least = arr[size - 1] - arr[size - 2], temp; for (i = 0; i < size - 2; i++) { temp = arr[i + 1] - arr[i]; if (temp <= least) least = temp; } cout << least << '\n'; } }
### Prompt Please formulate a Cpp solution to the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int no; cin >> no; while (no--) { int size; cin >> size; int arr[size]; int i = 0; while (i < size) { cin >> arr[i++]; } sort(arr, arr + size); int least = arr[size - 1] - arr[size - 2], temp; for (i = 0; i < size - 2; i++) { temp = arr[i + 1] - arr[i]; if (temp <= least) least = temp; } cout << least << '\n'; } } ```
#include <bits/stdc++.h> using namespace std; int main() { int n, t; cin >> t; for (int i = 0; i < t; i++) { cin >> n; int a[50]; for (int i = 0; i < n; i++) cin >> a[i]; int res = 1000; for (int i = 0; i < n - 1; i++) for (int j = i + 1; j < n; j++) if (abs(a[i] - a[j]) < res) res = abs(a[i] - a[j]); cout << res << endl; } }
### Prompt Please create a solution in Cpp to the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n, t; cin >> t; for (int i = 0; i < t; i++) { cin >> n; int a[50]; for (int i = 0; i < n; i++) cin >> a[i]; int res = 1000; for (int i = 0; i < n - 1; i++) for (int j = i + 1; j < n; j++) if (abs(a[i] - a[j]) < res) res = abs(a[i] - a[j]); cout << res << endl; } } ```
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize("-O3") const long double PI = 3.141592653589793; bool isPowerOfTwo(int x) { return x && (!(x & (x - 1))); } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long int t = 0; cin >> t; while (t--) { long long int n, i, pos1, pos2, pos3; cin >> n; vector<long long int> v(n); for (i = 0; i < n; i++) cin >> v[i]; sort((v).begin(), (v).end()); if (n == 2) cout << abs(v[1] - v[0]); else if (n > 2) { vector<long long int> x; for (i = 0; i < n - 1; i++) x.push_back(abs(v[i] - v[i + 1])); cout << *min_element((x).begin(), (x).end()); } cout << "\n"; } return 0; }
### Prompt Please create a solution in CPP to the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; #pragma GCC optimize("-O3") const long double PI = 3.141592653589793; bool isPowerOfTwo(int x) { return x && (!(x & (x - 1))); } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long int t = 0; cin >> t; while (t--) { long long int n, i, pos1, pos2, pos3; cin >> n; vector<long long int> v(n); for (i = 0; i < n; i++) cin >> v[i]; sort((v).begin(), (v).end()); if (n == 2) cout << abs(v[1] - v[0]); else if (n > 2) { vector<long long int> x; for (i = 0; i < n - 1; i++) x.push_back(abs(v[i] - v[i + 1])); cout << *min_element((x).begin(), (x).end()); } cout << "\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t, n; cin >> t; while (t--) { cin >> n; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); int lowdif = INT_MAX; for (int i = 1; i < n; i++) { if (a[i] - a[i - 1] < lowdif) lowdif = a[i] - a[i - 1]; } cout << lowdif << "\n"; } return 0; }
### Prompt Please formulate a Cpp solution to the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t, n; cin >> t; while (t--) { cin >> n; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); int lowdif = INT_MAX; for (int i = 1; i < n; i++) { if (a[i] - a[i - 1] < lowdif) lowdif = a[i] - a[i - 1]; } cout << lowdif << "\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int t, n, a[100]; cin >> t; while (t--) { cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a, a + n); vector<int> v; int dif = 0; for (int i = 1; i < n; i++) { dif = a[i] - a[i - 1]; v.push_back(dif); } sort(v.begin(), v.end()); cout << v[0] << endl; } return 0; }
### Prompt Your challenge is to write a CPP solution to the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int t, n, a[100]; cin >> t; while (t--) { cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a, a + n); vector<int> v; int dif = 0; for (int i = 1; i < n; i++) { dif = a[i] - a[i - 1]; v.push_back(dif); } sort(v.begin(), v.end()); cout << v[0] << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; bool com(long long a, long long b) { return a < b; } int main() { long long t; cin >> t; while (t--) { long long n, d, ans = 1000; cin >> n; long long a[n]; for (long long i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); for (long long i = 1; i < n; i++) { d = a[i] - a[i - 1]; ans = min(ans, d); } cout << ans << "\n"; } }
### Prompt Develop a solution in cpp to the problem described below: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; bool com(long long a, long long b) { return a < b; } int main() { long long t; cin >> t; while (t--) { long long n, d, ans = 1000; cin >> n; long long a[n]; for (long long i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); for (long long i = 1; i < n; i++) { d = a[i] - a[i - 1]; ans = min(ans, d); } cout << ans << "\n"; } } ```
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; using ll = long long; using P = pair<ll, ll>; template <class T> using V = vector<T>; const ll inf = (1e18); const ll mod = 1000000007; ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; } ll LCM(ll c, ll d) { return c / GCD(c, d) * d; } struct __INIT { __INIT() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(15); } } __init; template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; } void pitsu() { int n; cin >> n; V<ll> a(n); for (int i = 0; i < n; i++) cin >> a[i]; sort((a).begin(), (a).end()); ll ans = inf; for (int i = 0; i < n - 1; i++) chmin(ans, a[i + 1] - a[i]); cout << ans << "\n"; } int main() { int t; cin >> t; while (t--) pitsu(); }
### Prompt Your task is to create a Cpp solution to the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; using ll = long long; using P = pair<ll, ll>; template <class T> using V = vector<T>; const ll inf = (1e18); const ll mod = 1000000007; ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; } ll LCM(ll c, ll d) { return c / GCD(c, d) * d; } struct __INIT { __INIT() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(15); } } __init; template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; } void pitsu() { int n; cin >> n; V<ll> a(n); for (int i = 0; i < n; i++) cin >> a[i]; sort((a).begin(), (a).end()); ll ans = inf; for (int i = 0; i < n - 1; i++) chmin(ans, a[i + 1] - a[i]); cout << ans << "\n"; } int main() { int t; cin >> t; while (t--) pitsu(); } ```
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; vector<int> v(n); for (int i = 0; i < n; i++) { cin >> v[i]; } sort(v.begin(), v.end()); int result = v[n - 1] - v[0]; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { result = min(result, v[j] - v[i]); } } cout << result << endl; } }
### Prompt Your challenge is to write a cpp solution to the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; vector<int> v(n); for (int i = 0; i < n; i++) { cin >> v[i]; } sort(v.begin(), v.end()); int result = v[n - 1] - v[0]; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { result = min(result, v[j] - v[i]); } } cout << result << endl; } } ```
#include <bits/stdc++.h> using namespace std; signed main() { ios_base::sync_with_stdio(false); cin.tie(0); long long t; cin >> t; for (long long curT = 0; curT < t; ++curT) { long long n; cin >> n; vector<long long> in(n); for (long long i = 0; i < n; ++i) { cin >> in[i]; } sort(in.begin(), in.end()); long long minn = 1e18; for (long long i = 1; i < n; ++i) { minn = min(minn, in[i] - in[i - 1]); } cout << minn << "\n"; } }
### Prompt Generate a Cpp solution to the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; signed main() { ios_base::sync_with_stdio(false); cin.tie(0); long long t; cin >> t; for (long long curT = 0; curT < t; ++curT) { long long n; cin >> n; vector<long long> in(n); for (long long i = 0; i < n; ++i) { cin >> in[i]; } sort(in.begin(), in.end()); long long minn = 1e18; for (long long i = 1; i < n; ++i) { minn = min(minn, in[i] - in[i - 1]); } cout << minn << "\n"; } } ```
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t; cin >> t; while (t--) { int n; cin >> n; int arr[n]; for (int i = 0; i < n; ++i) cin >> arr[i]; sort(arr, arr + n); int mini = INT_MAX, diff = 0; for (int i = 1; i < n; ++i) { diff = arr[i] - arr[i - 1]; mini = min(mini, diff); } cout << mini << '\n'; } }
### Prompt In CPP, your task is to solve the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t; cin >> t; while (t--) { int n; cin >> n; int arr[n]; for (int i = 0; i < n; ++i) cin >> arr[i]; sort(arr, arr + n); int mini = INT_MAX, diff = 0; for (int i = 1; i < n; ++i) { diff = arr[i] - arr[i - 1]; mini = min(mini, diff); } cout << mini << '\n'; } } ```
#include <bits/stdc++.h> using namespace std; int main() { int t, n, i, j, x, y, ans, k; int ar[100]; scanf("%d", &t); while (t--) { scanf("%d", &n); ans = 99999999; for (i = 1; i <= n; i++) { scanf("%d", &ar[i]); } sort(ar + 1, ar + n + 1); for (i = 1; i < n; i++) { ans = min(ans, ar[i + 1] - ar[i]); } printf("%d\n", ans); } return 0; }
### Prompt Construct a Cpp code solution to the problem outlined: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int t, n, i, j, x, y, ans, k; int ar[100]; scanf("%d", &t); while (t--) { scanf("%d", &n); ans = 99999999; for (i = 1; i <= n; i++) { scanf("%d", &ar[i]); } sort(ar + 1, ar + n + 1); for (i = 1; i < n; i++) { ans = min(ans, ar[i + 1] - ar[i]); } printf("%d\n", ans); } return 0; } ```
#include <bits/stdc++.h> using namespace std; int tc, n, a[1000]; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); ; cin >> tc; while (tc--) { cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); int ans = 1e7; for (int i = 0; i < n; i++) { int t = 0; for (int j = i; j < n; j++) { if (i == j) continue; t = abs(a[i] - a[j]); ans = min(ans, t); } } cout << ans << '\n'; } return 0; }
### Prompt Construct a cpp code solution to the problem outlined: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int tc, n, a[1000]; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); ; cin >> tc; while (tc--) { cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); int ans = 1e7; for (int i = 0; i < n; i++) { int t = 0; for (int j = i; j < n; j++) { if (i == j) continue; t = abs(a[i] - a[j]); ans = min(ans, t); } } cout << ans << '\n'; } return 0; } ```
#include <bits/stdc++.h> using namespace std; long long t, n, a[100]; int main() { cin >> t; while (t--) { cin >> n; for (int i = 0; i < n; ++i) cin >> a[i]; sort(a, a + n); long long ans = 1e9; for (int i = 1; i < n; ++i) { ans = min(a[i] - a[i - 1], ans); } cout << ans << endl; } }
### Prompt Please provide a cpp coded solution to the problem described below: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long t, n, a[100]; int main() { cin >> t; while (t--) { cin >> n; for (int i = 0; i < n; ++i) cin >> a[i]; sort(a, a + n); long long ans = 1e9; for (int i = 1; i < n; ++i) { ans = min(a[i] - a[i - 1], ans); } cout << ans << endl; } } ```
#include <bits/stdc++.h> using namespace std; void solve() { int n; cin >> n; vector<int> athletes(n); for (auto &athlete : athletes) cin >> athlete; sort(athletes.begin(), athletes.end()); int diff = INT_MAX; for (int i = 1; i < n; ++i) { diff = min(diff, athletes[i] - athletes[i - 1]); } cout << diff << endl; } int main() { int numTests = 1; cin >> numTests; while (numTests--) solve(); }
### Prompt Please provide a CPP coded solution to the problem described below: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; void solve() { int n; cin >> n; vector<int> athletes(n); for (auto &athlete : athletes) cin >> athlete; sort(athletes.begin(), athletes.end()); int diff = INT_MAX; for (int i = 1; i < n; ++i) { diff = min(diff, athletes[i] - athletes[i - 1]); } cout << diff << endl; } int main() { int numTests = 1; cin >> numTests; while (numTests--) solve(); } ```
#include <bits/stdc++.h> using namespace std; int main() { int t; scanf("%d", &t); while (t--) { int n; scanf("%d", &n); vector<int> nums(n); for (int i = 0; i < n; i++) { scanf("%d", &nums[i]); } sort(nums.begin(), nums.end()); int result = 0x3f3f3f3f; for (int i = 1; i < n; i++) { result = min(result, abs(nums[i] - nums[i - 1])); } printf("%d\n", result); } }
### Prompt Construct a cpp code solution to the problem outlined: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int t; scanf("%d", &t); while (t--) { int n; scanf("%d", &n); vector<int> nums(n); for (int i = 0; i < n; i++) { scanf("%d", &nums[i]); } sort(nums.begin(), nums.end()); int result = 0x3f3f3f3f; for (int i = 1; i < n; i++) { result = min(result, abs(nums[i] - nums[i - 1])); } printf("%d\n", result); } } ```
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; vector<int> strength(n, 0); for (int i = 0; i < n; i++) { cin >> strength[i]; } sort(strength.begin(), strength.end()); int mindeff = 10001; for (int i = 0; i < n - 1; i++) { if (strength[i + 1] - strength[i] < mindeff) { mindeff = strength[i + 1] - strength[i]; } } cout << mindeff << endl; } return 0; }
### Prompt Develop a solution in Cpp to the problem described below: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; vector<int> strength(n, 0); for (int i = 0; i < n; i++) { cin >> strength[i]; } sort(strength.begin(), strength.end()); int mindeff = 10001; for (int i = 0; i < n - 1; i++) { if (strength[i + 1] - strength[i] < mindeff) { mindeff = strength[i + 1] - strength[i]; } } cout << mindeff << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int t, n; cin >> t; while (t--) { cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a.begin(), a.end()); int ans = INT_MAX; for (int i = 1; i < n; i++) { ans = min(ans, a[i] - a[i - 1]); } cout << ans << '\n'; } return 0; }
### Prompt Your challenge is to write a CPP solution to the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int t, n; cin >> t; while (t--) { cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a.begin(), a.end()); int ans = INT_MAX; for (int i = 1; i < n; i++) { ans = min(ans, a[i] - a[i - 1]); } cout << ans << '\n'; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { long long int tc; scanf("%lld", &tc); while (tc--) { long long int n; scanf("%lld", &n); vector<long long int> v; for (long long int i = 0; i < n; i++) { long long int a; scanf("%lld", &a); v.push_back(a); } sort(v.begin(), v.end()); long long int mx = 1e9; for (long long int i = 1; i < n; i++) { long long int ans = v[i] - v[i - 1]; mx = min(mx, ans); } printf("%lld\n", mx); } }
### Prompt Please provide a CPP coded solution to the problem described below: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { long long int tc; scanf("%lld", &tc); while (tc--) { long long int n; scanf("%lld", &n); vector<long long int> v; for (long long int i = 0; i < n; i++) { long long int a; scanf("%lld", &a); v.push_back(a); } sort(v.begin(), v.end()); long long int mx = 1e9; for (long long int i = 1; i < n; i++) { long long int ans = v[i] - v[i - 1]; mx = min(mx, ans); } printf("%lld\n", mx); } } ```
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; for (int i = 0; i < t; i++) { int n; cin >> n; int arr[n]; for (int j = 0; j < n; j++) { cin >> arr[j]; } sort(arr, arr + n); int minn = 1000; for (int j = 0; j < n - 1; j++) { if (minn > (arr[j + 1] - arr[j])) { minn = (arr[j + 1] - arr[j]); } } cout << minn << endl; } return 0; }
### Prompt Construct a Cpp code solution to the problem outlined: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; for (int i = 0; i < t; i++) { int n; cin >> n; int arr[n]; for (int j = 0; j < n; j++) { cin >> arr[j]; } sort(arr, arr + n); int minn = 1000; for (int j = 0; j < n - 1; j++) { if (minn > (arr[j + 1] - arr[j])) { minn = (arr[j + 1] - arr[j]); } } cout << minn << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n, d = 1001; cin >> n; int s[n]; for (int i = 0; i < n; i++) { cin >> s[i]; } sort(s, s + n); int result = s[n - 1] - s[0]; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { result = min(result, s[j] - s[i]); } } cout << result << endl; } return 0; }
### Prompt Construct a CPP code solution to the problem outlined: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n, d = 1001; cin >> n; int s[n]; for (int i = 0; i < n; i++) { cin >> s[i]; } sort(s, s + n); int result = s[n - 1] - s[0]; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { result = min(result, s[j] - s[i]); } } cout << result << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int t, n; cin >> t; while (t--) { cin >> n; int arr[n]; map<int, int> maps; for (int i = 0; i < n; i++) { cin >> arr[i]; maps[arr[i]]++; } int flag = 0; for (auto i : maps) { if (i.second > 1) { cout << 0 << endl; flag = 1; break; } } if (flag) { continue; } sort(arr, arr + n); int min_in = INT_MAX, k2 = INT_MAX; for (int i = 1; i < n; i += 2) { int k1 = arr[i] - arr[i - 1]; if (i != n - 1) { k2 = arr[i + 1] - arr[i]; } min_in = min(min_in, min(k1, k2)); } cout << min_in << endl; } }
### Prompt Please provide a CPP coded solution to the problem described below: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int t, n; cin >> t; while (t--) { cin >> n; int arr[n]; map<int, int> maps; for (int i = 0; i < n; i++) { cin >> arr[i]; maps[arr[i]]++; } int flag = 0; for (auto i : maps) { if (i.second > 1) { cout << 0 << endl; flag = 1; break; } } if (flag) { continue; } sort(arr, arr + n); int min_in = INT_MAX, k2 = INT_MAX; for (int i = 1; i < n; i += 2) { int k1 = arr[i] - arr[i - 1]; if (i != n - 1) { k2 = arr[i + 1] - arr[i]; } min_in = min(min_in, min(k1, k2)); } cout << min_in << endl; } } ```
#include <bits/stdc++.h> using namespace std; int i = 0, j = 0; long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } bool isPowerOfTwo(long long n) { return n && (!(n & (n - 1))); } bool is_numeric(char c) { return c >= 'a' and c <= 'z'; } string decToBinary(int n) { string binaryNum; int i = 0; while (n > 0) { binaryNum.push_back(n % 2); n = n / 2; i++; } return binaryNum; } void solve() { int n; cin >> n; vector<int> v(n); for (i = 0; i < n; ++i) { cin >> v[i]; } sort(v.begin(), v.end()); int mini = INT_MAX; for (i = 1; i < n; ++i) { mini = ((mini) < (v[i] - v[i - 1]) ? (mini) : (v[i] - v[i - 1])); } cout << mini << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ; int test; cin >> test; while (test--) { solve(); } }
### Prompt Please provide a CPP coded solution to the problem described below: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int i = 0, j = 0; long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } bool isPowerOfTwo(long long n) { return n && (!(n & (n - 1))); } bool is_numeric(char c) { return c >= 'a' and c <= 'z'; } string decToBinary(int n) { string binaryNum; int i = 0; while (n > 0) { binaryNum.push_back(n % 2); n = n / 2; i++; } return binaryNum; } void solve() { int n; cin >> n; vector<int> v(n); for (i = 0; i < n; ++i) { cin >> v[i]; } sort(v.begin(), v.end()); int mini = INT_MAX; for (i = 1; i < n; ++i) { mini = ((mini) < (v[i] - v[i - 1]) ? (mini) : (v[i] - v[i - 1])); } cout << mini << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ; int test; cin >> test; while (test--) { solve(); } } ```
#include <bits/stdc++.h> int main() { int t; scanf("%d", &t); int n; for (int i = 0; i < t; i++) { scanf("%d", &n); int a[n]; for (int j = 0; j < n; j++) { scanf("%d", &a[j]); } int min = 10000; for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++) { int temp; if (j != k) temp = a[j] - a[k]; else continue; if (temp < 0) temp *= -1; if (temp < min) min = temp; } } printf("%d\n", min); } return 0; }
### Prompt Please formulate a cpp solution to the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> int main() { int t; scanf("%d", &t); int n; for (int i = 0; i < t; i++) { scanf("%d", &n); int a[n]; for (int j = 0; j < n; j++) { scanf("%d", &a[j]); } int min = 10000; for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++) { int temp; if (j != k) temp = a[j] - a[k]; else continue; if (temp < 0) temp *= -1; if (temp < min) min = temp; } } printf("%d\n", min); } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int t; cin >> t; while (t--) { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a, a + n); int mn = 1001; int vl1 = 0; int vl2 = 0; for (int i = 1; i < n; i++) { if (a[i] - a[i - 1] < mn) { vl1 = a[i]; vl2 = a[i - 1]; mn = a[i] - a[i - 1]; } } cout << mn << "\n"; } return 0; }
### Prompt Create a solution in CPP for the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int t; cin >> t; while (t--) { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a, a + n); int mn = 1001; int vl1 = 0; int vl2 = 0; for (int i = 1; i < n; i++) { if (a[i] - a[i - 1] < mn) { vl1 = a[i]; vl2 = a[i - 1]; mn = a[i] - a[i - 1]; } } cout << mn << "\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; int powerMod(int x, unsigned int y, int MOD) { int res = 1; x = x % MOD; if (x == 0) return 0; while (y > 0) { if (y & 1) res = (res * x) % MOD; y = y >> 1; x = (x * x) % MOD; } return res; } int main() { int t; cin >> t; for (int i = 0; i < t; i++) { int n; cin >> n; vector<int> v(n); for (int j = 0; j < n; j++) { cin >> v[j]; } sort(v.begin(), v.end()); int min = abs(v[1] - v[0]); for (int j = 0; j < n - 1; j++) { int temp = abs(v[j] - v[j + 1]); if (temp < min) { min = temp; } } cout << min << "\n"; } return 0; }
### Prompt Please formulate a CPP solution to the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; int powerMod(int x, unsigned int y, int MOD) { int res = 1; x = x % MOD; if (x == 0) return 0; while (y > 0) { if (y & 1) res = (res * x) % MOD; y = y >> 1; x = (x * x) % MOD; } return res; } int main() { int t; cin >> t; for (int i = 0; i < t; i++) { int n; cin >> n; vector<int> v(n); for (int j = 0; j < n; j++) { cin >> v[j]; } sort(v.begin(), v.end()); int min = abs(v[1] - v[0]); for (int j = 0; j < n - 1; j++) { int temp = abs(v[j] - v[j + 1]); if (temp < min) { min = temp; } } cout << min << "\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; void input(int &x) { bool neg = false; register int c; x = 0; c = getchar(); if (c == '-') { neg = true; c = getchar(); } for (; (c > 47 && c < 58); c = getchar()) x = (x << 1) + (x << 3) + c - 48; if (neg) x *= -1; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while (t--) { int n; cin >> n; int arr[n]; for (int i = 0; i < n; i++) { cin >> arr[i]; } sort(arr, arr + n); int m = INT_MAX; for (int i = 1; i < n; i++) { m = min(m, abs(arr[i] - arr[i - 1])); } cout << m << endl; } return 0; }
### Prompt Your challenge is to write a cpp solution to the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; void input(int &x) { bool neg = false; register int c; x = 0; c = getchar(); if (c == '-') { neg = true; c = getchar(); } for (; (c > 47 && c < 58); c = getchar()) x = (x << 1) + (x << 3) + c - 48; if (neg) x *= -1; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while (t--) { int n; cin >> n; int arr[n]; for (int i = 0; i < n; i++) { cin >> arr[i]; } sort(arr, arr + n); int m = INT_MAX; for (int i = 1; i < n; i++) { m = min(m, abs(arr[i] - arr[i - 1])); } cout << m << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int MAX = 50; void Get_Array(int n, int a[]) { for (int i = 0; i < n; i++) { cin >> a[i]; } } void Print_Array(int n, int a[]) { for (int i = 0; i < n; i++) { cout << i << "---->" << a[i] << endl; } } void MG(int k, int m, int v[], int u[], int s[]) { int i = 0, j = 0, h = 0; while (h < k + m) { while (i < k && j < m) { if (v[i] < u[j]) { s[h] = v[i]; i++; } else { s[h] = u[j]; j++; } h++; } if (i = k) { int t = j; while (t < m) { s[h] = u[t]; h++; t++; } } else if (j = m) { int t = i; while (t < k) { s[h] = v[t]; h++; t++; } } } } void MS(int n, int s[]) { int v[MAX]; int u[MAX]; int m = n / 2; int k = n - m; for (int i = 0; i < k; i++) { s[i] = v[k]; } for (int j = k; j < n; j++) { s[j] = u[j]; } MS(k, v); MS(m, u); MG(k, m, v, u, s); } void bubblesort(int n, int s[]) { for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { if (s[i] > s[j]) { swap(s[i], s[j]); } } } } int main() { int t; cin >> t; for (int test = 0; test < t; test++) { int s[MAX]; int n; cin >> n; Get_Array(n, s); if (n > 2) { bubblesort(n, s); int mini = abs(s[1] - s[0]); for (int i = 2; i < n; i++) { if (abs(s[i] - s[i - 1]) < mini) { mini = abs(s[i] - s[i - 1]); } } cout << mini << endl; } else if (n == 2) { if (s[1] > s[0]) { cout << s[1] - s[0] << endl; } else { cout << s[0] - s[1] << endl; } } } return 0; }
### Prompt Construct a CPP code solution to the problem outlined: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MAX = 50; void Get_Array(int n, int a[]) { for (int i = 0; i < n; i++) { cin >> a[i]; } } void Print_Array(int n, int a[]) { for (int i = 0; i < n; i++) { cout << i << "---->" << a[i] << endl; } } void MG(int k, int m, int v[], int u[], int s[]) { int i = 0, j = 0, h = 0; while (h < k + m) { while (i < k && j < m) { if (v[i] < u[j]) { s[h] = v[i]; i++; } else { s[h] = u[j]; j++; } h++; } if (i = k) { int t = j; while (t < m) { s[h] = u[t]; h++; t++; } } else if (j = m) { int t = i; while (t < k) { s[h] = v[t]; h++; t++; } } } } void MS(int n, int s[]) { int v[MAX]; int u[MAX]; int m = n / 2; int k = n - m; for (int i = 0; i < k; i++) { s[i] = v[k]; } for (int j = k; j < n; j++) { s[j] = u[j]; } MS(k, v); MS(m, u); MG(k, m, v, u, s); } void bubblesort(int n, int s[]) { for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { if (s[i] > s[j]) { swap(s[i], s[j]); } } } } int main() { int t; cin >> t; for (int test = 0; test < t; test++) { int s[MAX]; int n; cin >> n; Get_Array(n, s); if (n > 2) { bubblesort(n, s); int mini = abs(s[1] - s[0]); for (int i = 2; i < n; i++) { if (abs(s[i] - s[i - 1]) < mini) { mini = abs(s[i] - s[i - 1]); } } cout << mini << endl; } else if (n == 2) { if (s[1] > s[0]) { cout << s[1] - s[0] << endl; } else { cout << s[0] - s[1] << endl; } } } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int long long n, a[60], mx = 2000; scanf("%lld", &n); for (int i = 0; i < n; i++) { scanf("%lld", &a[i]); } sort(a, a + n); for (int i = 1; i < n; i++) { mx = min(mx, abs(a[i - 1] - a[i])); } printf("%lld\n", mx); } return 0; }
### Prompt Generate a cpp solution to the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int long long n, a[60], mx = 2000; scanf("%lld", &n); for (int i = 0; i < n; i++) { scanf("%lld", &a[i]); } sort(a, a + n); for (int i = 1; i < n; i++) { mx = min(mx, abs(a[i - 1] - a[i])); } printf("%lld\n", mx); } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; long long ar[n], ans = 1000000, a, b; for (int i = 0; i < n; i++) { cin >> ar[i]; } sort(ar, ar + n); for (int i = 0; i < n - 1; i++) { a = ar[i + 1] - ar[i]; ans = min(ans, a); } cout << ans << endl; } }
### Prompt Create a solution in cpp for the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; long long ar[n], ans = 1000000, a, b; for (int i = 0; i < n; i++) { cin >> ar[i]; } sort(ar, ar + n); for (int i = 0; i < n - 1; i++) { a = ar[i + 1] - ar[i]; ans = min(ans, a); } cout << ans << endl; } } ```
#include <bits/stdc++.h> using namespace std; long long n, a[51]; bool cmp(long long x, long long y) { return x < y; } void A() { long long ans = 1000000000; scanf("%lld", &n); for (long long i = 1; i <= n; i++) scanf("%lld", &a[i]); sort(a + 1, a + n + 1, cmp); for (long long i = 1; i < n; i++) ans = min(ans, abs(a[i] - a[i + 1])); printf("%lld\n", ans); } signed main() { long long q; scanf("%lld", &q); while (q--) A(); return 0; }
### Prompt Generate a Cpp solution to the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long n, a[51]; bool cmp(long long x, long long y) { return x < y; } void A() { long long ans = 1000000000; scanf("%lld", &n); for (long long i = 1; i <= n; i++) scanf("%lld", &a[i]); sort(a + 1, a + n + 1, cmp); for (long long i = 1; i < n; i++) ans = min(ans, abs(a[i] - a[i + 1])); printf("%lld\n", ans); } signed main() { long long q; scanf("%lld", &q); while (q--) A(); return 0; } ```
#include <bits/stdc++.h> using namespace std; int T; int answer = 0; vector<int> v; int main(void) { ios::sync_with_stdio(0); cin.tie(nullptr); cout.tie(nullptr); cin >> T; while (T--) { v.clear(); int n; cin >> n; v.resize(n); for (int i = 0; i < n; ++i) { cin >> v[i]; } sort(v.begin(), v.end()); answer = 987654321; for (int i = 1; i < n; ++i) { int diff = v[i] - v[i - 1]; if (diff < answer) { answer = diff; } } cout << answer << "\n"; } return 0; }
### Prompt Generate a CPP solution to the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int T; int answer = 0; vector<int> v; int main(void) { ios::sync_with_stdio(0); cin.tie(nullptr); cout.tie(nullptr); cin >> T; while (T--) { v.clear(); int n; cin >> n; v.resize(n); for (int i = 0; i < n; ++i) { cin >> v[i]; } sort(v.begin(), v.end()); answer = 987654321; for (int i = 1; i < n; ++i) { int diff = v[i] - v[i - 1]; if (diff < answer) { answer = diff; } } cout << answer << "\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int honest_coach(vector<int> arr, int n) { sort(arr.begin(), arr.end()); int result = INT_MAX; for (int i = 0; i < arr.size() - 1; i++) { result = min(result, abs(arr[i] - arr[i + 1])); } return result; } int main() { int T; cin >> T; while (T--) { int n; cin >> n; vector<int> arr(n); for (int i = 0; i < n; i++) cin >> arr[i]; cout << honest_coach(arr, n) << "\n"; } return 0; }
### Prompt Please create a solution in Cpp to the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int honest_coach(vector<int> arr, int n) { sort(arr.begin(), arr.end()); int result = INT_MAX; for (int i = 0; i < arr.size() - 1; i++) { result = min(result, abs(arr[i] - arr[i + 1])); } return result; } int main() { int T; cin >> T; while (T--) { int n; cin >> n; vector<int> arr(n); for (int i = 0; i < n; i++) cin >> arr[i]; cout << honest_coach(arr, n) << "\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int mod = 998244353; vector<long long> p; int ho, mi, se, ho1, mi1, cnt; void fix_time() { if (se > 59) mi += se / 60, se = se % 60; if (se < 0) se += 60, mi--; if (mi > 59) ho += mi / 60, mi = mi % 60; if (mi < 0) mi += 60, ho--; if (ho < 0) ho += 24; if (ho >= 24) ho -= 24; } unsigned long long my_pow(int x, int y) { unsigned long long num = 1; for (unsigned long long i = x; y > 0; i = (i * i), y >>= 1) if (y & 1) num = (num * i); return num; } bool prime[10000001], prime2[1000001]; void sieve() { for (int i = 2; i <= 10000000; i += 2) prime[i] = false, prime[i - 1] = true; prime[2] = true; for (int i = 3; i * i <= 10000000; i += 2) if (prime[i]) for (int j = i * i; j <= 10000000; j += 2 * i) prime[j] = false; } vector<long long> vv, vv1; void pfact(long long xx) { if (xx % 2 == 0) { p.push_back(2); while (xx % 2 == 0) xx /= 2, cnt++; vv.push_back(cnt); } for (long long i = 3; i * i <= xx; i += 2) if (xx % i == 0) { cnt = 0; p.push_back(i); while (xx % i == 0) xx /= i, cnt++; vv.push_back(cnt); } if (xx > 1) p.push_back(xx), cnt++, vv.push_back(1); } long long gcd(long long a, long long b) { if (b == 0) return a; a %= b; return gcd(b, a); } long long m, n, x, y, z, k, mn = 3e9, mx, ans, a[500001], b[500003]; vector<long long> v; char ch; string s; int t; bool yes; int main() { ios_base::sync_with_stdio(false); cin >> t; while (t--) { cin >> n; mn = 1e9; for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a, a + n); for (int i = 0; i < n - 1; i++) { mn = min(mn, a[i + 1] - a[i]); } cout << mn << endl; } return 0; }
### Prompt Generate a CPP solution to the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int mod = 998244353; vector<long long> p; int ho, mi, se, ho1, mi1, cnt; void fix_time() { if (se > 59) mi += se / 60, se = se % 60; if (se < 0) se += 60, mi--; if (mi > 59) ho += mi / 60, mi = mi % 60; if (mi < 0) mi += 60, ho--; if (ho < 0) ho += 24; if (ho >= 24) ho -= 24; } unsigned long long my_pow(int x, int y) { unsigned long long num = 1; for (unsigned long long i = x; y > 0; i = (i * i), y >>= 1) if (y & 1) num = (num * i); return num; } bool prime[10000001], prime2[1000001]; void sieve() { for (int i = 2; i <= 10000000; i += 2) prime[i] = false, prime[i - 1] = true; prime[2] = true; for (int i = 3; i * i <= 10000000; i += 2) if (prime[i]) for (int j = i * i; j <= 10000000; j += 2 * i) prime[j] = false; } vector<long long> vv, vv1; void pfact(long long xx) { if (xx % 2 == 0) { p.push_back(2); while (xx % 2 == 0) xx /= 2, cnt++; vv.push_back(cnt); } for (long long i = 3; i * i <= xx; i += 2) if (xx % i == 0) { cnt = 0; p.push_back(i); while (xx % i == 0) xx /= i, cnt++; vv.push_back(cnt); } if (xx > 1) p.push_back(xx), cnt++, vv.push_back(1); } long long gcd(long long a, long long b) { if (b == 0) return a; a %= b; return gcd(b, a); } long long m, n, x, y, z, k, mn = 3e9, mx, ans, a[500001], b[500003]; vector<long long> v; char ch; string s; int t; bool yes; int main() { ios_base::sync_with_stdio(false); cin >> t; while (t--) { cin >> n; mn = 1e9; for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a, a + n); for (int i = 0; i < n - 1; i++) { mn = min(mn, a[i + 1] - a[i]); } cout << mn << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; struct compare { bool operator()(pair<long long int, long long int> a, pair<long long int, long long int> b) { if ((a.second - a.first) > (b.second - b.first)) return false; else if ((a.second - a.first) == (b.second - b.first)) { if (a.first < b.first) return false; else return true; } else return true; } }; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long int t; cin >> t; while (t--) { long long int n, b; cin >> n; vector<long long int> a(n); for (long long int i = 0; i < n; i++) cin >> a[i]; sort(a.begin(), a.end()); long long int ans = 10000000; for (long long int i = 0; i < n; i++) { for (long long int j = i + 1; j < n; j++) { ans = min(ans, a[j] - a[i]); } } cout << ans << endl; } }
### Prompt Your task is to create a Cpp solution to the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; struct compare { bool operator()(pair<long long int, long long int> a, pair<long long int, long long int> b) { if ((a.second - a.first) > (b.second - b.first)) return false; else if ((a.second - a.first) == (b.second - b.first)) { if (a.first < b.first) return false; else return true; } else return true; } }; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long int t; cin >> t; while (t--) { long long int n, b; cin >> n; vector<long long int> a(n); for (long long int i = 0; i < n; i++) cin >> a[i]; sort(a.begin(), a.end()); long long int ans = 10000000; for (long long int i = 0; i < n; i++) { for (long long int j = i + 1; j < n; j++) { ans = min(ans, a[j] - a[i]); } } cout << ans << endl; } } ```
#include <bits/stdc++.h> using namespace std; int main() { int n, a, b; cin >> n; while (n--) { cin >> a; vector<int> vect; int mini(1001); while (a--) { cin >> b; vect.push_back(b); } sort(vect.begin(), vect.end()); for (int i(0); i < vect.size() - 1; i++) { if (abs(vect[i] - vect[i + 1]) < mini) mini = abs(vect[i] - vect[i + 1]); } cout << mini << '\n'; } return 0; }
### Prompt Generate a cpp solution to the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n, a, b; cin >> n; while (n--) { cin >> a; vector<int> vect; int mini(1001); while (a--) { cin >> b; vect.push_back(b); } sort(vect.begin(), vect.end()); for (int i(0); i < vect.size() - 1; i++) { if (abs(vect[i] - vect[i + 1]) < mini) mini = abs(vect[i] - vect[i + 1]); } cout << mini << '\n'; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int n, m; int read() { int s = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { s = s * 10 + ch - '0'; ch = getchar(); } return s * f; } int s[101010]; int main() { int T = read(); while (T--) { n = read(); for (int i = 1; i <= n; ++i) { s[i] = read(); } sort(s + 1, s + n + 1); int ans = 101010101; for (int i = 1; i < n; ++i) { ans = min(ans, abs(s[i + 1] - s[i])); } cout << ans << endl; } }
### Prompt Develop a solution in CPP to the problem described below: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, m; int read() { int s = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { s = s * 10 + ch - '0'; ch = getchar(); } return s * f; } int s[101010]; int main() { int T = read(); while (T--) { n = read(); for (int i = 1; i <= n; ++i) { s[i] = read(); } sort(s + 1, s + n + 1); int ans = 101010101; for (int i = 1; i < n; ++i) { ans = min(ans, abs(s[i + 1] - s[i])); } cout << ans << endl; } } ```
#include <bits/stdc++.h> using namespace std; int abs(int a, int b) { if (a < b) { return b - a; } else { return a - b; } } int main() { int t; int arr[100]; cin >> t; for (int i = 0; i < t; i++) { int n; cin >> n; for (int j = 0; j < n; j++) { cin >> arr[j]; } int min_value = 2000; for (int j = 0; j < n; j++) { for (int k = j + 1; k < n; k++) { int cmin = abs(arr[j], arr[k]); if (cmin < min_value) { min_value = cmin; } } } cout << min_value << endl; } return 0; }
### Prompt Your task is to create a cpp solution to the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int abs(int a, int b) { if (a < b) { return b - a; } else { return a - b; } } int main() { int t; int arr[100]; cin >> t; for (int i = 0; i < t; i++) { int n; cin >> n; for (int j = 0; j < n; j++) { cin >> arr[j]; } int min_value = 2000; for (int j = 0; j < n; j++) { for (int k = j + 1; k < n; k++) { int cmin = abs(arr[j], arr[k]); if (cmin < min_value) { min_value = cmin; } } } cout << min_value << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int mod = 998244353; long long t, n, a[55], b; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> t; while (t--) { cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; long long ans = 1e9; sort(a + 1, a + 1 + n); for (int i = 2; i <= n; i++) ans = min(ans, a[i] - a[i - 1]); cout << ans << '\n'; } }
### Prompt Generate a Cpp solution to the following problem: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int mod = 998244353; long long t, n, a[55], b; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> t; while (t--) { cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; long long ans = 1e9; sort(a + 1, a + 1 + n); for (int i = 2; i <= n; i++) ans = min(ans, a[i] - a[i - 1]); cout << ans << '\n'; } } ```
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a, a + n); int m = a[1] - a[0]; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { m = min(m, a[j] - a[i]); } } cout << m << endl; } }
### Prompt Develop a solution in Cpp to the problem described below: There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β€” the athlete number i has the strength s_i. You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team. You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B. For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is: * first team: A = [1, 2, 4], * second team: B = [3, 6]. In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams. Print the minimum value |max(A) - min(B)|. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case consists of two lines. The first line contains positive integer n (2 ≀ n ≀ 50) β€” number of athletes. The second line contains n positive integers s_1, s_2, …, s_n (1 ≀ s_i ≀ 1000), where s_i β€” is the strength of the i-th athlete. Please note that s values may not be distinct. Output For each test case print one integer β€” the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams. Example Input 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 Output 1 0 2 999 50 Note The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a, a + n); int m = a[1] - a[0]; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { m = min(m, a[j] - a[i]); } } cout << m << endl; } } ```