mediumArrayMath 0 views

Global and Local Inversions

You are given an integer array nums of length n which represents a permutation of all the integers in the range [0, n - 1].

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:

The number of local inversions is the number of indices i where:

Return true if the number of global inversions is equal to the number of local inversions.

Global and Local Inversions diagram

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.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].

Hints

Companies

No companies reported yet.

Discussion

Sign in to join the discussion.

Loading discussion...

Test results

No test cases yet.