Global and Local Inversions - Solution & Explanation
Problem Statement
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] < n- All the integers of
numsare unique. numsis a permutation of all the numbers in the range[0, n - 1].
Approach Overview
Problem Overview: You are given a permutation of numbers from 0 to n-1. A local inversion occurs when nums[i] > nums[i+1], while a global inversion occurs when nums[i] > nums[j] for any i < j. The task is to check whether the total number of global inversions is exactly equal to the number of local inversions.
For the two counts to match, every global inversion must also be a local inversion. That means the array cannot contain any inversion where the distance between indices is greater than one. Any such pair automatically makes global inversions exceed local inversions.
Approach 1: Max Constraint Check (O(n) time, O(1) space)
This approach scans the array while tracking the maximum value seen so far up to index i. For each position, compare that maximum with nums[i+2]. If the previous maximum is greater than nums[i+2], a non-local global inversion exists because the larger element appears at least two indices before a smaller one. That directly violates the condition that all global inversions must be local.
The algorithm performs a single pass through the array and maintains only one running maximum. The moment a violation is detected, you can return false. If no such pair appears, every inversion is adjacent. This method is efficient because it avoids explicitly counting inversions and relies on a simple constraint check derived from the permutation structure. Problems involving permutation properties and constraints like this often appear in array reasoning tasks.
Approach 2: Compare Positions (O(n) time, O(1) space)
This method uses a key property of ideal permutations. If global inversions equal local inversions, each element can be displaced from its sorted position by at most one index. In other words, for every index i, the condition |nums[i] - i| ≤ 1 must hold.
You iterate through the array and compute the absolute difference between the value and its index. If any element is more than one position away from where it belongs in the sorted order, that displacement necessarily creates a non-local global inversion. Because the array is a permutation of 0..n-1, this check works in constant space and linear time. The reasoning relies on simple permutation math, which is common in math and array interview problems.
Recommended for interviews: The Max Constraint Check approach is typically expected in interviews because it demonstrates understanding of how global inversions extend beyond adjacent indices. It directly proves that no element two positions ahead violates the ordering constraint. The position comparison trick is shorter and elegant, but explaining the reasoning behind the displacement rule is essential to show deeper insight.
Approach 1: Approach 1: Max Constraint Check
This approach checks the condition if there is any inversion other than local inversions by checking for the maximum value condition constraint. We assume that if there exists any i such that nums[i] > nums[j] and j > i + 1, then it implies that the number of global inversions is greater than the number of local inversions.
Therefore, ensure for all i, nums[i] > nums[i+2] does not hold since, for it, nums[i] should be larger than any of the elements up to at least i + 2.
We iterate through the array while keeping track of the maximum number seen so far. The core idea is to ensure that the maximum value we pass is less than or equal to both the current value and the follow-up after skipping one (e.g., nums[i+2]).
Complexity
Time Complexity: O(n) because it requires a single traversal of the array.
Space Complexity: O(1) as no additional data structures are used.
Approach 2: Approach 2: Compare Positions
In this method, we will check each element if it is in a permissible range, conditionally permitting swaps with one element left or right, as can occur with local inversions. Assume an element is misplaced if it is not at i or i ± 1.
Iterate the array and verify every element index difference from the original is no greater than 1, suggesting only local inversions exist.
Complexity
Time Complexity: O(n)
Space Complexity: O(1)
Approach 3: Default Approach
Try this approach in the editor →Complexity Comparison
| Approach | Complexity |
|---|---|
| Approach 1: Max Constraint Check | Time Complexity: O(n) because it requires a single traversal of the array. |
| Approach 2: Compare Positions | Time Complexity: O(n) |
| Default Approach | — |
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Max Constraint Check | O(n) | O(1) | Best general solution; detects non-local global inversions during a single pass |
| Compare Positions | O(n) | O(1) | When the permutation property (0..n-1) is guaranteed and you want the shortest logic |
Video Solution
LeetCode 775. Global and Local Inversions (Algorithm Explained) • Nick White • 8,403 views views
Watch 9 more video solutions →Frequently Asked Questions
Is Global and Local Inversions easy or hard?
Global and Local Inversions Python/Java solution
How to solve Global and Local Inversions in O(n)?
What is the best approach for Global and Local Inversions?
Is Global and Local Inversions asked at Google/Amazon/Meta?
What data structure is used in Global and Local Inversions?
What is the time complexity of Global and Local Inversions?
Ready to solve this problem?
Practice Global and Local Inversions with our built-in code editor and test cases.
Practice on FleetCode