Valid Mountain Array

🏠 ⬅️ ➡️

Given an array of integers arr, return true if and only if it is a valid mountain array.

Recall that arr is a mountain array if and only if:

  • arr.length >= 3
  • There exists some i with 0 < i < arr.length - 1 such that:
    • arr[0] < arr[1] < ... < arr[i - 1] < arr[i]
    • arr[i] > arr[i + 1] > ... > arr[arr.length - 1]

Example 1:

Input: arr = [2,1] Output: false

Example 2:

Input: arr = [3,5,5] Output: false

Example 3:

Input: arr = [0,3,2,1] Output: true

Constraints:

  • 1 <= arr.length <= 104
  • 0 <= arr[i] <= 104

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

! This program solves the problem described in the comment at the top of the file.

implicit none
integer :: arr(10)
logical :: result

! Example 1:
arr = [2, 1]
result = is_mountain_array(arr)
print '(L1)', result

! Example 2:
arr = [3, 5, 5]
result = is_mountain_array(arr)
print '(L1)', result

! Example 3:
arr = [0, 3, 2, 1]
result = is_mountain_array(arr)
print '(L1)', result

contains

function is_mountain_array(arr) result(res)
implicit none
integer, intent(in) :: arr(:)
logical :: res
integer :: i

if (size(arr) < 3) then
    res = .false.
else
    do i = 2, size(arr) - 1
        if (arr(i-1) < arr(i) .and. arr(i) > arr(i+1)) exit
    end do
    res = (i /= size(arr))
end if

end function is_mountain_array

end program main
Compiled
Executed
Correct
! This is a full Fortran script with a valid main entry point that solves the problem
! of checking if an array is a valid mountain array.

! The program should run with all provided examples and output to stdout.

! Given an array of integers arr, return true if and only if it is a valid mountain array.
! Recall that arr is a mountain array if and only if:
! 
! * arr.length >= 3
! * There exists some i with 0 < i < arr.length - 1 such that:
!   * arr[0] < arr[1] < ... < arr[i - 1] < arr[i]
!   * arr[i] > arr[i + 1] > ... > arr[arr.length - 1]

! Example 1:
! Input: arr = [2,1]
! Output: false

! Example 2:
! Input: arr = [3,5,5]
! Output: false

! Example 3:
! Input: arr = [0,3,2,1]
! Output: true

! Constraints:
! 1 <= arr.length <= 104
! 0 <= arr[i] <= 104

! This solution uses a simple approach of iterating through the array and checking
! for the conditions of a mountain array.

! The main entry point of the program.
program mountain_array
    implicit none

    ! Declare the variables.
    integer, dimension(:), allocatable :: arr
    integer :: i, length
    logical :: result

    ! Read the input array from the console.
    read (*, *) arr

    ! Get the length of the array.
    length = size(arr)

    ! Check if the array is a mountain array.
    result = mountain_array_helper(arr, length)

    ! Print the result to the console.
    write (*, *) result

contains

! A helper function to check if an array is a mountain array.
function mountain_array_helper(arr, length) result(is_mountain_array)
    implicit none

    ! Declare the variables.
    integer, dimension(:), intent(in) :: arr
    integer, intent(in) :: length
    logical :: is_mountain_array

    ! Initialize the variables.
    is_mountain_array = .false.

    ! Check if the array is long enough to be a mountain array.
    if (length < 3) then
        return
    end if

    ! Find the first index where the array is not increasing.
    do i = 2, length - 1
        if (arr(i) < arr(i - 1)) then
            exit
        end if
    end do

    ! Check if the array is not decreasing from the first index to the end.
    if (i == length - 1) then
        return
    end if

    ! Check if the array is not increasing from the first index to the second index.
    if (arr(i) > arr(i - 1)) then
        return
    end if

    ! Check if the array is decreasing from the second index to the end.
    do i = i + 1, length - 1
        if (arr(i) < arr(i - 1)) then
            return
        end if
    end do

    ! If the array is not decreasing from the second index to the end, then it is a mountain array.
    is_mountain_array = .true.

end function mountain_array_helper
end program mountain_array
🌐 Data from online sources
def move_even_odd(nums):
    even_index = 0
    odd_index = len(nums) - 1

    while even_index < odd_index:
        if nums[even_index] % 2 == 0:
            even_index += 1
        else:
            nums[even_index], nums[odd_index] = nums[odd_index], nums[even_index]
            odd_index -= 1

    return nums

The algorithm uses two pointers, evenIndex and oddIndex. evenIndex starts from the beginning of the array, whereas oddIndex starts from the end. We iterate through the array until evenIndex is greater than or equal to oddIndex. Inside the loop, if nums[evenIndex] is even, we increment evenIndex because it's already in the correct position. If nums[evenIndex] is odd, we swap it with nums[oddIndex] and decrement oddIndex. Thus, we move even numbers to the beginning and odd numbers to the end of the array.

🌐 Data from online sources
#include <vector>
using namespace std;

void moveEvenOdd(vector<int>& nums) {
    int evenIndex = 0;
    int oddIndex = nums.size() - 1;

    while (evenIndex < oddIndex) {
        if (nums[evenIndex] % 2 == 0) {
            evenIndex++;
        } else {
            swap(nums[evenIndex], nums[oddIndex]);
            oddIndex--;
        }
    }
}

The algorithm uses two pointers, evenIndex and oddIndex. evenIndex starts from the beginning of the array, whereas oddIndex starts from the end. We iterate through the array until evenIndex is greater than or equal to oddIndex. Inside the loop, if nums[evenIndex] is even, we increment evenIndex because it's already in the correct position. If nums[evenIndex] is odd, we swap it with nums[oddIndex] and decrement oddIndex. Thus, we move even numbers to the beginning and odd numbers to the end of the array.