Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
You are given an integer array nums of length n which represents a permutation of all the integers in the range [0, n - 1].
The number of global inversions is the number of the different pairs (i, j) where:
0 <= i < j < nnums[i] > nums[j]The number of local inversions is the number of indices i where:
0 <= i < n - 1nums[i] > nums[i + 1]Return true if the number of global inversions is equal to the number of local inversions.
Example 1:
Input: nums = [1,0,2] Output: true Explanation: There is 1 global inversion and 1 local inversion.
Example 2:
Input: nums = [1,2,0] Output: false Explanation: There are 2 global inversions and 1 local inversion.
Constraints:
n == nums.length1 <= n <= 1050 <= nums[i] < nnums are unique.nums is a permutation of all the numbers in the range [0, n - 1].Problem summary: You are given an integer array nums of length n which represents a permutation of all the integers in the range [0, n - 1]. The number of global inversions is the number of the different pairs (i, j) where: 0 <= i < j < n nums[i] > nums[j] The number of local inversions is the number of indices i where: 0 <= i < n - 1 nums[i] > nums[i + 1] Return true if the number of global inversions is equal to the number of local inversions.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Math
[1,0,2]
[1,2,0]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #775: Global and Local Inversions
class Solution {
public boolean isIdealPermutation(int[] nums) {
int mx = 0;
for (int i = 2; i < nums.length; ++i) {
mx = Math.max(mx, nums[i - 2]);
if (mx > nums[i]) {
return false;
}
}
return true;
}
}
// Accepted solution for LeetCode #775: Global and Local Inversions
func isIdealPermutation(nums []int) bool {
mx := 0
for i := 2; i < len(nums); i++ {
mx = max(mx, nums[i-2])
if mx > nums[i] {
return false
}
}
return true
}
# Accepted solution for LeetCode #775: Global and Local Inversions
class Solution:
def isIdealPermutation(self, nums: List[int]) -> bool:
mx = 0
for i in range(2, len(nums)):
if (mx := max(mx, nums[i - 2])) > nums[i]:
return False
return True
// Accepted solution for LeetCode #775: Global and Local Inversions
struct Solution;
impl Solution {
fn is_ideal_permutation(a: Vec<i32>) -> bool {
let n = a.len();
for i in 0..n {
if (a[i] - i as i32).abs() > 1 {
return false;
}
}
true
}
}
#[test]
fn test() {
let a = vec![1, 0, 2];
let res = true;
assert_eq!(Solution::is_ideal_permutation(a), res);
let a = vec![1, 2, 0];
let res = false;
assert_eq!(Solution::is_ideal_permutation(a), res);
}
// Accepted solution for LeetCode #775: Global and Local Inversions
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #775: Global and Local Inversions
// class Solution {
// public boolean isIdealPermutation(int[] nums) {
// int mx = 0;
// for (int i = 2; i < nums.length; ++i) {
// mx = Math.max(mx, nums[i - 2]);
// if (mx > nums[i]) {
// return false;
// }
// }
// return true;
// }
// }
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.