The Array Height Checker Problem in Java


The challenge

Students are asked to stand in non-decreasing order of heights for an annual photo.

Return the minimum number of students that must move in order for all students to be standing in non-decreasing order of height.

Notice that when a group of students is selected they can reorder in any possible way between themselves and the non selected students remain on their seats.

Example 1:

Input: heights = [1,1,4,2,1,3]
Output: 3
Explanation: 
Current array : [1,1,4,2,1,3]
Target array  : [1,1,1,2,3,4]
On index 2 (0-based) we have 4 vs 1 so we have to move this student.
On index 4 (0-based) we have 1 vs 3 so we have to move this student.
On index 5 (0-based) we have 3 vs 4 so we have to move this student.

Example 2:

Input: heights = [5,1,2,3,4]
Output: 5

Example 3:

Input: heights = [1,2,3,4,5]
Output: 0

Constraints:

  • 1 <= heights.length <= 100
  • 1 <= heights[i] <= 100

The solution

The easiest way to solve this is to sort a copy of the array, then compare which values are different.

class Solution {
    // our method
    public int heightChecker(int[] heights) {
        // clone the input array
        int[] heights2 = Arrays.copyOf(heights, heights.length);
        
        // sort the new array to compare against
        Arrays.sort(heights2);
        
        // keep a count to return
        int count = 0;

        // loop through the input array
        for (int i=0; i<heights.length; i++) {
            // if the sorted value doesn't match
            // then increment
            if (heights[i]!=heights2[i]) count++;
        }
        
        // return our count
        return count;
    }
}