easy Height Checker
Problem Statement
A school is organizing its annual photo session, and students must stand in a single line arranged by height in non-decreasing order. The expected order of heights is represented by an array expected, where expected[i] is the height of the ith student in line.
Given an array heights representing the current order in which students are standing, determine how many positions in the heights array do not match the expected order.
Examples
-
Example 1:
- Input: heights =
[5, 1, 2, 3, 4, 8, 1] - Expected Output:
3 - Justification: The expected order is
[1, 1, 2, 3, 4, 5, 8]. Comparing withheights, the positions that do not match are 0, 5 and 6.
- Input: heights =
-
Example 2:
- Input: heights =
[10, 6, 8, 5, 9] - Expected Output:
3 - Justification: The expected order is
[5, 6, 8, 9, 10]. Comparing withheights, the positions that do not match are 0, 3 and 4.
- Input: heights =
-
Example 3:
- Input: heights =
[7, 3, 2, 1, 4] - Expected Output:
5 - Justification: The expected order is
[1, 2, 3, 4, 7]. All positions differ fromheights.
- Input: heights =
Constraints:
1 <= heights.length <= 1001 <= heights[i] <= 100
Solution
Try it yourself
Try solving this question here:
✅ Solution Height Checker
Problem Statement
A school is organizing its annual photo session, and students must stand in a single line arranged by height in non-decreasing order. The expected order of heights is represented by an array expected, where expected[i] is the height of the ith student in line.
Given an array heights representing the current order in which students are standing, determine how many positions in the heights array do not match the expected order.
Examples
-
Example 1:
- Input: heights =
[5, 1, 2, 3, 4, 8, 1] - Expected Output:
3 - Justification: The expected order is
[1, 1, 2, 3, 4, 5, 8]. Comparing withheights, the positions that do not match are 0, 5 and 6.
- Input: heights =
-
Example 2:
- Input: heights =
[10, 6, 8, 5, 9] - Expected Output:
3 - Justification: The expected order is
[5, 6, 8, 9, 10]. Comparing withheights, the positions that do not match are 0, 3 and 4.
- Input: heights =
-
Example 3:
- Input: heights =
[7, 3, 2, 1, 4] - Expected Output:
5 - Justification: The expected order is
[1, 2, 3, 4, 7]. All positions differ fromheights.
- Input: heights =
Constraints:
1 <= heights.length <= 1001 <= heights[i] <= 100
Solution
To solve this problem, we use a counting sort approach to determine how many students are standing in incorrect positions. We start by creating a frequency array to count the occurrences of each height in the heights array. This frequency array helps us understand how many times each height appears, making it easy to reconstruct the sorted order of heights. By leveraging the count of each height, we can then build the expected sorted array without actually sorting the original array, which is more efficient.
Next, we compare the original heights array with the reconstructed sorted array. We iterate through the heights array and, for each position, check if the height matches the expected height from the sorted array. If there is a mismatch, we increment a counter. This approach works efficiently because it avoids the need for a traditional sorting algorithm, instead relying on the counting sort's linear time complexity to handle the sorting implicitly.
Step-by-Step Algorithm
-
Initialize Variables:
- Define
max_heightas the maximum value in the heights array (given as 100 for simplicity). - Create a frequency array
countof sizemax_height + 1initialized to zero.
- Define
-
Count Frequencies:
- Iterate through each height in the
heightsarray. - For each height, increment the corresponding index in the
countarray.
- Iterate through each height in the
-
Initialize Result and Current Height:
- Initialize
resultto zero, which will count the mismatches. - Initialize
currentHeightto zero, which will track the current height being processed.
- Initialize
-
Compare Arrays:
- Iterate through the
heightsarray using an indexi. - For each height in
heights, find the next non-zero count in thecountarray:- This step will give us next height in the sorted order, and we will compare it with the
height[i]. - While
count[currentHeight]is zero, incrementcurrentHeight. In this step, we skip index related to elements which are not in theheightsarray. - Compare
heights[i]withcurrentHeight:- If they do not match, increment
result.
- If they do not match, increment
- Decrement the count of
currentHeightin thecountarray.
- This step will give us next height in the sorted order, and we will compare it with the
- Iterate through the
-
Return Result:
- Return
resultas the number of positions where the heights do not match the expected order.
- Return
Algorithm Walkthrough
Given heights = [5, 1, 2, 3, 4, 8, 1]:
-
Initialize Variables:
max_height = 100.count = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0](array of sizemax_height + 1).
-
Count Frequencies:
- Iterate through
heights:- For height 5:
count[5]becomes 1. - For height 1:
count[1]becomes 1. - For height 2:
count[2]becomes 1. - For height 3:
count[3]becomes 1. - For height 4:
count[4]becomes 1. - For height 8:
count[8]becomes 1. - For height 1:
count[1]becomes 2.
- For height 5:
- Resulting
countarray:[0, 2, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0].
- Iterate through
-
Initialize Result and Current Height:
result = 0.currentHeight = 0.
-
Compare Arrays:
- Iterate through
heightswith indexi:- For
i = 0,heights[0] = 5:- While
count[currentHeight] == 0, incrementcurrentHeightto 1. - Compare 5 with 1: Mismatch, increment
resultto 1. - Decrement
count[1]to 1.
- While
- For
i = 1,heights[1] = 1:- Compare 1 with 1: Match,
resultremains 1. - Decrement
count[1]to 0.
- Compare 1 with 1: Match,
- For
i = 2,heights[2] = 2:- Increment
currentHeightto 2 (sincecount[1]is 0). - Compare 2 with 2: Match,
resultremains 1. - Decrement
count[2]to 0.
- Increment
- For
i = 3,heights[3] = 3:- Increment
currentHeightto 3 (sincecount[2]is 0). - Compare 3 with 3: Match,
resultremains 1. - Decrement
count[3]to 0.
- Increment
- For
i = 4,heights[4] = 4:- Increment
currentHeightto 4 (sincecount[3]is 0). - Compare 4 with 4: Match,
resultremains 1. - Decrement
count[4]to 0.
- Increment
- For
i = 5,heights[5] = 8:- Increment
currentHeightto 5 (sincecount[4]is 0). - Compare 8 with 5: Mismatch, increment
resultto 2. - Decrement
count[5]to 0.
- Increment
- For
i = 6,heights[6] = 1:- Increment
currentHeightto 8 (sincecount[5]tocount[7]is 0). - Compare 1 with 8: Mismatch, increment
resultto 3. - Decrement
count[8]to 0.
- Increment
- For
- Iterate through
-
Return Result:
- The function returns
result, which is 3.
- The function returns
Code
class Solution {
public int heightChecker(int[] heights) {
int max_height = 100;
// Create a count array to keep track of the number of students with each height
int[] count = new int[max_height + 1];
// Populate the count array based on the heights in the input
for (int height : heights) {
count[height]++;
}
int result = 0;
// Start checking from the smallest possible height
int currentHeight = 0;
// Iterate over each height in the input array
for (int i = 0; i < heights.length; i++) {
// Find the next height with a non-zero count (skip heights not in the array)
while (count[currentHeight] == 0) {
currentHeight++;
}
// If the current height in the input array does not match the expected sorted height
if (heights[i] != currentHeight) {
result++;
}
// Decrement the count of the current height (since we've used one occurrence of it)
count[currentHeight]--;
}
return result;
}
public static void main(String[] args) {
Solution sol = new Solution();
System.out.println(sol.heightChecker(new int[] { 5, 1, 2, 3, 4, 8, 1 })); // Output: 3
System.out.println(sol.heightChecker(new int[] { 10, 6, 8, 5, 9 })); // Output: 3
System.out.println(sol.heightChecker(new int[] { 7, 3, 2, 1, 4 })); // Output: 5
}
}
Complexity Analysis
Time Complexity
- Counting Heights: We iterate through the
heightsarray once to populate thecountarray, which takestime. - Comparison and Counting: We iterate through the
heightsarray again to compare each height with the expected order, which also takestime. The inner while loop that increments currentHeightonly iterates a constant number of times (at most 100 times, due to the constraints), which does not add significant complexity. - Thus, the overall time complexity is
.
Space Complexity
- The space complexity is
because the countarray size is constant (fixed at 101), regardless of the input size due to given constraints. We do not use any additional space that scales with the input size.
🤖 Don't fully get this? Learn it with Claude
Stuck on Height Checker? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.
Progressively stronger hints — you still solve it.
I'm working on the problem **Height Checker** (DSA). Give me a HINT LADDER: start with the tiniest nudge, then wait. Only reveal the next, stronger hint when I ask. Do NOT show the full solution unless I type 'show solution'. Keep me doing the thinking. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
See the technique, not just code.
Explain the optimal approach to **Height Checker** with a VISUAL walkthrough: trace it on a small concrete example using ASCII art / a step-by-step diagram, narrate what changes each step, then give time & space complexity with a one-line derivation. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Catch bugs, edge cases, sub-optimality.
I'll paste my solution to **Height Checker**. Review it for correctness, missed edge cases, and time/space complexity, then coach me toward the optimal — don't just rewrite it. Ask me to paste my code now. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Lock in recognition with look-alikes.
Give me 2 problems that use the SAME underlying pattern as **Height Checker**. For each, let me attempt first, then review my answer and name the trigger signal that reveals the pattern. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.