Global and Local Inversions

Medium

Description

A local inversion is A[i] > A[i+1]. A global inversion is A[i] > A[j] where i < j. Return true if the count of each is equal.

Examples

Input:nums = [1,0,2]
Output:true
Explanation:

Both have 1 inversion.

Input:nums = [1,2,0]
Output:false
Explanation:

For nums = [1,2,0], the answer is false because the global and local inversions condition is not met.

Input:nums = [0,1,2,3]
Output:true
Explanation:

This array is already sorted, so there are no inversions at all. Both global inversions and local inversions equal 0, making the condition satisfied.

Constraints

  • 1 ≤ nums.length ≤ 10⁵

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!