Count Heights Away From Their Sorted Positions

Quick Overview

Count Heights Away From Their Sorted Positions. Let expected be the heights sorted into nondecreasing order.

Count Heights Away From Their Sorted Positions

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem Let expected be the heights sorted into nondecreasing order. Return the number of positions i where the current `heights[i]` differs from expected[i]. Equal-height students are indistinguishable for this comparison. ```cpp class Solution { public: int heightChecker(vector<int>& heights); }; ``` ### Examples **Example 1:** ```text Input: heights = [3,1,2,2] Output: 3 Explanation: Expected is[1,2,2,3]; positions0,1,3 differ. ``` **Example 2:** ```text Input: heights = [2,2,4] Output: 0 Explanation: The current array is already nondecreasing. ``` ### Constraints - The line contains 1 to 100 students. - Every supplied height is an integer in `[1,100]`. ### Contract considerations Compare numeric heights at each index; identities among equal heights do not matter. ```hint Read the boundary condition If equal-height students exchange identities, should a position whose numeric height already matches still count? ``` ### Boundary reminders Compare numeric heights at each index; identities among equal heights do not matter.

Overview: Count Heights Away From Their Sorted Positions. Let expected be the heights sorted into nondecreasing order.

Google logo
Google
Jun 20, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

Let expected be the heights sorted into nondecreasing order. Return the number of positions i where the current heights[i] differs from expected[i]. Equal-height students are indistinguishable for this comparison.

class Solution {
public:
    int heightChecker(vector<int>& heights);
};

Examples

Example 1:

Input: heights = [3,1,2,2]
Output: 3
Explanation: Expected is[1,2,2,3]; positions0,1,3 differ.

Example 2:

Input: heights = [2,2,4]
Output: 0
Explanation: The current array is already nondecreasing.

Constraints

  • The line contains 1 to 100 students.
  • Every supplied height is an integer in [1,100] .

Contract considerations

Compare numeric heights at each index; identities among equal heights do not matter.

Boundary reminders

Compare numeric heights at each index; identities among equal heights do not matter.