-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA_Desorting.cpp
More file actions
47 lines (42 loc) · 899 Bytes
/
A_Desorting.cpp
File metadata and controls
47 lines (42 loc) · 899 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
// Check if already not sorted
bool sorted = true;
for (int i = 1; i < n; i++)
{
if (a[i] < a[i - 1])
{
sorted = false;
break;
}
}
if (!sorted)
{
cout << 0 << "\n";
continue;
}
// Array is sorted -> calculate min operations
int mindiff = INT_MAX;
for (int i = 1; i < n; i++)
{
mindiff = min(mindiff, a[i] - a[i - 1]);
}
cout << (mindiff / 2 + 1) << "\n";
}
return 0;
}