-
Notifications
You must be signed in to change notification settings - Fork 16
/
check-sort-rotate.cpp
67 lines (53 loc) · 1.29 KB
/
check-sort-rotate.cpp
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// C++ implementation of above approach
#include <bits/stdc++.h>
using namespace std;
// Function to check if it is possible
bool isPossible(int a[], int n)
{
// step 1
if (is_sorted(a, a + n)) {
cout << "Possible" << endl;
}
else {
// break where a[i] > a[i+1]
bool flag = true;
int i;
for (i = 0; i < n - 1; i++) {
if (a[i] > a[i + 1]) {
break;
}
}
// break point + 1
i++;
// check whether the sequence is
// further increasing or not
for (int k = i; k < n - 1; k++) {
if (a[k] > a[k + 1]) {
flag = false;
break;
}
}
// If not increasing after break point
if (!flag)
return false;
else {
// last element <= First element
if (a[n - 1] <= a[0])
return true;
else
return false;
}
}
}
// Driver code
int main()
{
int arr[] = { 3, 1, 2, 2, 3 };
int n = sizeof(arr) / sizeof(arr[0]);
if (isPossible(arr, n))
cout << "Possible";
else
cout << "Not Possible";
return
0;
}