-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck-if-array-is-sorted-and-rotated.cpp
More file actions
41 lines (39 loc) · 1.39 KB
/
check-if-array-is-sorted-and-rotated.cpp
File metadata and controls
41 lines (39 loc) · 1.39 KB
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
// https://leetcode.com/problems/check-if-array-is-sorted-and-rotated/submissions/1257037433/
// Iterate over given array until point of rotation by comparing with previous value. If there's no rotation, return true. Set flag at point of rotation and start copying elements from this point till the end into a new array. If more points of rotation are found, return false. Initialize a new loop and copy all the values till the point of rotation into the new array. Iterate over new array. If it is sorted, return true, otherwise return false.
class Solution {
public:
bool check(vector<int>& nums) {
int arr[nums.size()];
bool flag = false;
int rot_pt;
int j = 0;
for(int i=1; i<nums.size(); i++){
if(nums[i-1] > nums[i]){
if(flag){
return false;
}else{
flag = true;
rot_pt = i;
}
}
if(flag){
arr[j] = nums[i];
j++;
}
}
if(!flag){
return true;
}else{
for(int k=0; k<rot_pt; k++){
arr[j] = nums[k];
j++;
}
for(int i=1; i<nums.size(); i++){
if(arr[i-1] > arr[i]){
return false;
}
}
}
return true;
}
};