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
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
Example 1: Input: 11 7 2 15 Output: 2 Example 2: Input: -3 3 3 90 Output: 0
```
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
```
temp.f95:1:2: 1 | ``` | 1 Error: Invalid character in name at (1) temp.f95:37:2: 37 | ``` | 1 Error: Invalid character in name at (1)
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
seats
and students
arrays in ascending order.
This ensures that students will be paired up with the nearest available seat.moves
to store the total number of moves.moves
variable.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.
#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;
}
seats
and students
arrays in ascending order.
This ensures that students will be paired up with the nearest available seat.moves
to store the total number of moves.moves
variable.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.