Skip to content
BytePatterns

Out Of Place Count

EasySorting#sorting#pairwise-comparison~15m

Problem

Students stand in a line, each described by a height. They were supposed to be lined up from shortest to tallest. Count the positions holding a student whose height differs from the height that should be standing there. Equal heights are interchangeable, so a position holding the right height is never counted.

Examples

Input:  heights = [1, 1, 4, 2, 1, 3]
Output: 3
Why:    the correct line is 1 1 1 2 3 4, and three positions disagree
Input:  heights = [5, 4, 3, 2, 1]
Output: 4
Why:    only the middle student happens to already stand correctly
Input:  heights = [1, 2, 3]
Output: 0
Why:    edge case, an already correct line has nothing out of place

Hints

0 / 3

Stuck on the idea rather than the code? Sorting Basics covers it.