output
stringlengths 52
181k
| instruction
stringlengths 296
182k
|
---|---|
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n;
while (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++) {
int t = a[i] - a[i - 1];
ans = min(ans, t);
}
cout << ans << "\n";
}
}
int main() {
ios_base ::sync_with_stdio(0);
cin.tie(0);
int tc;
cin >> tc;
while (tc--) {
solve();
}
}
| ### 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;
void solve() {
int n;
while (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++) {
int t = a[i] - a[i - 1];
ans = min(ans, t);
}
cout << ans << "\n";
}
}
int main() {
ios_base ::sync_with_stdio(0);
cin.tie(0);
int tc;
cin >> tc;
while (tc--) {
solve();
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
template <typename A>
ostream &operator<<(ostream &cout, vector<A> const &v);
template <typename A, typename B>
ostream &operator<<(ostream &cout, pair<A, B> const &p) {
return cout << "(" << p.first << ", " << p.second << ")";
}
template <typename A>
ostream &operator<<(ostream &cout, vector<A> const &v) {
cout << "[ ";
for (int i = 0; i < v.size(); i++) {
if (i) cout << ", ";
cout << v[i];
}
return cout << " ]";
}
template <typename A>
ostream &operator<<(ostream &cout, vector<vector<A>> const &v) {
cout << "[";
for (int i = 0; i < v.size(); i++) {
if (i) {
cout << " ";
}
cout << v[i];
if (i < v.size() - 1) {
cout << "\n";
}
}
return cout << "]";
}
template <typename A, typename B>
istream &operator>>(istream &cin, pair<A, B> &p) {
cin >> p.first;
return cin >> p.second;
}
template <typename T, typename U>
void cmin(T &a, U b) {
if (a > b) a = b;
}
template <typename T, typename U>
void cmax(T &a, U b) {
if (a < b) a = b;
}
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 = INT_MAX;
for (int i = 1; i < n; i++) {
mn = min(mn, abs(a[i] - a[i - 1]));
}
cout << mn << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t = 1;
cin >> t;
while (t--) {
solve();
}
}
| ### 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 A>
ostream &operator<<(ostream &cout, vector<A> const &v);
template <typename A, typename B>
ostream &operator<<(ostream &cout, pair<A, B> const &p) {
return cout << "(" << p.first << ", " << p.second << ")";
}
template <typename A>
ostream &operator<<(ostream &cout, vector<A> const &v) {
cout << "[ ";
for (int i = 0; i < v.size(); i++) {
if (i) cout << ", ";
cout << v[i];
}
return cout << " ]";
}
template <typename A>
ostream &operator<<(ostream &cout, vector<vector<A>> const &v) {
cout << "[";
for (int i = 0; i < v.size(); i++) {
if (i) {
cout << " ";
}
cout << v[i];
if (i < v.size() - 1) {
cout << "\n";
}
}
return cout << "]";
}
template <typename A, typename B>
istream &operator>>(istream &cin, pair<A, B> &p) {
cin >> p.first;
return cin >> p.second;
}
template <typename T, typename U>
void cmin(T &a, U b) {
if (a > b) a = b;
}
template <typename T, typename U>
void cmax(T &a, U b) {
if (a < b) a = b;
}
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 = INT_MAX;
for (int i = 1; i < n; i++) {
mn = min(mn, abs(a[i] - a[i - 1]));
}
cout << mn << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t = 1;
cin >> t;
while (t--) {
solve();
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
vector<int> ath;
int a;
cin >> a;
for (int i = 0; i < a; i++) {
int g;
cin >> g;
ath.emplace_back(g);
}
sort(ath.begin(), ath.end());
int diff = ath[a - 1] - ath[a - 2];
for (int i = 1; i < a; i++) {
int d = ath[i] - ath[i - 1];
if (d < diff) diff = d;
}
cout << diff << "\n";
}
}
| ### 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() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
vector<int> ath;
int a;
cin >> a;
for (int i = 0; i < a; i++) {
int g;
cin >> g;
ath.emplace_back(g);
}
sort(ath.begin(), ath.end());
int diff = ath[a - 1] - ath[a - 2];
for (int i = 1; i < a; i++) {
int d = ath[i] - ath[i - 1];
if (d < diff) diff = d;
}
cout << diff << "\n";
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int cases;
cin >> cases;
for (int i = 0; i < cases; i++) {
vector<int> v;
int len;
cin >> len;
for (int j = 0; j < len; j++) {
int inp;
cin >> inp;
v.push_back(inp);
}
sort(v.begin(), v.end());
int min = 2000;
for (int k = 0; k < v.size() - 1; k++) {
int check = v[k + 1] - v[k];
if (check < min) min = check;
}
cout << min << 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() {
int cases;
cin >> cases;
for (int i = 0; i < cases; i++) {
vector<int> v;
int len;
cin >> len;
for (int j = 0; j < len; j++) {
int inp;
cin >> inp;
v.push_back(inp);
}
sort(v.begin(), v.end());
int min = 2000;
for (int k = 0; k < v.size() - 1; k++) {
int check = v[k + 1] - v[k];
if (check < min) min = check;
}
cout << min << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int32_t main() {
long long t;
cin >> t;
while (t--) {
long long n;
cin >> n;
vector<long long> v(n);
for (long long i = 0; i < n; i++) cin >> v[i];
sort(v.begin(), v.end());
long long ans = INT_MAX;
for (long long i = 0; i < (n - 1); i++) ans = min(ans, v[i + 1] - v[i]);
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;
int32_t main() {
long long t;
cin >> t;
while (t--) {
long long n;
cin >> n;
vector<long long> v(n);
for (long long i = 0; i < n; i++) cin >> v[i];
sort(v.begin(), v.end());
long long ans = INT_MAX;
for (long long i = 0; i < (n - 1); i++) ans = min(ans, v[i + 1] - v[i]);
cout << ans << "\n";
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
srand(time(NULL));
long long int t, i, j;
cin >> t;
while (t--) {
long long int n;
cin >> n;
long long int a[n];
long long int ans = LONG_LONG_MAX;
for (i = 0; i < n; i++) {
cin >> a[i];
}
sort(a, a + n);
for (i = 0; i < n - 1; i++) {
if (a[i + 1] - a[i] < ans) ans = a[i + 1] - 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;
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
srand(time(NULL));
long long int t, i, j;
cin >> t;
while (t--) {
long long int n;
cin >> n;
long long int a[n];
long long int ans = LONG_LONG_MAX;
for (i = 0; i < n; i++) {
cin >> a[i];
}
sort(a, a + n);
for (i = 0; i < n - 1; i++) {
if (a[i + 1] - a[i] < ans) ans = a[i + 1] - a[i];
}
cout << ans << endl;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
long long int n;
cin >> n;
int arr[n];
vector<long long int> v;
for (int i = 0; i < n; i++) cin >> arr[i];
sort(arr, arr + n);
for (int i = 0; i < n - 1; i++) {
int x = arr[i] - arr[i + 1];
x = abs(x);
v.push_back(x);
}
cout << *min_element(v.begin(), v.end()) << "\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 t;
cin >> t;
while (t--) {
long long int n;
cin >> n;
int arr[n];
vector<long long int> v;
for (int i = 0; i < n; i++) cin >> arr[i];
sort(arr, arr + n);
for (int i = 0; i < n - 1; i++) {
int x = arr[i] - arr[i + 1];
x = abs(x);
v.push_back(x);
}
cout << *min_element(v.begin(), v.end()) << "\n";
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1);
const long long int MOD = 1000000007;
int main() {
int t;
scanf("%d", &t);
while (t--) {
int n;
scanf("%d", &n);
vector<int> v(n);
for (int i = int(0); i <= int(n - 1); i++) scanf("%d", &v[i]);
sort(v.begin(), v.end());
int mx = 1001;
for (int i = 1; i < n; i++) {
mx = min(mx, v[i] - v[i - 1]);
}
printf("%d", mx), printf("\n");
}
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;
const double PI = acos(-1);
const long long int MOD = 1000000007;
int main() {
int t;
scanf("%d", &t);
while (t--) {
int n;
scanf("%d", &n);
vector<int> v(n);
for (int i = int(0); i <= int(n - 1); i++) scanf("%d", &v[i]);
sort(v.begin(), v.end());
int mx = 1001;
for (int i = 1; i < n; i++) {
mx = min(mx, v[i] - v[i - 1]);
}
printf("%d", mx), printf("\n");
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long N = 1e6 + 1;
set<long long> o;
map<long long, long long> mp, pm;
map<char, long long> hs;
vector<long long> v, vc, oreder1, inoreder, posoreder, ve, vv, vd;
deque<char> p, pe;
deque<string> ts, ss, stoi, rs;
deque<double> sd, ds;
string g, s, w, h, s1;
char z, u = 'a';
bool re, rt;
deque<int> e;
map<pair<long long, long long>, long long> aw, wa;
long long cnt, ans, TT, n, a, b, c, q, m, t, d, temp, mx = 0, mn = 0, rem, sum,
tt = 1e32;
int main() {
ios_base::sync_with_stdio(0);
srand(time(NULL));
cin >> t;
while (t--) {
cin >> a;
cnt = 0;
v.clear();
ans = 0;
tt = 1e32;
for (long long i = 0; i < a; i++) {
cin >> b;
v.push_back(b);
}
sort(v.begin(), v.end());
for (long long i = 0; i < v.size() - 1; i++) {
if (v[i] == v[i + 1]) cnt++;
}
if (cnt > 0)
cout << 0 << endl;
else {
for (long long i = 0; i < a - 1; i++) {
ans = (v[i + 1] - v[i]);
tt = min(ans, tt);
}
cout << tt << 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;
const long long N = 1e6 + 1;
set<long long> o;
map<long long, long long> mp, pm;
map<char, long long> hs;
vector<long long> v, vc, oreder1, inoreder, posoreder, ve, vv, vd;
deque<char> p, pe;
deque<string> ts, ss, stoi, rs;
deque<double> sd, ds;
string g, s, w, h, s1;
char z, u = 'a';
bool re, rt;
deque<int> e;
map<pair<long long, long long>, long long> aw, wa;
long long cnt, ans, TT, n, a, b, c, q, m, t, d, temp, mx = 0, mn = 0, rem, sum,
tt = 1e32;
int main() {
ios_base::sync_with_stdio(0);
srand(time(NULL));
cin >> t;
while (t--) {
cin >> a;
cnt = 0;
v.clear();
ans = 0;
tt = 1e32;
for (long long i = 0; i < a; i++) {
cin >> b;
v.push_back(b);
}
sort(v.begin(), v.end());
for (long long i = 0; i < v.size() - 1; i++) {
if (v[i] == v[i + 1]) cnt++;
}
if (cnt > 0)
cout << 0 << endl;
else {
for (long long i = 0; i < a - 1; i++) {
ans = (v[i + 1] - v[i]);
tt = min(ans, tt);
}
cout << tt << endl;
}
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long exp(long long x, long long y, long long p) {
long long res = 1;
while (y) {
if (y % 2) res = (res * x % p) % p;
x = (x * x) % p;
y /= 2;
}
return res;
}
long long exp(long long x, long long y) {
long long res = 1;
while (y) {
if (y % 2) res = (res * x);
x = (x * x);
y /= 2;
}
return res;
}
long long sv[1000000] = {0};
long long factorialNumInverse[((long long)2e6 + 10) + 1];
long long naturalNumInverse[((long long)2e6 + 10) + 1];
long long fact[((long long)2e6 + 10) + 1];
void InverseofNumber() {
naturalNumInverse[0] = naturalNumInverse[1] = 1;
for (long long i = 2; i <= ((long long)2e6 + 10); i++)
naturalNumInverse[i] = naturalNumInverse[((long long)1e9 + 7) % i] *
(((long long)1e9 + 7) - ((long long)1e9 + 7) / i) %
((long long)1e9 + 7);
}
void InverseofFactorial() {
factorialNumInverse[0] = factorialNumInverse[1] = 1;
for (long long i = 2; i <= ((long long)2e6 + 10); i++)
factorialNumInverse[i] =
(naturalNumInverse[i] * factorialNumInverse[i - 1]) %
((long long)1e9 + 7);
}
void factorial() {
fact[0] = 1;
for (long long i = 1; i <= ((long long)2e6 + 10); i++) {
fact[i] = (fact[i - 1] * i) % ((long long)1e9 + 7);
}
}
long long Binomial(long long n, long long R) {
long long ans = ((fact[n] * factorialNumInverse[R]) % ((long long)1e9 + 7) *
(factorialNumInverse[n - R]) % ((long long)1e9 + 7)) %
((long long)1e9 + 7);
return ans;
}
long long phi(long long n) {
long long ans = n;
for (long long i = 2; i * i <= n; i++) {
if (n % i == 0) {
while (n % i == 0) n /= i;
ans -= ans / i;
}
}
if (n > 1) ans -= ans / n;
return ans;
}
void fileIO() {}
long long gcd(long long a, long long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
long long issquare(long long x) {
long long k = sqrt(x);
if (k * k == x) return 1;
return 0;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
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 first[10001] = {0};
for (long long i = 0; i < n; i++) {
first[a[i]]++;
}
sort(a, a + n);
bool ok;
for (long long i = 0; i < 10001; i++) {
if (first[i] == 1 || first[i] == 0) {
ok = 1;
} else {
ok = 0;
break;
}
}
if (ok == 1) {
long long mini = 1000000000;
for (long long i = 0; i < n - 1; i++) {
mini = min(mini, a[i + 1] - a[i]);
}
cout << mini << "\n";
} else {
cout << 0 << "\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;
long long exp(long long x, long long y, long long p) {
long long res = 1;
while (y) {
if (y % 2) res = (res * x % p) % p;
x = (x * x) % p;
y /= 2;
}
return res;
}
long long exp(long long x, long long y) {
long long res = 1;
while (y) {
if (y % 2) res = (res * x);
x = (x * x);
y /= 2;
}
return res;
}
long long sv[1000000] = {0};
long long factorialNumInverse[((long long)2e6 + 10) + 1];
long long naturalNumInverse[((long long)2e6 + 10) + 1];
long long fact[((long long)2e6 + 10) + 1];
void InverseofNumber() {
naturalNumInverse[0] = naturalNumInverse[1] = 1;
for (long long i = 2; i <= ((long long)2e6 + 10); i++)
naturalNumInverse[i] = naturalNumInverse[((long long)1e9 + 7) % i] *
(((long long)1e9 + 7) - ((long long)1e9 + 7) / i) %
((long long)1e9 + 7);
}
void InverseofFactorial() {
factorialNumInverse[0] = factorialNumInverse[1] = 1;
for (long long i = 2; i <= ((long long)2e6 + 10); i++)
factorialNumInverse[i] =
(naturalNumInverse[i] * factorialNumInverse[i - 1]) %
((long long)1e9 + 7);
}
void factorial() {
fact[0] = 1;
for (long long i = 1; i <= ((long long)2e6 + 10); i++) {
fact[i] = (fact[i - 1] * i) % ((long long)1e9 + 7);
}
}
long long Binomial(long long n, long long R) {
long long ans = ((fact[n] * factorialNumInverse[R]) % ((long long)1e9 + 7) *
(factorialNumInverse[n - R]) % ((long long)1e9 + 7)) %
((long long)1e9 + 7);
return ans;
}
long long phi(long long n) {
long long ans = n;
for (long long i = 2; i * i <= n; i++) {
if (n % i == 0) {
while (n % i == 0) n /= i;
ans -= ans / i;
}
}
if (n > 1) ans -= ans / n;
return ans;
}
void fileIO() {}
long long gcd(long long a, long long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
long long issquare(long long x) {
long long k = sqrt(x);
if (k * k == x) return 1;
return 0;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
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 first[10001] = {0};
for (long long i = 0; i < n; i++) {
first[a[i]]++;
}
sort(a, a + n);
bool ok;
for (long long i = 0; i < 10001; i++) {
if (first[i] == 1 || first[i] == 0) {
ok = 1;
} else {
ok = 0;
break;
}
}
if (ok == 1) {
long long mini = 1000000000;
for (long long i = 0; i < n - 1; i++) {
mini = min(mini, a[i + 1] - a[i]);
}
cout << mini << "\n";
} else {
cout << 0 << "\n";
}
}
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;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int ans = INT_MAX;
sort(a.begin(), a.end());
for (int i = 0; i < n - 1; i++) {
ans = min(ans, abs(a[i] - a[i + 1]));
}
cout << ans << 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() {
long long int t;
cin >> t;
while (t--) {
long long int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int ans = INT_MAX;
sort(a.begin(), a.end());
for (int i = 0; i < n - 1; i++) {
ans = min(ans, abs(a[i] - a[i + 1]));
}
cout << ans << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
void codeforces() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int32_t main() {
codeforces();
long long t;
cin >> t;
while (t--) {
long long n;
cin >> n;
vector<long long> a(n);
long long d = INT_MAX;
for (long long i = 0; i < n; i++) cin >> a[i];
for (long long i = 0; i < n; i++) {
for (long long j = i + 1; j < n; j++) {
d = min(d, abs(a[i] - a[j]));
}
}
cout << d << 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;
void codeforces() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int32_t main() {
codeforces();
long long t;
cin >> t;
while (t--) {
long long n;
cin >> n;
vector<long long> a(n);
long long d = INT_MAX;
for (long long i = 0; i < n; i++) cin >> a[i];
for (long long i = 0; i < n; i++) {
for (long long j = i + 1; j < n; j++) {
d = min(d, abs(a[i] - a[j]));
}
}
cout << d << endl;
}
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];
for (long long i = 0; i < n; i++) {
cin >> a[i];
}
sort(a, a + n);
long long m = a[1] - a[0];
for (long long i = 0; i < n - 1; i++) {
if (a[i] == a[i + 1]) {
m = 0;
} else {
m = min(m, a[i + 1] - a[i]);
}
}
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;
int main() {
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];
}
sort(a, a + n);
long long m = a[1] - a[0];
for (long long i = 0; i < n - 1; i++) {
if (a[i] == a[i + 1]) {
m = 0;
} else {
m = min(m, a[i + 1] - a[i]);
}
}
cout << m << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int M = 1e5 + 7;
int a[55];
int main() {
ios_base::sync_with_stdio(false), cin.tie(0);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
sort(a, a + n);
int mn = 1 << 29;
for (int i = 1; i < n; i++) {
mn = min(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;
const int M = 1e5 + 7;
int a[55];
int main() {
ios_base::sync_with_stdio(false), cin.tie(0);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
sort(a, a + n);
int mn = 1 << 29;
for (int i = 1; i < n; i++) {
mn = min(mn, a[i] - a[i - 1]);
}
cout << mn << '\n';
}
return 0;
}
``` |
#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];
}
int min = INT_MAX;
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++)
if (abs(a[i] - a[j]) < min) min = abs(a[i] - a[j]);
}
cout << min << 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() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int min = INT_MAX;
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++)
if (abs(a[i] - a[j]) < min) min = abs(a[i] - a[j]);
}
cout << min << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
inline char nc() {
static char buf[100000], *p1 = buf, *p2 = buf;
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
? EOF
: *p1++;
}
inline int read() {
static char c = nc();
int x = 0, f = 1;
for (; c > '9' || c < '0'; c = nc())
if (c == '-') f = -1;
for (; c <= '9' && c >= '0'; c = nc()) x = (x << 3) + (x << 1) + c - 48;
return x * f;
}
const int N = 1e2 + 10;
const int MOD = 1e9 + 7;
const int inf = 0x3f3f3f3f;
int a[N];
int main() {
int t, n;
cin >> t;
while (t--) {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sort(a, a + n);
int ans = inf;
for (int i = 1; i < n; i++) ans = min(ans, a[i] - a[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;
inline char nc() {
static char buf[100000], *p1 = buf, *p2 = buf;
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
? EOF
: *p1++;
}
inline int read() {
static char c = nc();
int x = 0, f = 1;
for (; c > '9' || c < '0'; c = nc())
if (c == '-') f = -1;
for (; c <= '9' && c >= '0'; c = nc()) x = (x << 3) + (x << 1) + c - 48;
return x * f;
}
const int N = 1e2 + 10;
const int MOD = 1e9 + 7;
const int inf = 0x3f3f3f3f;
int a[N];
int main() {
int t, n;
cin >> t;
while (t--) {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sort(a, a + n);
int ans = inf;
for (int i = 1; i < n; i++) ans = min(ans, a[i] - a[i - 1]);
cout << ans << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long int t;
cin >> t;
while (t--) {
long long n;
cin >> n;
long long a[n], i, ans = 999999999;
for (int i = (0); i < (n); i++) cin >> (a)[i];
;
sort(a, a + n);
for (i = 1; i < n; i++) {
ans = min(ans, abs(a[i] - a[i - 1]));
}
cout << ans << 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() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long int t;
cin >> t;
while (t--) {
long long n;
cin >> n;
long long a[n], i, ans = 999999999;
for (int i = (0); i < (n); i++) cin >> (a)[i];
;
sort(a, a + n);
for (i = 1; i < n; i++) {
ans = min(ans, abs(a[i] - a[i - 1]));
}
cout << ans << endl;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
const long long INF = 1e15;
using Graph = vector<vector<long long>>;
signed main() {
long long T;
cin >> T;
for (long long t = 0; t < T; t++) {
long long N;
cin >> N;
vector<long long> A(N);
for (long long i = 0; i < N; i++) cin >> A[i];
sort(A.begin(), A.end());
long long ans = 1000000;
for (long long i = 1; i < N; i++) {
ans = min(ans, A[i] - A[i - 1]);
}
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;
const long long MOD = 1000000007;
const long long INF = 1e15;
using Graph = vector<vector<long long>>;
signed main() {
long long T;
cin >> T;
for (long long t = 0; t < T; t++) {
long long N;
cin >> N;
vector<long long> A(N);
for (long long i = 0; i < N; i++) cin >> A[i];
sort(A.begin(), A.end());
long long ans = 1000000;
for (long long i = 1; i < N; i++) {
ans = min(ans, A[i] - A[i - 1]);
}
cout << ans << endl;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
vector<int> v;
while (t--) {
vector<int> v;
int n, m = 0;
cin >> n;
for (int i = 0; i < n; i++) {
int c;
cin >> c;
v.push_back(c);
}
sort(v.begin(), v.end());
for (int i = 0; i < v.size() - 1; i++)
if (v[i] == v[i + 1]) {
cout << 0 << "\n";
m++;
break;
}
int mn = 1000;
if (m == 0) {
for (int i = 0; i < v.size() - 1; i++)
for (int j = i + 1; j < v.size(); j++) mn = min(mn, abs(v[i] - v[j]));
}
if (m == 0) cout << mn << "\n";
v.clear();
}
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;
cin >> t;
vector<int> v;
while (t--) {
vector<int> v;
int n, m = 0;
cin >> n;
for (int i = 0; i < n; i++) {
int c;
cin >> c;
v.push_back(c);
}
sort(v.begin(), v.end());
for (int i = 0; i < v.size() - 1; i++)
if (v[i] == v[i + 1]) {
cout << 0 << "\n";
m++;
break;
}
int mn = 1000;
if (m == 0) {
for (int i = 0; i < v.size() - 1; i++)
for (int j = i + 1; j < v.size(); j++) mn = min(mn, abs(v[i] - v[j]));
}
if (m == 0) cout << mn << "\n";
v.clear();
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int t, n, a[1001], ans = 10000;
int main() {
cin >> t;
while (t--) {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sort(a, a + n);
for (int i = 0; i < n - 1; i++) {
ans = min(ans, (a[i + 1] - a[i]));
}
cout << ans << endl;
ans = 10000;
}
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 t, n, a[1001], ans = 10000;
int main() {
cin >> t;
while (t--) {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sort(a, a + n);
for (int i = 0; i < n - 1; i++) {
ans = min(ans, (a[i + 1] - a[i]));
}
cout << ans << endl;
ans = 10000;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
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 ans = INT_MAX;
for (int i = 0; i < n - 1; i++) {
ans = min(s[i + 1] - s[i], ans);
}
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 main() {
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 ans = INT_MAX;
for (int i = 0; i < n - 1; i++) {
ans = min(s[i + 1] - s[i], ans);
}
cout << ans << endl;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++) cin >> arr[i];
int mi = INT_MAX;
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
mi = min(mi, abs(arr[i] - arr[j]));
}
}
cout << mi;
cout << endl;
;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
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;
void solve() {
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++) cin >> arr[i];
int mi = INT_MAX;
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
mi = min(mi, abs(arr[i] - arr[j]));
}
}
cout << mi;
cout << endl;
;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
cin >> t;
while (t--) {
solve();
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long t, n, i, j, m, k, a, b, c = 0;
cin >> t;
while (t--) {
cin >> n;
long long ar[n];
for (long long i = 0; i < n; i++) cin >> ar[i];
sort(ar, ar + n);
long long ans = INT_MAX;
for (i = 1; i < n; i++) {
ans = min(ans, ar[i] - ar[i - 1]);
}
cout << ans << 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;
const long long mod = 1000000007;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long t, n, i, j, m, k, a, b, c = 0;
cin >> t;
while (t--) {
cin >> n;
long long ar[n];
for (long long i = 0; i < n; i++) cin >> ar[i];
sort(ar, ar + n);
long long ans = INT_MAX;
for (i = 1; i < n; i++) {
ans = min(ans, ar[i] - ar[i - 1]);
}
cout << ans << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
while (n--) {
bool digits[1005] = {false};
int numbers;
cin >> numbers;
int dataset[numbers], done = 0;
for (int i = 0; i < numbers; i++) {
cin >> dataset[i];
if (digits[dataset[i]]) done = 1;
digits[dataset[i]] = true;
}
if (done == 1)
cout << 0 << endl;
else {
sort(dataset, dataset + numbers);
int small = INT_MAX;
for (int i = 1; i < numbers; i++) {
int parthokko = dataset[i] - dataset[i - 1];
if (parthokko < small) small = parthokko;
}
cout << small << 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 n;
cin >> n;
while (n--) {
bool digits[1005] = {false};
int numbers;
cin >> numbers;
int dataset[numbers], done = 0;
for (int i = 0; i < numbers; i++) {
cin >> dataset[i];
if (digits[dataset[i]]) done = 1;
digits[dataset[i]] = true;
}
if (done == 1)
cout << 0 << endl;
else {
sort(dataset, dataset + numbers);
int small = INT_MAX;
for (int i = 1; i < numbers; i++) {
int parthokko = dataset[i] - dataset[i - 1];
if (parthokko < small) small = parthokko;
}
cout << small << endl;
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
vector<int> v;
int n, k, a, c, flag = 0, x, tlag = 0, dis, q, firstone, y, len, z, vagfol,
i, sum = 0, ans, total, coun = 0;
cin >> n;
string str;
for (int i = 0; i < n; i++) {
cin >> a;
v.push_back(a);
}
sort(v.begin(), v.end());
int minn = INT_MAX;
for (int i = 0; i < n - 1; i++) {
if (v[i + 1] - v[i] < minn) {
minn = v[i + 1] - v[i];
}
}
cout << minn << 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() {
int t;
cin >> t;
while (t--) {
vector<int> v;
int n, k, a, c, flag = 0, x, tlag = 0, dis, q, firstone, y, len, z, vagfol,
i, sum = 0, ans, total, coun = 0;
cin >> n;
string str;
for (int i = 0; i < n; i++) {
cin >> a;
v.push_back(a);
}
sort(v.begin(), v.end());
int minn = INT_MAX;
for (int i = 0; i < n - 1; i++) {
if (v[i + 1] - v[i] < minn) {
minn = v[i + 1] - v[i];
}
}
cout << minn << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int t, n;
vector<long long> s;
long long min_;
int main() {
cin >> t;
for (int z = 0; z < t; ++z) {
cin >> n;
for (int i = 0; i < n; ++i) {
long long temp;
cin >> temp;
s.push_back(temp);
}
sort(s.begin(), s.end());
min_ = s[1] - s[0];
for (int i = 1; i < n; ++i) {
long long temp = s[i] - s[i - 1];
if (temp < min_) min_ = temp;
}
cout << min_ << endl;
s.clear();
}
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 t, n;
vector<long long> s;
long long min_;
int main() {
cin >> t;
for (int z = 0; z < t; ++z) {
cin >> n;
for (int i = 0; i < n; ++i) {
long long temp;
cin >> temp;
s.push_back(temp);
}
sort(s.begin(), s.end());
min_ = s[1] - s[0];
for (int i = 1; i < n; ++i) {
long long temp = s[i] - s[i - 1];
if (temp < min_) min_ = temp;
}
cout << min_ << endl;
s.clear();
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int M = 1e9 + 7;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int s[n];
for (int i = 0; i < n; i++) {
cin >> s[i];
}
sort(s, s + n);
int ans = 1000;
for (int i = int(1); i <= int(n - 1); i++) {
ans = min(ans, s[i] - s[i - 1]);
}
cout << ans << "\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;
const int M = 1e9 + 7;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int s[n];
for (int i = 0; i < n; i++) {
cin >> s[i];
}
sort(s, s + n);
int ans = 1000;
for (int i = int(1); i <= int(n - 1); i++) {
ans = min(ans, s[i] - s[i - 1]);
}
cout << ans << "\n";
;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e5 + 5;
int main() {
ios_base::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 min = a[1] - a[0];
for (int i = 2; i < n; i++) {
int s = a[i] - a[i - 1];
if (s < min) min = s;
}
cout << min << "\n";
t--;
}
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 INF = 1e5 + 5;
int main() {
ios_base::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 min = a[1] - a[0];
for (int i = 2; i < n; i++) {
int s = a[i] - a[i - 1];
if (s < min) min = s;
}
cout << min << "\n";
t--;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
void solve(void) {
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
sort(arr, arr + n);
int res = 9999;
for (int i = 0; i < n - 1; i++) {
if (abs(arr[i] - arr[i + 1]) < res) {
res = abs(arr[i] - arr[i + 1]);
}
}
cout << res << endl;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
while (t--) {
solve();
}
}
| ### 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;
void solve(void) {
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
sort(arr, arr + n);
int res = 9999;
for (int i = 0; i < n - 1; i++) {
if (abs(arr[i] - arr[i + 1]) < res) {
res = abs(arr[i] - arr[i + 1]);
}
}
cout << res << endl;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
while (t--) {
solve();
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int arr[n];
int dif = INT_MAX;
for (int i = 0; i < n; i++) cin >> arr[i];
int locmin;
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
locmin = abs(arr[j] - arr[i]);
if (locmin < dif) {
dif = locmin;
}
}
}
cout << dif << 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() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int arr[n];
int dif = INT_MAX;
for (int i = 0; i < n; i++) cin >> arr[i];
int locmin;
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
locmin = abs(arr[j] - arr[i]);
if (locmin < dif) {
dif = locmin;
}
}
}
cout << dif << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
;
int t, n, i, x, min;
cin >> t;
while (t--) {
cin >> n;
int arr[n];
for (i = 0; i < n; i++) cin >> arr[i];
sort(arr, arr + n);
min = abs(arr[0] - arr[1]);
for (i = 1; i < n - 1; i++) {
x = arr[i + 1] - arr[i];
if (x < min) min = x;
}
cout << min << 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() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
;
int t, n, i, x, min;
cin >> t;
while (t--) {
cin >> n;
int arr[n];
for (i = 0; i < n; i++) cin >> arr[i];
sort(arr, arr + n);
min = abs(arr[0] - arr[1]);
for (i = 1; i < n - 1; i++) {
x = arr[i + 1] - arr[i];
if (x < min) min = x;
}
cout << min << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long int MOD = 1e9 + 7;
long long int power(long long int x, long long int y, long long int p) {
long long int res = 1;
x = x % p;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
long long int gcdExtended(long long int a, long long int b, long long int *x,
long long int *y);
long long int modInverse(long long int a, long long int m) {
long long int x, y;
long long int g = gcdExtended(a, m, &x, &y);
if (g != 1)
cout << "Inverse doesn't exist";
else {
int res = (x % m + m) % m;
return res;
}
}
long long int gcdExtended(long long int a, long long int b, long long int *x,
long long int *y) {
if (a == 0) {
*x = 0, *y = 1;
return b;
}
long long int x1, y1;
long long int gcd = gcdExtended(b % a, a, &x1, &y1);
*x = y1 - (b / a) * x1;
*y = x1;
return gcd;
}
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int s[n];
for (int i = 0; i < n; i++) {
cin >> s[i];
}
sort(s, s + n);
int mn = INT_MAX;
for (int i = 0; i < n - 1; i++) {
mn = min(mn, s[i + 1] - s[i]);
}
cout << mn << "\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;
const long long int MOD = 1e9 + 7;
long long int power(long long int x, long long int y, long long int p) {
long long int res = 1;
x = x % p;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
long long int gcdExtended(long long int a, long long int b, long long int *x,
long long int *y);
long long int modInverse(long long int a, long long int m) {
long long int x, y;
long long int g = gcdExtended(a, m, &x, &y);
if (g != 1)
cout << "Inverse doesn't exist";
else {
int res = (x % m + m) % m;
return res;
}
}
long long int gcdExtended(long long int a, long long int b, long long int *x,
long long int *y) {
if (a == 0) {
*x = 0, *y = 1;
return b;
}
long long int x1, y1;
long long int gcd = gcdExtended(b % a, a, &x1, &y1);
*x = y1 - (b / a) * x1;
*y = x1;
return gcd;
}
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int s[n];
for (int i = 0; i < n; i++) {
cin >> s[i];
}
sort(s, s + n);
int mn = INT_MAX;
for (int i = 0; i < n - 1; i++) {
mn = min(mn, s[i + 1] - s[i]);
}
cout << mn << "\n";
}
}
``` |
#include <bits/stdc++.h>
long long int power(long long int x, long long int n) {
long long int result = 1;
while (n) {
if (n % 2 == 1) result = result * x;
n = n / 2;
x = x * x;
}
return result;
}
long long int gcd(long long int a, long long int b) {
if (!b) return a;
return gcd(b, a % b);
}
long long int lcm(long long int a, long long int b) {
return (a * b) / gcd(a, b);
}
long long int BS(long long int a[], long long int s, long long int n,
long long int val) {
long long int mid, beg = s, end = n - 1;
while (beg <= end) {
mid = (beg + end) / 2;
if (val == a[mid]) {
break;
} else if (val > a[mid]) {
beg = mid + 1;
} else {
end = mid - 1;
}
}
return mid;
}
using namespace std;
void start() {}
int main() {
start();
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int t;
cin >> t;
while (t--) {
long long int n;
cin >> n;
long long int a[n];
for (long long int i = 0; i < n; i++) cin >> a[i];
sort(a, a + n);
long long int min = INT_MAX;
for (long long int i = 0; i < n - 1; i++) {
if (abs(a[i] - a[i + 1]) < min) min = abs(a[i] - a[i + 1]);
}
cout << min << "\n";
}
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>
long long int power(long long int x, long long int n) {
long long int result = 1;
while (n) {
if (n % 2 == 1) result = result * x;
n = n / 2;
x = x * x;
}
return result;
}
long long int gcd(long long int a, long long int b) {
if (!b) return a;
return gcd(b, a % b);
}
long long int lcm(long long int a, long long int b) {
return (a * b) / gcd(a, b);
}
long long int BS(long long int a[], long long int s, long long int n,
long long int val) {
long long int mid, beg = s, end = n - 1;
while (beg <= end) {
mid = (beg + end) / 2;
if (val == a[mid]) {
break;
} else if (val > a[mid]) {
beg = mid + 1;
} else {
end = mid - 1;
}
}
return mid;
}
using namespace std;
void start() {}
int main() {
start();
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int t;
cin >> t;
while (t--) {
long long int n;
cin >> n;
long long int a[n];
for (long long int i = 0; i < n; i++) cin >> a[i];
sort(a, a + n);
long long int min = INT_MAX;
for (long long int i = 0; i < n - 1; i++) {
if (abs(a[i] - a[i + 1]) < min) min = abs(a[i] - a[i + 1]);
}
cout << min << "\n";
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
for (size_t rq = 0; rq < t; ++rq) {
int n, s, ans = 0;
vector<int> vec;
cin >> n;
for (size_t i = 0; i < n; ++i) {
cin >> s;
vec.push_back(s);
}
sort(vec.begin(), vec.end());
ans = vec[n - 1] - vec[0];
for (size_t i = 0; i < n; ++i) {
for (size_t j = i + 1; j < n; ++j) {
ans = min(vec[j] - vec[i], ans);
}
}
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;
int main() {
int t;
cin >> t;
for (size_t rq = 0; rq < t; ++rq) {
int n, s, ans = 0;
vector<int> vec;
cin >> n;
for (size_t i = 0; i < n; ++i) {
cin >> s;
vec.push_back(s);
}
sort(vec.begin(), vec.end());
ans = vec[n - 1] - vec[0];
for (size_t i = 0; i < n; ++i) {
for (size_t j = i + 1; j < n; ++j) {
ans = min(vec[j] - vec[i], ans);
}
}
cout << ans << '\n';
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long fx[4] = {1, -1, 0, 0};
long long fy[4] = {0, 0, 1, -1};
bool sortinrev(const pair<int, int> &a, const pair<int, int> &b) {
return a.first > b.first;
}
long long pw(long long a, long long b) {
long long ans = 1;
for (long long i = 1; i <= b; ++i) ans = (ans * a);
return ans;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long t;
cin >> t;
while (t--) {
long long n;
cin >> n;
long long a[n + 1];
a[0] = 0;
for (int i = 1; i <= n; i++) cin >> a[i];
sort(a, a + n + 1);
long long ans = a[2] - a[1];
for (int i = 3; i <= n; i++) {
long long dif = abs(a[i] - a[i - 1]);
ans = min(ans, dif);
}
cout << ans << "\n";
}
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;
long long fx[4] = {1, -1, 0, 0};
long long fy[4] = {0, 0, 1, -1};
bool sortinrev(const pair<int, int> &a, const pair<int, int> &b) {
return a.first > b.first;
}
long long pw(long long a, long long b) {
long long ans = 1;
for (long long i = 1; i <= b; ++i) ans = (ans * a);
return ans;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long t;
cin >> t;
while (t--) {
long long n;
cin >> n;
long long a[n + 1];
a[0] = 0;
for (int i = 1; i <= n; i++) cin >> a[i];
sort(a, a + n + 1);
long long ans = a[2] - a[1];
for (int i = 3; i <= n; i++) {
long long dif = abs(a[i] - a[i - 1]);
ans = min(ans, dif);
}
cout << ans << "\n";
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long dij[10][10];
double pi = acos(-1);
long long a[1000010], b[1 << 20];
long long gcd(long long x, long long y) { return y ? gcd(y, x % y) : x; }
double C(long long n, long long m) {
if (m < 0 || m > n) return 0;
double ans = 1;
for (double i = 1; i <= m; i++) ans = ans * (n - m + i) / i;
return ans;
}
void gg() {
long long n, ans = 0x3f3f3f3f3f3f3f3f;
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
sort(a + 1, a + n + 1);
for (int i = 2; i <= n; i++) ans = min(ans, a[i] - a[i - 1]);
cout << ans << '\n';
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
long long n;
cin >> n;
while (n--) gg();
}
| ### 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;
long long dij[10][10];
double pi = acos(-1);
long long a[1000010], b[1 << 20];
long long gcd(long long x, long long y) { return y ? gcd(y, x % y) : x; }
double C(long long n, long long m) {
if (m < 0 || m > n) return 0;
double ans = 1;
for (double i = 1; i <= m; i++) ans = ans * (n - m + i) / i;
return ans;
}
void gg() {
long long n, ans = 0x3f3f3f3f3f3f3f3f;
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
sort(a + 1, a + n + 1);
for (int i = 2; i <= n; i++) ans = min(ans, a[i] - a[i - 1]);
cout << ans << '\n';
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
long long n;
cin >> n;
while (n--) gg();
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 7;
const int mod = 1e9 + 7;
int num[maxn];
int main() {
int t, n, m, ans;
scanf("%d", &t);
while (t--) {
ans = 1000000;
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d", &num[i]);
sort(num, num + n);
for (int i = 1; i < n; i++) {
ans = min(ans, num[i] - num[i - 1]);
}
printf("%d\n", ans);
}
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;
const int maxn = 1e5 + 7;
const int mod = 1e9 + 7;
int num[maxn];
int main() {
int t, n, m, ans;
scanf("%d", &t);
while (t--) {
ans = 1000000;
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d", &num[i]);
sort(num, num + n);
for (int i = 1; i < n; i++) {
ans = min(ans, num[i] - num[i - 1]);
}
printf("%d\n", ans);
}
return 0;
}
``` |
#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 = 1; i < n; 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 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 = 1; i < n; i++) {
result = min(result, abs(a[i] - a[i - 1]));
}
cout << result << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int 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];
}
sort(a, a + n);
long long min = a[1] - a[0], curr = 0;
for (long long i = 0; i < n - 1; i++) {
curr = a[i + 1] - a[i];
if (curr < min) min = curr;
}
cout << min << "\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() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int 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];
}
sort(a, a + n);
long long min = a[1] - a[0], curr = 0;
for (long long i = 0; i < n - 1; i++) {
curr = a[i + 1] - a[i];
if (curr < min) min = curr;
}
cout << min << "\n";
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
void program() {
int n, temp;
cin >> n;
vector<int> a;
for (int i = 0; i < n; i++) {
cin >> temp;
a.push_back(temp);
}
sort(a.begin(), a.end());
int dif1, flag, minimum = a[1] - a[0];
for (int i = 1; i < n - 1; i++) {
dif1 = a[i + 1] - a[i];
minimum = min(dif1, minimum);
}
cout << minimum << "\n";
}
int main() {
int t;
cin >> t;
while (t--) program();
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;
void program() {
int n, temp;
cin >> n;
vector<int> a;
for (int i = 0; i < n; i++) {
cin >> temp;
a.push_back(temp);
}
sort(a.begin(), a.end());
int dif1, flag, minimum = a[1] - a[0];
for (int i = 1; i < n - 1; i++) {
dif1 = a[i + 1] - a[i];
minimum = min(dif1, minimum);
}
cout << minimum << "\n";
}
int main() {
int t;
cin >> t;
while (t--) program();
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int T;
cin >> T;
for (int i = 1; i <= T; i++) {
int n;
cin >> n;
vector<int> v;
for (int j = 1; j <= n; j++) {
int x;
cin >> x;
v.push_back(x);
}
sort(v.begin(), v.end());
int ans = v[1] - v[0];
for (int j = 2; j < n; j++) {
ans = min(ans, v[j] - v[j - 1]);
}
cout << ans << "\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;
for (int i = 1; i <= T; i++) {
int n;
cin >> n;
vector<int> v;
for (int j = 1; j <= n; j++) {
int x;
cin >> x;
v.push_back(x);
}
sort(v.begin(), v.end());
int ans = v[1] - v[0];
for (int j = 2; j < n; j++) {
ans = min(ans, v[j] - v[j - 1]);
}
cout << ans << "\n";
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
void solve() {
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++) {
if (a[i + 1] - a[i] < m) {
m = a[i + 1] - a[i];
}
}
cout << m << endl;
}
int main() {
int t;
cin >> t;
for (int i = 0; i < t; i++) {
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 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++) {
if (a[i + 1] - a[i] < m) {
m = a[i + 1] - a[i];
}
}
cout << m << endl;
}
int main() {
int t;
cin >> t;
for (int i = 0; i < t; i++) {
solve();
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t, a, b, c, d, e, f;
vector<int> v;
cin >> t;
for (a = 0; a < t; a++) {
cin >> b;
f = 999;
for (c = 0; c < b; c++) {
cin >> d;
v.push_back(d);
}
sort(v.begin(), v.end());
for (c = b - 1; c > 0; c--) {
e = v[c] - v[c - 1];
if (e < f) {
f = e;
}
}
cout << f << endl;
v.clear();
}
}
| ### 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, a, b, c, d, e, f;
vector<int> v;
cin >> t;
for (a = 0; a < t; a++) {
cin >> b;
f = 999;
for (c = 0; c < b; c++) {
cin >> d;
v.push_back(d);
}
sort(v.begin(), v.end());
for (c = b - 1; c > 0; c--) {
e = v[c] - v[c - 1];
if (e < f) {
f = e;
}
}
cout << f << endl;
v.clear();
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long int mod = 1000000000 + 7;
long long int mul(long long int a, long long int b) {
return ((a % mod) * (b % mod)) % mod;
}
long long int add(long long int a, long long int b) {
return ((a % mod) + (b % mod)) % mod;
}
void solve() {
int n;
cin >> n;
vector<int> A(n);
for (int i = 0; i < n; i++) {
cin >> A[i];
}
int dif = INT_MAX;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
dif = min(dif, abs(A[i] - A[j]));
}
}
cout << dif << '\n';
return;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin >> t;
while (t--) {
solve();
}
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 long long int mod = 1000000000 + 7;
long long int mul(long long int a, long long int b) {
return ((a % mod) * (b % mod)) % mod;
}
long long int add(long long int a, long long int b) {
return ((a % mod) + (b % mod)) % mod;
}
void solve() {
int n;
cin >> n;
vector<int> A(n);
for (int i = 0; i < n; i++) {
cin >> A[i];
}
int dif = INT_MAX;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
dif = min(dif, abs(A[i] - A[j]));
}
}
cout << dif << '\n';
return;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sort(a.begin(), a.end());
int sub1 = a[n - 1] - a[0];
int i = 0, check = 0;
while (i < n - 1 && check == 0) {
int sub = a[i + 1] - a[i];
if (sub1 > sub) {
sub1 = sub;
if (sub1 == 0) {
check = 1;
}
}
i++;
}
cout << sub1 << "\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 t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sort(a.begin(), a.end());
int sub1 = a[n - 1] - a[0];
int i = 0, check = 0;
while (i < n - 1 && check == 0) {
int sub = a[i + 1] - a[i];
if (sub1 > sub) {
sub1 = sub;
if (sub1 == 0) {
check = 1;
}
}
i++;
}
cout << sub1 << "\n";
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, x;
int size;
int min;
vector<int> num;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> size;
for (int j = 0; j < size; j++) {
cin >> x;
num.push_back(x);
}
sort(num.begin(), num.end());
min = abs(num[1] - num[0]);
for (int j = 0; j < size; j++) {
if (min > abs(num[j] - num[j - 1])) min = abs(num[j] - num[j - 1]);
}
cout << min << endl;
num.clear();
}
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 n, x;
int size;
int min;
vector<int> num;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> size;
for (int j = 0; j < size; j++) {
cin >> x;
num.push_back(x);
}
sort(num.begin(), num.end());
min = abs(num[1] - num[0]);
for (int j = 0; j < size; j++) {
if (min > abs(num[j] - num[j - 1])) min = abs(num[j] - num[j - 1]);
}
cout << min << endl;
num.clear();
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n, x, m = 1001;
cin >> n;
vector<int> v;
for (int i = 0; i < n; i++) {
cin >> x;
v.push_back(x);
}
sort(v.begin(), v.end());
for (int i = 0; i < n - 1; i++) m = min(m, v[i + 1] - v[i]);
cout << m << 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, x, m = 1001;
cin >> n;
vector<int> v;
for (int i = 0; i < n; i++) {
cin >> x;
v.push_back(x);
}
sort(v.begin(), v.end());
for (int i = 0; i < n - 1; i++) m = min(m, v[i + 1] - v[i]);
cout << m << endl;
}
}
``` |
#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 min = 1000;
int diff[n - 1];
for (int i = 0; i < n - 1; i++) diff[i] = a[i + 1] - a[i];
for (int i = 0; i < n - 1; i++) {
if (min > diff[i]) min = diff[i];
}
cout << min << "\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];
for (int i = 0; i < n; i++) cin >> a[i];
sort(a, a + n);
int min = 1000;
int diff[n - 1];
for (int i = 0; i < n - 1; i++) diff[i] = a[i + 1] - a[i];
for (int i = 0; i < n - 1; i++) {
if (min > diff[i]) min = diff[i];
}
cout << min << "\n";
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t, n, s[55];
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d", &s[i]);
sort(s, s + n);
int ans = 2000;
for (int i = 1; i < n; i++) {
if (s[i] - s[i - 1] < ans) ans = s[i] - s[i - 1];
}
printf("%d\n", ans);
}
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() {
int t, n, s[55];
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d", &s[i]);
sort(s, s + n);
int ans = 2000;
for (int i = 1; i < n; i++) {
if (s[i] - s[i - 1] < ans) ans = s[i] - s[i - 1];
}
printf("%d\n", ans);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int dx[] = {0, 1};
int dy[] = {1, 0};
const long double Pi = acos(-1), e = 2.718;
const int N = 1e6 + 10, mod = 1e9 + 7;
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int T;
cin >> T;
while (T--) {
int n;
cin >> n;
vector<int> a(n);
for (auto &x : a) cin >> x;
sort(a.begin(), a.end());
int mn = 1e9;
for (int i = 0; i < n - 1; i++) {
mn = min(mn, a[i + 1] - a[i]);
}
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 dx[] = {0, 1};
int dy[] = {1, 0};
const long double Pi = acos(-1), e = 2.718;
const int N = 1e6 + 10, mod = 1e9 + 7;
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int T;
cin >> T;
while (T--) {
int n;
cin >> n;
vector<int> a(n);
for (auto &x : a) cin >> x;
sort(a.begin(), a.end());
int mn = 1e9;
for (int i = 0; i < n - 1; i++) {
mn = min(mn, a[i + 1] - a[i]);
}
cout << mn << '\n';
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
void merge(int *array, int a, int b, int c) {
int x = c + 50;
int y = a;
int z = b;
int subray[x];
for (int i = a; i <= c; i++) {
if ((array[y] > array[z] && y < b) || z > c) {
subray[i] = array[y];
y++;
} else {
subray[i] = array[z];
z++;
}
}
for (int i = a; i <= c; i++) array[i] = subray[i];
}
void mergesort(int *array, int x, int y) {
if (x < y) {
int a = (x + y) / 2;
mergesort(array, x, a);
mergesort(array, a + 1, y);
merge(array, x, a + 1, y);
}
}
int main() {
int t, n, x;
int array[100];
cin >> t;
for (int i = 1; i <= t; i++) {
cin >> n;
x = 2000;
for (int k = 1; k <= n; k++) {
cin >> array[k];
}
mergesort(array, 1, n);
for (int k = 1; k < n; k++) x = min(x, array[k] - array[k + 1]);
cout << x << 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;
void merge(int *array, int a, int b, int c) {
int x = c + 50;
int y = a;
int z = b;
int subray[x];
for (int i = a; i <= c; i++) {
if ((array[y] > array[z] && y < b) || z > c) {
subray[i] = array[y];
y++;
} else {
subray[i] = array[z];
z++;
}
}
for (int i = a; i <= c; i++) array[i] = subray[i];
}
void mergesort(int *array, int x, int y) {
if (x < y) {
int a = (x + y) / 2;
mergesort(array, x, a);
mergesort(array, a + 1, y);
merge(array, x, a + 1, y);
}
}
int main() {
int t, n, x;
int array[100];
cin >> t;
for (int i = 1; i <= t; i++) {
cin >> n;
x = 2000;
for (int k = 1; k <= n; k++) {
cin >> array[k];
}
mergesort(array, 1, n);
for (int k = 1; k < n; k++) x = min(x, array[k] - array[k + 1]);
cout << x << endl;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
long long int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
int diff = 0, min = 1000000000;
sort(arr, arr + n);
for (int i = 1; i < n; i++) {
diff = abs(arr[i - 1] - arr[i]);
if (diff < min) min = diff;
}
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);
int t;
cin >> t;
while (t--) {
long long int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
int diff = 0, min = 1000000000;
sort(arr, arr + n);
for (int i = 1; i < n; i++) {
diff = abs(arr[i - 1] - arr[i]);
if (diff < min) min = diff;
}
cout << min << "\n";
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int mina(int p) {
int a[p];
for (int j = 0; j < p; j++) {
cin >> a[j];
}
sort(a, a + p);
int minb = (int)1e9;
for (int k = 0; k < p - 1; k++) {
if (a[k + 1] - a[k] < minb) {
minb = a[k + 1] - a[k];
}
}
return minb;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin >> t;
int n, r;
for (int i = 0; i < t; i++) {
cin >> n;
r = mina(n);
cout << r << "\n";
}
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 mina(int p) {
int a[p];
for (int j = 0; j < p; j++) {
cin >> a[j];
}
sort(a, a + p);
int minb = (int)1e9;
for (int k = 0; k < p - 1; k++) {
if (a[k + 1] - a[k] < minb) {
minb = a[k + 1] - a[k];
}
}
return minb;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin >> t;
int n, r;
for (int i = 0; i < t; i++) {
cin >> n;
r = mina(n);
cout << r << "\n";
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
void printarray(int arr[], int n) {
for (int i = 0; i < n; i++) cout << arr[i] << " ";
cout << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cerr.tie(0);
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 m = arr[n - 1];
for (int j = 0; j < n - 1; j++) {
m = min(abs(arr[j] - arr[j + 1]), m);
if (m == 0) break;
}
cout << m << "\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;
void printarray(int arr[], int n) {
for (int i = 0; i < n; i++) cout << arr[i] << " ";
cout << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cerr.tie(0);
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 m = arr[n - 1];
for (int j = 0; j < n - 1; j++) {
m = min(abs(arr[j] - arr[j + 1]), m);
if (m == 0) break;
}
cout << m << "\n";
}
return 0;
}
``` |
#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];
for (int i = 0; i < n; i++) {
for (int j = 0; j < (n - i - 1); j++) {
if (a[j] > a[j + 1]) {
int x = a[j];
a[j] = a[j + 1];
a[j + 1] = x;
}
}
}
int min = INT_MAX;
for (int i = n - 1; i > 0; i--) {
int diff = a[i] - a[i - 1];
if (diff < min) min = diff;
}
cout << min << "\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];
for (int i = 0; i < n; i++) {
for (int j = 0; j < (n - i - 1); j++) {
if (a[j] > a[j + 1]) {
int x = a[j];
a[j] = a[j + 1];
a[j + 1] = x;
}
}
}
int min = INT_MAX;
for (int i = n - 1; i > 0; i--) {
int diff = a[i] - a[i - 1];
if (diff < min) min = diff;
}
cout << min << "\n";
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> v(n);
for (auto &z : v) cin >> z;
sort(v.begin(), v.end());
int res = 1e9;
for (int i = 0; i < n - 1; ++i) {
res = min(res, abs(v[i] - v[i + 1]));
}
cout << res << '\n';
}
}
| ### 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;
cin >> n;
vector<int> v(n);
for (auto &z : v) cin >> z;
sort(v.begin(), v.end());
int res = 1e9;
for (int i = 0; i < n - 1; ++i) {
res = min(res, abs(v[i] - v[i + 1]));
}
cout << res << '\n';
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int test;
cin >> test;
while (test--) {
long long int num;
cin >> num;
long long int arr[num + 1];
for (long long int i = 0; i < num; i++) cin >> arr[i];
sort(arr, arr + num);
long long int ans = arr[1] - arr[0];
for (long long int i = 2; i <= num; i++) {
ans = min(ans, (arr[i] - arr[i - 1]));
}
cout << ans << endl;
}
}
| ### 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() {
long long int test;
cin >> test;
while (test--) {
long long int num;
cin >> num;
long long int arr[num + 1];
for (long long int i = 0; i < num; i++) cin >> arr[i];
sort(arr, arr + num);
long long int ans = arr[1] - arr[0];
for (long long int i = 2; i <= num; i++) {
ans = min(ans, (arr[i] - arr[i - 1]));
}
cout << ans << endl;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int getMinDiff(int arr[], int n) {
int freq[100001] = {0};
for (int i = 0; i < n; i++) {
freq[arr[i]]++;
if (freq[arr[i]] > 1) return 0;
}
int mn = INT_MAX;
for (int i = 0; i < 100001; i++) {
if (freq[i] > 0) {
i++;
int cnt = 1;
while ((freq[i] == 0) && (i != 100001 - 1)) {
cnt++;
i++;
}
mn = min(cnt, mn);
i--;
}
}
return mn;
}
int main() {
int t;
cin >> t;
for (int i = 0; i < t; i++) {
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
int mine = arr[1] - arr[0];
cout << getMinDiff(arr, n) << 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 getMinDiff(int arr[], int n) {
int freq[100001] = {0};
for (int i = 0; i < n; i++) {
freq[arr[i]]++;
if (freq[arr[i]] > 1) return 0;
}
int mn = INT_MAX;
for (int i = 0; i < 100001; i++) {
if (freq[i] > 0) {
i++;
int cnt = 1;
while ((freq[i] == 0) && (i != 100001 - 1)) {
cnt++;
i++;
}
mn = min(cnt, mn);
i--;
}
}
return mn;
}
int main() {
int t;
cin >> t;
for (int i = 0; i < t; i++) {
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
int mine = arr[1] - arr[0];
cout << getMinDiff(arr, n) << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int t;
cin >> t;
while (t--) {
int n;
cin >> n;
long long int a[n + 1];
for (long long int i = 0; i < n; i++) {
cin >> a[i];
}
sort(a, a + n);
long long int res = a[n - 1];
long long int m;
for (long long int i = 0; i < n - 1; i++) {
m = a[i + 1] - a[i];
if (res > m) res = min(res, m);
}
cout << res << 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 main() {
long long int t;
cin >> t;
while (t--) {
int n;
cin >> n;
long long int a[n + 1];
for (long long int i = 0; i < n; i++) {
cin >> a[i];
}
sort(a, a + n);
long long int res = a[n - 1];
long long int m;
for (long long int i = 0; i < n - 1; i++) {
m = a[i + 1] - a[i];
if (res > m) res = min(res, m);
}
cout << res << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n = 0;
cin >> n;
vector<int> v(n);
for (int i = 0; i < v.size(); i++) cin >> v[i];
sort(v.begin(), v.end());
vector<int> ans(n);
ans[0] = INT_MAX;
for (int i = 1; i < n; i++) {
ans[i] = v[i] - v[i - 1];
}
cout << *min_element(ans.begin(), ans.end()) << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int tc = 0;
cin >> tc;
while (tc--) {
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 = 0;
cin >> n;
vector<int> v(n);
for (int i = 0; i < v.size(); i++) cin >> v[i];
sort(v.begin(), v.end());
vector<int> ans(n);
ans[0] = INT_MAX;
for (int i = 1; i < n; i++) {
ans[i] = v[i] - v[i - 1];
}
cout << *min_element(ans.begin(), ans.end()) << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int tc = 0;
cin >> tc;
while (tc--) {
solve();
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int tc;
cin >> tc;
while (tc--) {
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
sort(arr, arr + n);
int minn = INT_MAX;
for (int i = 0; i < n; i++) {
if (i == 0) {
minn = min(minn, abs(arr[i] - arr[i + 1]));
continue;
}
if (i == (n - 1)) {
minn = min(minn, abs(arr[i] - arr[i - 1]));
continue;
}
minn = min(minn, min(abs(arr[i] - arr[i - 1]), abs(arr[i] - arr[i + 1])));
}
cout << minn << 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() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int tc;
cin >> tc;
while (tc--) {
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
sort(arr, arr + n);
int minn = INT_MAX;
for (int i = 0; i < n; i++) {
if (i == 0) {
minn = min(minn, abs(arr[i] - arr[i + 1]));
continue;
}
if (i == (n - 1)) {
minn = min(minn, abs(arr[i] - arr[i - 1]));
continue;
}
minn = min(minn, min(abs(arr[i] - arr[i - 1]), abs(arr[i] - arr[i + 1])));
}
cout << minn << endl;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n, g, a;
cin >> n;
int s[n];
for (int i = 0; i < n; i++) {
cin >> s[i];
}
a = max(s[0], s[n - 1]);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i != j) {
g = abs(s[i] - s[j]);
if (a > g) {
a = g;
}
}
}
}
cout << abs(a) << 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, g, a;
cin >> n;
int s[n];
for (int i = 0; i < n; i++) {
cin >> s[i];
}
a = max(s[0], s[n - 1]);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i != j) {
g = abs(s[i] - s[j]);
if (a > g) {
a = g;
}
}
}
}
cout << abs(a) << endl;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n;
cin >> n;
vector<int> v;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
v.push_back(x);
}
sort(v.begin(), v.end());
int diff = INT_MAX;
for (int i = 0; i < n - 1; i++) {
diff = min(diff, v[i + 1] - v[i]);
}
cout << diff;
}
int main() {
int t;
cin >> t;
while (t--) {
solve();
cout << "\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;
void solve() {
int n;
cin >> n;
vector<int> v;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
v.push_back(x);
}
sort(v.begin(), v.end());
int diff = INT_MAX;
for (int i = 0; i < n - 1; i++) {
diff = min(diff, v[i + 1] - v[i]);
}
cout << diff;
}
int main() {
int t;
cin >> t;
while (t--) {
solve();
cout << "\n";
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int k, A[10000];
cin >> k;
for (int t = 1; t <= k; t++) {
int n;
int ans = INT_MAX;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> A[i];
}
sort(A + 1, A + 1 + n);
for (int i = 1; i < n; i++) {
ans = min(ans, A[i + 1] - A[i]);
}
cout << ans << "\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() {
int k, A[10000];
cin >> k;
for (int t = 1; t <= k; t++) {
int n;
int ans = INT_MAX;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> A[i];
}
sort(A + 1, A + 1 + n);
for (int i = 1; i < n; i++) {
ans = min(ans, A[i + 1] - A[i]);
}
cout << ans << "\n";
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int s[n + 1];
for (int i = 1; i <= n; cin >> s[i], ++i)
;
sort(s + 1, s + n + 1);
for (int i = n; i >= 2; --i) s[i] -= s[i - 1];
int res = 0x3f3f3f3f;
for (int i = 2; i <= n; ++i) res = min(res, s[i]);
cout << res << '\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 main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int s[n + 1];
for (int i = 1; i <= n; cin >> s[i], ++i)
;
sort(s + 1, s + n + 1);
for (int i = n; i >= 2; --i) s[i] -= s[i - 1];
int res = 0x3f3f3f3f;
for (int i = 2; i <= n; ++i) res = min(res, s[i]);
cout << res << '\n';
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, k, asdasjkd, adsnamsd, a[101], mna, mnb, b[101];
int ans;
int main() {
int t23 = 1;
cin >> t23;
for (int qwe = 1; qwe <= t23; qwe++) {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
ans = 1e9;
sort(a + 1, a + n + 1);
for (int i = 2; i <= n; i++) ans = min(ans, a[i] - a[i - 1]);
cout << ans << 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 n, k, asdasjkd, adsnamsd, a[101], mna, mnb, b[101];
int ans;
int main() {
int t23 = 1;
cin >> t23;
for (int qwe = 1; qwe <= t23; qwe++) {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
ans = 1e9;
sort(a + 1, a + n + 1);
for (int i = 2; i <= n; i++) ans = min(ans, a[i] - a[i - 1]);
cout << ans << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t, n;
cin >> t;
for (int i = 0; i < t; i++) {
cin >> n;
vector<int> a;
for (int j = 0; j < n; j++) {
int k;
cin >> k;
a.push_back(k);
}
sort(a.begin(), a.end());
int min = 10000;
for (int j = 0; j < n - 1; j++) {
if (a[j + 1] - a[j] < min) {
min = a[j + 1] - a[j];
}
}
cout << min << 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, n;
cin >> t;
for (int i = 0; i < t; i++) {
cin >> n;
vector<int> a;
for (int j = 0; j < n; j++) {
int k;
cin >> k;
a.push_back(k);
}
sort(a.begin(), a.end());
int min = 10000;
for (int j = 0; j < n - 1; j++) {
if (a[j + 1] - a[j] < min) {
min = a[j + 1] - a[j];
}
}
cout << min << endl;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int TN = 1;
void sol() {
long long n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
sort(a.begin(), a.end());
long long ans = 1e18;
for (int i = 1; i < n; ++i) {
ans = min(ans, a[i] - a[i - 1]);
}
cout << ans << '\n';
}
int main() {
cin >> TN;
while (TN--) {
sol();
}
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 TN = 1;
void sol() {
long long n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
sort(a.begin(), a.end());
long long ans = 1e18;
for (int i = 1; i < n; ++i) {
ans = min(ans, a[i] - a[i - 1]);
}
cout << ans << '\n';
}
int main() {
cin >> TN;
while (TN--) {
sol();
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int t, n, a[10000];
int main() {
int t = 1;
cin >> t;
while (t--) {
cin >> n;
int ans = 1000000;
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 << "\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 t, n, a[10000];
int main() {
int t = 1;
cin >> t;
while (t--) {
cin >> n;
int ans = 1000000;
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 << "\n";
}
}
``` |
#include <bits/stdc++.h>
int main() {
int t;
scanf("%d", &t);
while (t--) {
int n;
scanf("%d", &n);
int a[n];
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (a[j + 1] < a[j]) {
int t = a[j];
a[j] = a[j + 1];
a[j + 1] = t;
}
}
}
int ans = INT_MAX;
for (int i = 0; i < n - 1; i++) {
if (a[i + 1] - a[i] < ans) {
ans = a[i + 1] - a[i];
if (ans == 0) break;
}
}
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>
int main() {
int t;
scanf("%d", &t);
while (t--) {
int n;
scanf("%d", &n);
int a[n];
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (a[j + 1] < a[j]) {
int t = a[j];
a[j] = a[j + 1];
a[j + 1] = t;
}
}
}
int ans = INT_MAX;
for (int i = 0; i < n - 1; i++) {
if (a[i + 1] - a[i] < ans) {
ans = a[i + 1] - a[i];
if (ans == 0) break;
}
}
printf("%d\n", ans);
}
return 0;
}
``` |
#include <bits/stdc++.h>
const double pi = 3.14159265358979323846;
using namespace std;
int main() {
int t, n;
cin >> t;
while (t--) {
cin >> n;
int* arr = new int[n];
set<int> s;
for (int i = 0; i < n; i++) {
cin >> arr[i];
s.insert(arr[i]);
}
sort(arr, arr + n);
int best = INT32_MAX;
for (int i = 1; i < n; i++) {
best = min(best, arr[i] - arr[i - 1]);
}
cout << best << endl;
delete[] arr;
}
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>
const double pi = 3.14159265358979323846;
using namespace std;
int main() {
int t, n;
cin >> t;
while (t--) {
cin >> n;
int* arr = new int[n];
set<int> s;
for (int i = 0; i < n; i++) {
cin >> arr[i];
s.insert(arr[i]);
}
sort(arr, arr + n);
int best = INT32_MAX;
for (int i = 1; i < n; i++) {
best = min(best, arr[i] - arr[i - 1]);
}
cout << best << endl;
delete[] arr;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long mod = 998244353;
int main() {
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];
sort(a, a + n);
long long min = a[n - 1];
for (long long i = 0; i < n - 1; i++) {
if (min > (a[i + 1] - a[i])) min = (a[i + 1] - a[i]);
}
cout << min << "\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;
long long mod = 998244353;
int main() {
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];
sort(a, a + n);
long long min = a[n - 1];
for (long long i = 0; i < n - 1; i++) {
if (min > (a[i + 1] - a[i])) min = (a[i + 1] - a[i]);
}
cout << min << "\n";
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
long long n;
cin >> n;
vector<long long> vec;
while (n--) {
long long a;
cin >> a;
vec.push_back(a);
}
sort(vec.begin(), vec.end());
long long d = LLONG_MAX;
for (int i = 0; i < vec.size() - 1; i++) {
if (vec[i + 1] - vec[i] < d) {
d = vec[i + 1] - vec[i];
}
}
cout << d << 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() {
int t;
cin >> t;
while (t--) {
long long n;
cin >> n;
vector<long long> vec;
while (n--) {
long long a;
cin >> a;
vec.push_back(a);
}
sort(vec.begin(), vec.end());
long long d = LLONG_MAX;
for (int i = 0; i < vec.size() - 1; i++) {
if (vec[i + 1] - vec[i] < d) {
d = vec[i + 1] - vec[i];
}
}
cout << d << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(!cin.tie(0));
long long int t;
cin >> t;
for (long long int p = 1; p <= t; p++) {
long long int n;
cin >> n;
vector<long long int> a(n);
vector<long long int> diff(n - 1);
for (long long int i = 0; i < n; i++) {
cin >> a[i];
}
sort(a.begin(), a.end());
long long int min = 100000;
long long int imin = 0;
for (long long int i = 0; i < n - 1; i++) {
diff[i] = a[i + 1] - a[i];
if (diff[i] < min) {
min = diff[i];
imin = i;
}
}
cout << min << 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 main() {
ios_base::sync_with_stdio(!cin.tie(0));
long long int t;
cin >> t;
for (long long int p = 1; p <= t; p++) {
long long int n;
cin >> n;
vector<long long int> a(n);
vector<long long int> diff(n - 1);
for (long long int i = 0; i < n; i++) {
cin >> a[i];
}
sort(a.begin(), a.end());
long long int min = 100000;
long long int imin = 0;
for (long long int i = 0; i < n - 1; i++) {
diff[i] = a[i + 1] - a[i];
if (diff[i] < min) {
min = diff[i];
imin = i;
}
}
cout << min << endl;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e9 + 7;
const long long mod = 998244353;
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long T;
cin >> T;
while (T--) {
long long n;
cin >> n;
long long s[n];
long long i, j, l;
for (i = 0; i < n; i++) {
cin >> s[i];
}
sort(s, s + n, greater<long long>());
long long diff = INF;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if (i != j) {
long long cl = abs(s[j] - s[i]);
diff = min(diff, cl);
}
}
}
cout << diff << "\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;
const long long INF = 1e9 + 7;
const long long mod = 998244353;
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long T;
cin >> T;
while (T--) {
long long n;
cin >> n;
long long s[n];
long long i, j, l;
for (i = 0; i < n; i++) {
cin >> s[i];
}
sort(s, s + n, greater<long long>());
long long diff = INF;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if (i != j) {
long long cl = abs(s[j] - s[i]);
diff = min(diff, cl);
}
}
}
cout << diff << "\n";
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAX = 2e5 + 5;
long long gcd(long long x, long long y) { return y == 0 ? x : gcd(y, x % y); }
int s[MAX];
int main() {
std::ios::sync_with_stdio(false);
int T;
int n;
cin >> T;
while (T--) {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> s[i];
}
sort(s, s + n);
int sum = 10000;
for (int i = 0; i < n - 1; i++) {
sum = min(sum, s[i + 1] - s[i]);
}
cout << sum << 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;
const int INF = 0x3f3f3f3f;
const int MAX = 2e5 + 5;
long long gcd(long long x, long long y) { return y == 0 ? x : gcd(y, x % y); }
int s[MAX];
int main() {
std::ios::sync_with_stdio(false);
int T;
int n;
cin >> T;
while (T--) {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> s[i];
}
sort(s, s + n);
int sum = 10000;
for (int i = 0; i < n - 1; i++) {
sum = min(sum, s[i + 1] - s[i]);
}
cout << sum << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
using namespace std;
const long long MOD = 1e+9 + 7;
const long long INF = 0x7f7f7f7f7f7f7f7f;
const int INFi = 0x7f7f7f7f;
const long long MAXN = 1e+5 + 7;
vector<long long> adj[MAXN];
long long visit[MAXN] = {};
int dx8[] = {0, 1, 1, 1, 0, -1, -1, -1}, dy8[] = {1, 1, 0, -1, -1, -1, 0, 1};
int dx4[] = {0, 1, 0, -1}, dy4[] = {1, 0, -1, 0};
int t = 1;
long long a[100];
long long n;
void MAIN() {
cin >> n;
for (long long i = 0; i < n; i++) cin >> a[i];
sort(a, a + n);
long long min1 = 1e+18;
for (long long i = 0; i < n; i++) {
if (i == 0)
min1 = min(abs(a[i] - a[i + 1]), min1);
else if (i == n - 1)
min1 = min(abs(a[i] - a[i - 1]), min1);
else {
min1 = min(abs(a[i] - a[i + 1]), min1);
min1 = min(abs(a[i] - a[i - 1]), min1);
}
}
cout << min1 << endl;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cout << fixed;
cout << setprecision(10);
;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
;
cin >> t;
while (t--) {
MAIN();
}
}
| ### 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>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
using namespace std;
const long long MOD = 1e+9 + 7;
const long long INF = 0x7f7f7f7f7f7f7f7f;
const int INFi = 0x7f7f7f7f;
const long long MAXN = 1e+5 + 7;
vector<long long> adj[MAXN];
long long visit[MAXN] = {};
int dx8[] = {0, 1, 1, 1, 0, -1, -1, -1}, dy8[] = {1, 1, 0, -1, -1, -1, 0, 1};
int dx4[] = {0, 1, 0, -1}, dy4[] = {1, 0, -1, 0};
int t = 1;
long long a[100];
long long n;
void MAIN() {
cin >> n;
for (long long i = 0; i < n; i++) cin >> a[i];
sort(a, a + n);
long long min1 = 1e+18;
for (long long i = 0; i < n; i++) {
if (i == 0)
min1 = min(abs(a[i] - a[i + 1]), min1);
else if (i == n - 1)
min1 = min(abs(a[i] - a[i - 1]), min1);
else {
min1 = min(abs(a[i] - a[i + 1]), min1);
min1 = min(abs(a[i] - a[i - 1]), min1);
}
}
cout << min1 << endl;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cout << fixed;
cout << setprecision(10);
;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
;
cin >> t;
while (t--) {
MAIN();
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
vector<int> a;
int main() {
ios_base::sync_with_stdio(false);
int q, small;
cin >> q;
while (q--) {
int n;
cin >> n;
a.clear();
a.resize(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sort(a.begin(), a.end());
small = 999;
for (int i = n - 1; i >= 1; i--) {
small = min(small, a[i] - a[i - 1]);
}
cout << small << 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;
vector<int> a;
int main() {
ios_base::sync_with_stdio(false);
int q, small;
cin >> q;
while (q--) {
int n;
cin >> n;
a.clear();
a.resize(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sort(a.begin(), a.end());
small = 999;
for (int i = n - 1; i >= 1; i--) {
small = min(small, a[i] - a[i - 1]);
}
cout << small << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sort(a.begin(), a.end());
int ans = 1E5;
for (int i = 1; i < n; i++) {
ans = min(ans, a[i] - a[i - 1]);
}
cout << ans << 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;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sort(a.begin(), a.end());
int ans = 1E5;
for (int i = 1; i < n; i++) {
ans = min(ans, a[i] - a[i - 1]);
}
cout << ans << endl;
}
return 0;
}
``` |
#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 min = INT_MAX, ans;
for (int i = 0; i < n - 1; i++) {
if ((a[i + 1] - a[i]) < min) {
ans = a[i + 1] - a[i];
min = ans;
}
}
cout << ans << endl;
}
}
| ### 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;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) cin >> a[i];
sort(a, a + n);
int min = INT_MAX, ans;
for (int i = 0; i < n - 1; i++) {
if ((a[i + 1] - a[i]) < min) {
ans = a[i + 1] - a[i];
min = ans;
}
}
cout << ans << endl;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
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());
vector<int> diff(n - 1);
int ind = 0;
for (int i = 0; i < n - 1; ++i) {
diff[ind++] = abs(a[i + 1] - a[i]);
}
sort(diff.begin(), diff.end());
cout << diff[0] << '\n';
}
int main(int argc, char const *argv[]) {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
solve();
}
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;
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());
vector<int> diff(n - 1);
int ind = 0;
for (int i = 0; i < n - 1; ++i) {
diff[ind++] = abs(a[i + 1] - a[i]);
}
sort(diff.begin(), diff.end());
cout << diff[0] << '\n';
}
int main(int argc, char const *argv[]) {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
for (int l = 0; l < t; ++l) {
int n;
cin >> n;
int mn = 1000;
int a[n];
for (int i = 0; i < n; ++i) cin >> a[i];
sort(a, a + n);
for (int i = 1; i < n; ++i) mn = min(abs(a[i] - a[i - 1]), mn);
cout << mn << '\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;
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
for (int l = 0; l < t; ++l) {
int n;
cin >> n;
int mn = 1000;
int a[n];
for (int i = 0; i < n; ++i) cin >> a[i];
sort(a, a + n);
for (int i = 1; i < n; ++i) mn = min(abs(a[i] - a[i - 1]), mn);
cout << mn << '\n';
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long t, n, b, k, i, c;
cin >> t;
while (t--) {
cin >> n;
long long a[n];
for (i = 0; i < n; i++) cin >> a[i];
sort(a, a + n);
c = a[n - 1] - a[0];
for (i = 1; i < n; i++) {
if (a[i] - a[i - 1] < c) c = a[i] - a[i - 1];
}
cout << c << 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() {
long long t, n, b, k, i, c;
cin >> t;
while (t--) {
cin >> n;
long long a[n];
for (i = 0; i < n; i++) cin >> a[i];
sort(a, a + n);
c = a[n - 1] - a[0];
for (i = 1; i < n; i++) {
if (a[i] - a[i - 1] < c) c = a[i] - a[i - 1];
}
cout << c << endl;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int tests(void);
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
int test;
cin >> test;
while (test--) {
tests();
}
return 0;
}
int tests() {
int n, mini = INT_MAX, sum;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sort(a, a + n);
for (int i = 0; i < n; i++) {
if (i != 0) {
sum = abs(a[i] - a[i - 1]);
if (sum < mini) {
mini = sum;
}
}
}
cout << mini << "\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 tests(void);
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
int test;
cin >> test;
while (test--) {
tests();
}
return 0;
}
int tests() {
int n, mini = INT_MAX, sum;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sort(a, a + n);
for (int i = 0; i < n; i++) {
if (i != 0) {
sum = abs(a[i] - a[i - 1]);
if (sum < mini) {
mini = sum;
}
}
}
cout << mini << "\n";
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;
vector<long long int> vec(n);
vector<long long int> v;
for (long long int i = 0; i < n; i++) cin >> vec[i];
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
int x = abs(vec[i] - vec[j]);
v.push_back(x);
}
}
sort(v.begin(), v.end());
cout << v[0] << 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;
vector<long long int> vec(n);
vector<long long int> v;
for (long long int i = 0; i < n; i++) cin >> vec[i];
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
int x = abs(vec[i] - vec[j]);
v.push_back(x);
}
}
sort(v.begin(), v.end());
cout << v[0] << endl;
}
}
``` |
#include <bits/stdc++.h>
#pragma GCC target( \
"sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#pragma GCC optimize("unroll-loops")
using namespace std;
const long double EPS = 0.001;
const long double EPS2 = 1e-6;
const long double PI = acos(-1);
const int MOD7 = 1000000007;
const int MOD9 = 1000000009;
const int a228 = 18;
const int inf = 1e9;
const long long kekmod = 1791791791;
const long long bestmod = 1148822869;
const int mod = MOD7;
signed main() {
ios_base::sync_with_stdio(0);
cout.tie(0);
cin.tie(0);
srand(time(NULL));
long long t;
cin >> t;
while (t--) {
long long n;
cin >> n;
vector<long long> a(n);
for (long long i = 0; i < a.size(); ++i) cin >> a[i];
;
map<long long, long long> cnt;
long long ans = 1e9;
sort((a).begin(), (a).end());
for (long long i = 0; i < n - 1; ++i) ans = min(ans, a[i + 1] - a[i]);
cout << ans << 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>
#pragma GCC target( \
"sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#pragma GCC optimize("unroll-loops")
using namespace std;
const long double EPS = 0.001;
const long double EPS2 = 1e-6;
const long double PI = acos(-1);
const int MOD7 = 1000000007;
const int MOD9 = 1000000009;
const int a228 = 18;
const int inf = 1e9;
const long long kekmod = 1791791791;
const long long bestmod = 1148822869;
const int mod = MOD7;
signed main() {
ios_base::sync_with_stdio(0);
cout.tie(0);
cin.tie(0);
srand(time(NULL));
long long t;
cin >> t;
while (t--) {
long long n;
cin >> n;
vector<long long> a(n);
for (long long i = 0; i < a.size(); ++i) cin >> a[i];
;
map<long long, long long> cnt;
long long ans = 1e9;
sort((a).begin(), (a).end());
for (long long i = 0; i < n - 1; ++i) ans = min(ans, a[i + 1] - a[i]);
cout << ans << endl;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int athlete(int a[], int n) {
int diff = 0, min = INT_MAX;
sort(a, a + n);
for (int i = 1; i < n; i++) {
diff = a[i] - a[i - 1];
if (diff <= min) {
min = diff;
}
}
return min;
}
int main() {
int t, n, at;
int a[50];
cin >> t;
for (int i = 0; i < t; i++) {
cin >> n;
for (int j = 0; j < n; j++) {
cin >> a[j];
}
at = athlete(a, n);
cout << at << 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 athlete(int a[], int n) {
int diff = 0, min = INT_MAX;
sort(a, a + n);
for (int i = 1; i < n; i++) {
diff = a[i] - a[i - 1];
if (diff <= min) {
min = diff;
}
}
return min;
}
int main() {
int t, n, at;
int a[50];
cin >> t;
for (int i = 0; i < t; i++) {
cin >> n;
for (int j = 0; j < n; j++) {
cin >> a[j];
}
at = athlete(a, n);
cout << at << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
int main() {
int t;
scanf("%d", &t);
for (int i = 0; i < t; i++) {
int n;
scanf("%d", &n);
int ar[n];
for (int i = 0; i < n; i++) {
scanf("%d", &ar[i]);
}
int temp;
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (ar[i] > ar[j]) {
temp = ar[i];
ar[i] = ar[j];
ar[j] = temp;
}
}
}
int a = 100000000, c = 0;
for (int i = 0; i < n; i++) {
c = ar[i + 1] - ar[i];
if (a > c) {
a = c;
}
}
printf("%d\n", a);
}
}
| ### 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>
int main() {
int t;
scanf("%d", &t);
for (int i = 0; i < t; i++) {
int n;
scanf("%d", &n);
int ar[n];
for (int i = 0; i < n; i++) {
scanf("%d", &ar[i]);
}
int temp;
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (ar[i] > ar[j]) {
temp = ar[i];
ar[i] = ar[j];
ar[j] = temp;
}
}
}
int a = 100000000, c = 0;
for (int i = 0; i < n; i++) {
c = ar[i + 1] - ar[i];
if (a > c) {
a = c;
}
}
printf("%d\n", a);
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int a[10005];
int n, z;
int t, sum;
int ans;
int main() {
cin >> t;
while (t--) {
z = 0;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sort(a, a + n);
int y = 10000;
for (int i = 1; i < n; i++) {
z = a[i] - a[i - 1];
y = min(z, y);
}
cout << y << 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 a[10005];
int n, z;
int t, sum;
int ans;
int main() {
cin >> t;
while (t--) {
z = 0;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sort(a, a + n);
int y = 10000;
for (int i = 1; i < n; i++) {
z = a[i] - a[i - 1];
y = min(z, y);
}
cout << y << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int num[60], flag[2000];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
int n, f = 1;
cin >> n;
memset(flag, 0, sizeof(flag));
for (int i = 1; i <= n; i++) {
cin >> num[i];
if (flag[num[i]]) {
f = 0;
}
flag[num[i]] = 1;
}
if (!f) {
cout << 0 << endl;
continue;
}
sort(num + 1, num + 1 + n);
int ans = 1e9;
for (int i = 1; i < n; i++) {
ans = min(abs(num[i + 1] - num[i]), ans);
}
cout << ans << 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 num[60], flag[2000];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
int n, f = 1;
cin >> n;
memset(flag, 0, sizeof(flag));
for (int i = 1; i <= n; i++) {
cin >> num[i];
if (flag[num[i]]) {
f = 0;
}
flag[num[i]] = 1;
}
if (!f) {
cout << 0 << endl;
continue;
}
sort(num + 1, num + 1 + n);
int ans = 1e9;
for (int i = 1; i < n; i++) {
ans = min(abs(num[i + 1] - num[i]), ans);
}
cout << ans << endl;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int a[51], n;
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
sort(a, a + n);
int res = INT_MAX;
for (int i = 1; i < n; i++) {
res = min(res, a[i] - a[i - 1]);
}
cout << res << 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 a[51], n;
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
sort(a, a + n);
int res = INT_MAX;
for (int i = 1; i < n; i++) {
res = min(res, a[i] - a[i - 1]);
}
cout << res << endl;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e18;
long long add(long long x, long long y) {
long long res = x + y;
return (res >= 1000000007 ? res - 1000000007 : res);
}
long long mul(long long x, long long y) {
long long res = x * y;
return (res >= 1000000007 ? res % 1000000007 : res);
}
long long sub(long long x, long long y) {
long long res = x - y;
return (res < 0 ? res + 1000000007 : res);
}
long long power(long long x, long long y) {
long long res = 1;
x %= 1000000007;
while (y) {
if (y & 1) res = mul(res, x);
y >>= 1;
x = mul(x, x);
}
return res;
}
long long mod_inv(long long x) { return power(x, 1000000007 - 2); }
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long ttt;
cin >> ttt;
while (ttt--) {
long long n;
cin >> n;
long long a[n];
for (long long i = (0); i < (n); i++) cin >> (a)[i];
;
sort(a, a + n);
long long ans = 999999;
for (long long i = 1; i < n; i++) {
ans = min(ans, a[i] - a[i - 1]);
}
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;
const long long inf = 1e18;
long long add(long long x, long long y) {
long long res = x + y;
return (res >= 1000000007 ? res - 1000000007 : res);
}
long long mul(long long x, long long y) {
long long res = x * y;
return (res >= 1000000007 ? res % 1000000007 : res);
}
long long sub(long long x, long long y) {
long long res = x - y;
return (res < 0 ? res + 1000000007 : res);
}
long long power(long long x, long long y) {
long long res = 1;
x %= 1000000007;
while (y) {
if (y & 1) res = mul(res, x);
y >>= 1;
x = mul(x, x);
}
return res;
}
long long mod_inv(long long x) { return power(x, 1000000007 - 2); }
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long ttt;
cin >> ttt;
while (ttt--) {
long long n;
cin >> n;
long long a[n];
for (long long i = (0); i < (n); i++) cin >> (a)[i];
;
sort(a, a + n);
long long ans = 999999;
for (long long i = 1; i < n; i++) {
ans = min(ans, a[i] - a[i - 1]);
}
cout << ans << endl;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long int minn(long long int a, long long int b) {
if (a > b)
return b;
else
return a;
}
long long int power(long long int x, long long int y, long long int p) {
long long int res = 1;
x = x % p;
if (x == 0) return 0;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
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 = new int[n];
for (int i = 0; i < n; i++) cin >> a[i];
sort(a, a + n);
int min = a[1] - a[0];
for (int i = 1; i < n; i++) {
if (a[i] - a[i - 1] < min) min = a[i] - a[i - 1];
}
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;
long long int minn(long long int a, long long int b) {
if (a > b)
return b;
else
return a;
}
long long int power(long long int x, long long int y, long long int p) {
long long int res = 1;
x = x % p;
if (x == 0) return 0;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
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 = new int[n];
for (int i = 0; i < n; i++) cin >> a[i];
sort(a, a + n);
int min = a[1] - a[0];
for (int i = 1; i < n; i++) {
if (a[i] - a[i - 1] < min) min = a[i] - a[i - 1];
}
cout << min << "\n";
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int arr[n + 3];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
sort(arr, arr + n);
int m = 10000;
for (int i = 0; i < n - 1; i++) {
if (arr[i + 1] - arr[i] < m) {
m = arr[i + 1] - arr[i];
}
}
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() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int arr[n + 3];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
sort(arr, arr + n);
int m = 10000;
for (int i = 0; i < n - 1; i++) {
if (arr[i + 1] - arr[i] < m) {
m = arr[i + 1] - arr[i];
}
}
cout << m << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n, min = 999999999;
cin >> n;
vector<int> arr(n);
int val;
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
sort(arr.begin(), arr.end());
for (int i = 0; i < n - 1; i++) {
if (arr[i + 1] - arr[i] < min) {
min = arr[i + 1] - arr[i];
}
}
cout << min << 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 main() {
int t;
cin >> t;
while (t--) {
int n, min = 999999999;
cin >> n;
vector<int> arr(n);
int val;
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
sort(arr.begin(), arr.end());
for (int i = 0; i < n - 1; i++) {
if (arr[i + 1] - arr[i] < min) {
min = arr[i + 1] - arr[i];
}
}
cout << min << endl;
}
}
``` |
#include <bits/stdc++.h>
int min(int a, int b) {
if (a < b) return a;
return b;
}
int main() {
int t, n;
scanf("%d", &t);
for (int l = 0; l < t; l++) {
scanf("%d", &n);
int a[n];
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
if (n == 2) {
printf("%d\n", -min((a[0] - a[1]), (a[1] - a[0])));
} else if (n == 1) {
printf("%d\n", a[0]);
} else {
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (a[i] < a[j]) {
int t = a[i];
a[i] = a[j];
a[j] = t;
}
}
}
int max = 1 << 30;
int b[n - 1];
for (int i = 0; i < n - 1; i++) {
b[i] = a[i] - a[i + 1];
if (b[i] < max) max = b[i];
}
printf("%d\n", max);
}
}
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>
int min(int a, int b) {
if (a < b) return a;
return b;
}
int main() {
int t, n;
scanf("%d", &t);
for (int l = 0; l < t; l++) {
scanf("%d", &n);
int a[n];
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
if (n == 2) {
printf("%d\n", -min((a[0] - a[1]), (a[1] - a[0])));
} else if (n == 1) {
printf("%d\n", a[0]);
} else {
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (a[i] < a[j]) {
int t = a[i];
a[i] = a[j];
a[j] = t;
}
}
}
int max = 1 << 30;
int b[n - 1];
for (int i = 0; i < n - 1; i++) {
b[i] = a[i] - a[i + 1];
if (b[i] < max) max = b[i];
}
printf("%d\n", max);
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int j, t;
cin >> t;
for (j = 0; j < t; j++) {
int n, i, x;
cin >> n;
int a[100] = {0};
for (i = 0; i < n; i++) {
cin >> a[i];
}
sort(a, a + n);
int mi = 0;
mi = a[1] - a[0];
for (i = 0; i < n - 1; i++) {
x = a[i + 1] - a[i];
if (mi >= x) {
mi = x;
}
}
cout << mi;
cout << 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 j, t;
cin >> t;
for (j = 0; j < t; j++) {
int n, i, x;
cin >> n;
int a[100] = {0};
for (i = 0; i < n; i++) {
cin >> a[i];
}
sort(a, a + n);
int mi = 0;
mi = a[1] - a[0];
for (i = 0; i < n - 1; i++) {
x = a[i + 1] - a[i];
if (mi >= x) {
mi = x;
}
}
cout << mi;
cout << endl;
}
}
``` |
#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);
vector<int> v;
for (int i = 0; i < n - 1; i++) {
v.push_back(a[i + 1] - a[i]);
}
int min = *min_element(v.begin(), v.end());
cout << min << 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 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);
vector<int> v;
for (int i = 0; i < n - 1; i++) {
v.push_back(a[i + 1] - a[i]);
}
int min = *min_element(v.begin(), v.end());
cout << min << endl;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long int t, n, s = 0, A[1000000];
int main() {
cin >> t;
for (int y = 1; y <= t; y++) {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> A[i];
}
sort(A + 1, A + n + 1);
s = A[2] - A[1];
for (int i = 1; i <= n - 1; i++) {
if (s > A[i + 1] - A[i]) {
s = A[i + 1] - A[i];
}
if (s == 0) {
break;
}
}
cout << s << 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;
long long int t, n, s = 0, A[1000000];
int main() {
cin >> t;
for (int y = 1; y <= t; y++) {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> A[i];
}
sort(A + 1, A + n + 1);
s = A[2] - A[1];
for (int i = 1; i <= n - 1; i++) {
if (s > A[i + 1] - A[i]) {
s = A[i + 1] - A[i];
}
if (s == 0) {
break;
}
}
cout << s << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int isalpha(int *arr, int n) {
int ans = INT_MAX;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
ans = min(ans, abs(arr[i] - arr[j]));
}
}
return ans;
}
int main() {
int test;
scanf("%d", &test);
while (test--) {
int n;
scanf("%d", &n);
int *arr = new int[n];
for (int i = 0; i < n; i++) scanf("%d", &arr[i]);
int ans = isalpha(arr, n);
printf("%d\n", ans);
}
}
| ### 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 isalpha(int *arr, int n) {
int ans = INT_MAX;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
ans = min(ans, abs(arr[i] - arr[j]));
}
}
return ans;
}
int main() {
int test;
scanf("%d", &test);
while (test--) {
int n;
scanf("%d", &n);
int *arr = new int[n];
for (int i = 0; i < n; i++) scanf("%d", &arr[i]);
int ans = isalpha(arr, n);
printf("%d\n", ans);
}
}
``` |
Subsets and Splits