Global and Local Inversions LeetCode solution in C++

1
/*
2
Approach :-> Given nums are from 0 to n-1 so to make local and global count
3
equal it must have swapping between adjacent element in sorted array so for any
4
element at index if it is either swapped with i or i+1 or i-1 this is only
5
condition when both are equal else it got swapped with j > i+1 or j<i-1 then
6
global count become greater than local count
7
*/
8
class Solution {
9
public:
10
bool isIdealPermutation(vector<int> &nums) {
11
for (int i = 0; i < nums.size(); i++) {
12
if (nums[i] == i || nums[i] == i - 1 || nums[i] == i + 1)
13
continue;
14
else
15
return false;
16
}
17
return true;
18
}
19
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0