29. Height Checker

EasyArrayArray

A school tries to take photos of students in increasing height order. Students are asked to stand in a line and given an array heights. Return the number of indices where heights[i] != expected[i], where expected is the sorted version of heights.

Input: An integer array heights of length n.

Output: Integer — number of positions where height doesn't match expected.

Examples

Example 1
Input: [1,1,4,2,1,3]
Output: 3
Explanation: Expected: [1,1,1,2,3,4]. Mismatches at indices 2,4,5 → 3.
Example 2
Input: [5,1,2,3,4]
Output: 5
Explanation: Expected: [1,2,3,4,5]. All 5 positions differ → 5.
Example 3
Input: [1,2,3,4,5]
Output: 0
Explanation: Already sorted. No mismatches → 0.

Constraints

Asked by

IBMMicrosoftBloombergGoogleAmazonMeta
Solve this problem in the editor →