Height Checker
Let’s understand the problem first: We need to arrange students in non-decreasing order of their heights, and then compare the current order of heights with the expected order. The task is to count how many heights are out of place.
|
|
Explanation
- We sort the given
heights
array to create the expected order of heights. - We then initialize a variable
mismatch_count
to keep track of the number of heights that are out of place. - We iterate through the
heights
array and compare each height with the corresponding height in the sortedexpected
array. If they don’t match, we increment themismatch_count
. - Finally, we return the total number of mismatched heights, which gives the number of indices where
heights[i]
is not equal toexpected[i]
.