Count Elements With Strictly Smaller and Greater Elements

🏠 ⬅️ ➡️

Given an integer array nums, return the number of elements that have both a strictly smaller and a strictly greater element appear in nums.

Example 1:

Input: nums = [11,7,2,15] Output: 2 Explanation: The element 7 has the element 2 strictly smaller than it and the element 11 strictly greater than it. Element 11 has element 7 strictly smaller than it and element 15 strictly greater than it. In total there are 2 elements having both a strictly smaller and a strictly greater element appear in nums.

Example 2:

Input: nums = [-3,3,3,90] Output: 2 Explanation: The element 3 has the element -3 strictly smaller than it and the element 90 strictly greater than it. Since there are two elements with the value 3, in total there are 2 elements having both a strictly smaller and a strictly greater element appear in nums.

Constraints:

  • 1 <= nums.length <= 100
  • -105 <= nums[i] <= 105

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

    integer, parameter :: n = 4
    integer, dimension(n) :: nums
    integer :: i, j, count

    ! Example 1
    nums(1) = 11
    nums(2) = 7
    nums(3) = 2
    nums(4) = 15
    write(*,*) 'Example 1:'
    write(*,*) 'Input: ', nums
    count = count_strictly_smaller_and_greater(nums)
    write(*,*) 'Output: ', count
    write(*,*) ''

    ! Example 2
    nums(1) = -3
    nums(2) = 3
    nums(3) = 3
    nums(4) = 90
    write(*,*) 'Example 2:'
    write(*,*) 'Input: ', nums
    count = count_strictly_smaller_and_greater(nums)
    write(*,*) 'Output: ', count
    write(*,*) ''

contains

    function count_strictly_smaller_and_greater(nums) result(count)
        implicit none
        integer, intent(in) :: nums(:)
        integer :: count
        logical :: found_smaller, found_greater

        count = 0
        do i = 1, size(nums)-1
            found_smaller = .false.
            found_greater = .false.
            do j = i+1, size(nums)
                if (nums(j) < nums(i)) then
                    found_smaller = .true.
                end if
                if (nums(j) > nums(i)) then
                    found_greater = .true.
                end if
                if (found_smaller .and. found_greater) then
                    count = count + 1
                    exit
                end if
            end do
        end do
    end function count_strictly_smaller_and_greater

end program main
Compiled
Executed
Correct
```
module count_strictly_increasing_elements_module
    implicit none

contains

    function count_strictly_increasing_elements(nums) result(count)
        integer, intent(in) :: nums(:)
        integer :: count
        integer :: i, j

        count = 0
        do i = 2, size(nums)
            do j = 1, i - 1
                if (nums(i) > nums(j) .and. nums(i) > nums(j + 1)) then
                    count = count + 1
                end if
            end do
        end do
    end function count_strictly_increasing_elements
end module count_strictly_increasing_elements_module

program main
    use count_strictly_increasing_elements_module
    implicit none

    integer, parameter :: nums1(4) = [11, 7, 2, 15]
    integer, parameter :: nums2(4) = [-3, 3, 3, 90]
    integer :: count

    count = count_strictly_increasing_elements(nums1)
    write (*, '(A, I0)') 'Example 1: ', count

    count = count_strictly_increasing_elements(nums2)
    write (*, '(A, I0)') 'Example 2: ', count
end program main
```
🌐 Data from online sources
def min_moves(seats, students):
    seats.sort()
    students.sort()

    moves = 0
    for i in range(len(seats)):
        moves += abs(seats[i] - students[i])
    return moves
  1. Sort both the seats and students arrays in ascending order. This ensures that students will be paired up with the nearest available seat.
  2. Initialize a variable moves to store the total number of moves.
  3. Iterate through both arrays simultaneously using a single index.
  4. In each iteration, add the absolute difference between the current seat and student position to the moves variable.
  5. After the iteration, return the moves variable for the minimum number of moves required.

Note: This solution assumes that the input arrays have the same length and are non-empty.

🌐 Data from online sources
#include <algorithm>
#include <vector>

int min_moves(std::vector<int>& seats, std::vector<int>& students) {
    std::sort(seats.begin(), seats.end());
    std::sort(students.begin(), students.end());

    int moves = 0;
    for (int i = 0; i < seats.size(); i++) {
        moves += std::abs(seats[i] - students[i]);
    }
    return moves;
}
  1. Sort both the seats and students arrays in ascending order. This ensures that students will be paired up with the nearest available seat.
  2. Initialize a variable moves to store the total number of moves.
  3. Iterate through both arrays simultaneously using a single index.
  4. In each iteration, add the absolute difference between the current seat and student position to the moves variable.
  5. After the iteration, return the moves variable for the minimum number of moves required.

Note: This solution assumes that the input arrays have the same length and are non-empty.