#K89227. Local vs Global Inversions
Local vs Global Inversions
Local vs Global Inversions
You are given a permutation of the integers from 0 to n-1. An inversion is a pair of indices (i, j) such that i < j and nums[i] > nums[j]. A local inversion is an inversion where j = i+1. The task is to determine if the permutation has the property that the number of local inversions is equal to the total number of inversions (global inversions). It can be shown that this is true if and only if every element is no more than one position away from its value's natural sorted position, i.e. |nums[i] - i| \(\leq 1\) for all valid indices i.
Input will be provided as follows: The first line contains a single integer n representing the number of elements. The second line contains n space-separated integers describing the permutation.
Your program should output True
if the permutation satisfies the above property, and False
otherwise.
inputFormat
The first line contains an integer n (1 ≤ n ≤ 105) indicating the number of elements.
The second line contains n space-separated integers which represent a permutation of the integers from 0 to n-1.
outputFormat
Output a single line: True
if the permutation has the property that the number of local inversions equals the number of global inversions, otherwise False
.
3
1 0 2
True