A school is trying to take an annual photo of all the students. The students are asked to stand in a single file line in non-decreasing order by height. Let this ordering be represented by the integer array expected where expected[i] is the expected height of the ith student in line.

You are given an integer array heights representing the current order that the students are standing in. Each heights[i] is the height of the ith student in line (0-indexed).

Return the number of indices where heights[i] != expected[i].

Example 1:

Input: heights = [1,1,4,2,1,3] Output: 3 Explanation: heights: [1,1,4,2,1,3] expected: [1,1,1,2,3,4] Indices 2, 4, and 5 do not match.

Example 2:

Input: heights = [5,1,2,3,4] Output: 5 Explanation: heights: [5,1,2,3,4] expected: [1,2,3,4,5] All indices do not match.

Example 3:

Input: heights = [1,2,3,4,5] Output: 0 Explanation: heights: [1,2,3,4,5] expected: [1,2,3,4,5] All indices match.

Constraints:

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

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program main
    implicit none

    integer, parameter :: n = 6
    integer, dimension(n) :: heights, expected
    integer :: i, j, count

    ! Examples
    heights = [1, 1, 4, 2, 1, 3]
    write (*, '(A)') 'Example 1:'
    call solve(heights, expected)
    write (*, '(A)') 'Expected: 3'
    write (*, '(A)') 'Actual: ', count

    heights = [5, 1, 2, 3, 4]
    write (*, '(A)') 'Example 2:'
    call solve(heights, expected)
    write (*, '(A)') 'Expected: 5'
    write (*, '(A)') 'Actual: ', count

    heights = [1, 2, 3, 4, 5]
    write (*, '(A)') 'Example 3:'
    call solve(heights, expected)
    write (*, '(A)') 'Expected: 0'
    write (*, '(A)') 'Actual: ', count

contains

    subroutine solve(heights, expected)
        implicit none
        integer, intent(in) :: heights(:), expected(:)
        integer :: i, j

        count = 0
        do i = 1, size(heights) - 1
            if (heights(i) /= expected(i)) then
                count = count + 1
            end if
        end do
    end subroutine solve
end program main
Compiled
Executed
Correct
module main

implicit none

integer, parameter :: int64 = selected_int_kind(13)

interface

function solve(heights, expected) result(res)
    integer(int64), dimension(:), intent(in) :: heights
    integer(int64), dimension(:), intent(in) :: expected
    integer(int64) :: res
end function solve

end interface

contains

subroutine run_test_case(heights, expected, output)
    integer(int64), dimension(:), intent(in) :: heights
    integer(int64), dimension(:), intent(in) :: expected
    integer(int64), intent(out) :: output

    output = solve(heights, expected)

    write (*,*) "Output: ", output
    write (*,*) "Expected: ", expected
    write (*,*) "Pass: ", (output == expected)
    write (*,*) ""

end subroutine run_test_case

subroutine run_test_cases()
    integer(int64), dimension(5) :: heights
    integer(int64), dimension(5) :: expected
    integer(int64) :: output

    ! Test case 1:
    heights = [1, 1, 4, 2, 1, 3]
    expected = [1, 1, 1, 2, 3, 4]
    call run_test_case(heights, expected, output)

    ! Test case 2:
    heights = [5, 1, 2, 3, 4]
    expected = [1, 2, 3, 4, 5]
    call run_test_case(heights, expected, output)

    ! Test case 3:
    heights = [1, 2, 3, 4, 5]
    expected = [1, 2, 3, 4, 5]
    call run_test_case(heights, expected, output)

end subroutine run_test_cases

end module main

module solution

contains

function solve(heights, expected) result(res)
    integer(int64), dimension(:), intent(in) :: heights
    integer(int64), dimension(:), intent(in) :: expected
    integer(int64) :: res

    res = count(heights /= expected)

end function solve

end module solution

program main
    use solution
    implicit none

    integer(int64), dimension(5) :: heights
    integer(int64), dimension(5) :: expected
    integer(int64) :: output

    call run_test_cases()

end program main
🌐 Data from online sources
def height_checker(heights):
    expected = sorted(heights)

    count = 0
    for i in range(len(heights)):
        if heights[i] != expected[i]:
            count += 1

    return count
  1. Create a copy of the input array called expected.
  2. Sort the expected array in non-decreasing order.
  3. Initialize a counter variable called count to 0.
  4. Iterate through the input array with index i. 4.1. If the element at index i in the input array is not equal to the element at index i in the expected array, increment the count.
  5. Return the count.
🌐 Data from online sources
#include <vector>
#include <algorithm>

int heightChecker(std::vector<int>& heights) {
    std::vector<int> expected = heights;
    std::sort(expected.begin(), expected.end());

    int count = 0;
    for (int i = 0; i < heights.size(); ++i) {
        if (heights[i] != expected[i]) {
            ++count;
        }
    }

    return count;
}
  1. Create a copy of the input array called expected.
  2. Sort the expected array in non-decreasing order.
  3. Initialize a counter variable called count to 0.
  4. Iterate through the input array with index i. 4.1. If the element at index i in the input array is not equal to the element at index i in the expected array, increment the count.
  5. Return the count.