Height Checker

Given an array of heights, return how many indices are not in the position they would occupy after sorting the array in non-decreasing order. A counting-style approach works well when the value range is bounded.

Input Format

heights = array of heights

Output Format

number of indices that differ from sorted order

Constraints

  • 1 <= heights.length <= 100; 1 <= heights[i] <= 100

Examples

Example 1:

Input:

heights = [1,1,4,2,1,3]

Output:

3

Explanation:

Three positions differ from the sorted order [1,1,1,2,3,4].

Example 2:

Input:

heights = [5,1,2,3,4]

Output:

5

Explanation:

Every index differs from the sorted order.

Loading...
Height Checker - Sorting Based Array Problems