Skip to content

Commit eb5540a

Browse files
authored
Merge pull request #31 from satishguptaji/patch-3
File for rotation checking
2 parents 7966439 + beb96b4 commit eb5540a

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

check-sort-rotate.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// C++ implementation of above approach
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
5+
// Function to check if it is possible
6+
bool isPossible(int a[], int n)
7+
{
8+
// step 1
9+
if (is_sorted(a, a + n)) {
10+
cout << "Possible" << endl;
11+
}
12+
13+
else {
14+
15+
// break where a[i] > a[i+1]
16+
bool flag = true;
17+
int i;
18+
for (i = 0; i < n - 1; i++) {
19+
if (a[i] > a[i + 1]) {
20+
break;
21+
}
22+
}
23+
// break point + 1
24+
i++;
25+
26+
// check whether the sequence is
27+
// further increasing or not
28+
for (int k = i; k < n - 1; k++) {
29+
if (a[k] > a[k + 1]) {
30+
flag = false;
31+
break;
32+
}
33+
}
34+
35+
// If not increasing after break point
36+
if (!flag)
37+
return false;
38+
39+
else {
40+
41+
// last element <= First element
42+
if (a[n - 1] <= a[0])
43+
return true;
44+
45+
else
46+
return false;
47+
}
48+
}
49+
}
50+
51+
// Driver code
52+
int main()
53+
{
54+
55+
int arr[] = { 3, 1, 2, 2, 3 };
56+
int n = sizeof(arr) / sizeof(arr[0]);
57+
58+
if (isPossible(arr, n))
59+
cout << "Possible";
60+
61+
else
62+
cout << "Not Possible";
63+
64+
return
65+
0;
66+
}
67+

0 commit comments

Comments
 (0)