Problem · Array

Count Students Outside Their Sorted Positions

Learn this problem
EasyConcentric AI logoConcentric AIFULLTIMEOA

Problem statement

You are given an array heights, where heights[i] is the height of the student currently standing at index i.

The students should stand in nondecreasing height order. Return the number of indices whose current height differs from the height that would appear at that index after sorting the entire array.

Function

countStudentsOutOfPosition(heights: int[]) → int

Examples

Example 1

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

The sorted array is [1,1,1,2,3,4]. Indices 2, 4, and 5 differ.

Example 2

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

After sorting, every index contains a different height.

Example 3

heights = [1,2,2,4]return = 0

The students are already in nondecreasing order.

Constraints

  • 1 <= heights.length <= 100000
  • 1 <= heights[i] <= 1000000000

More Concentric AI problems

drafts saved locally
public int countStudentsOutOfPosition(int[] heights) {
    // Write your code here.
}
heights[1,1,4,2,1,3]
expected3
checking account