> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Global and Local Inversions Python Solution

> Tested Python solution for LeetCode 775 with 33 pytest cases. Generate a practice environment with lcpy.

LeetCode 775, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Math](/catalog/topics/math). [View on LeetCode](https://leetcode.com/problems/global-and-local-inversions/description/).

Generate this problem as a practice environment: tested reference solution, 33 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 775   # by problem number
lcpy gen -s global_and_local_inversions   # by problem name
```

## Problem

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.

### Examples

```
Input: nums = [1,0,2]
Output: true
Explanation: There is 1 global inversion and 1 local inversion.
```

```
Input: nums = [1,2,0]
Output: false
Explanation: There are 2 global inversions and 1 local inversion.
```

### Constraints

* n == nums.length
* 1 \<= n \<= 10^5
* 0 \<= nums\[i] \< n
* All the integers of nums are unique.
* nums is a permutation of all the numbers in the range \[0, n - 1].

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/global_and_local_inversions/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/global_and_local_inversions/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(1)
    def is_ideal_permutation(self, nums: list[int]) -> bool:
        # A global inversion that is not local needs indices i, j with j > i + 1
        # and nums[i] > nums[j]. In a permutation that can only happen when some
        # value sits more than one slot away from its own index, so every value
        # must be within distance 1 of its position.
        return all(abs(value - index) <= 1 for index, value in enumerate(nums))
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(1) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.