Camel
Peter
Peter Campbell Smith

Thousands of mountains

Weekly challenge 355 — 5 January 2026

Week 355: 5 Jan 2026

Task 2

Task — Mountain array

You are given an array of integers, @ints. Write a script to return true if the given array is a valid mountain array. An array is a mountain if and only if:

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

Examples


Example 1
Input: @ints = (1, 2, 3, 4, 5)
Output: false

Example 2
Input: @ints = (0, 2, 4, 6, 4, 2, 0)
Output: true

Example 3
Input: @ints = (5, 4, 3, 2, 1)
Output: false

Example 4
Input: @ints = (1, 3, 5, 5, 4, 2)
Output: false

Example 5
Input: @ints = (1, 3, 2)
Output: true

Analysis

So here we are at the bottom of an alleged mountain.

We can proceed so long as the next step is up.

Oops! The next step is the same height as this, but mountains don't have flat bits so it isn't a mountain.

The next step is down, so we're at the summit.

If we meet a down or level step on the way down then it isn't a mountain ... or else it is!

There's probably a more concise way to code it, but this works and needs only a single scan of the array.

Try it 

Try running the script with any input:



example: 1, 2, 3, 4, 5, 3, 1

Script


#!/usr/bin/perl

# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge

use v5.26;    # The Weekly Challenge - 2026-01-05
use utf8;     # Week 355 - task 2 - Mountain array
use warnings; # Peter Campbell Smith
binmode STDOUT, ':utf8';
use Encode;

mountain_array(1, 2, 3, 4, 5);       # no down steps
mountain_array(0, 2, 4, 6, 4, 2, 0); # good
mountain_array(5, 4, 3, 2, 1);       # no up steps
mountain_array(1, 3, 2);             # minimal good
mountain_array(1, 3, 5, 5, 4, 2);    # repeated up step
mountain_array(1, 2, 3, 2, 1, 1);    # repeated down step
mountain_array(1, 1, 2, 3, 2, 1);    # repeated up step

sub mountain_array {
    
    my (@height, $going, $result, $j);
    
    # initialise
    @height = @_;
    $going = 'up';
    $result = 'true';
    
    # start climbing
    STEP: for $j (1 .. $#height) {
        if ($going eq 'up') {
            
            # next step is up - good
            if ($height[$j] > $height[$j - 1]) {

            # next step is level or first step isn't up
            } elsif ($height[$j] == $height[$j - 1] or $j == 1) {
                $result = 'false';
                last STEP;
                
            # reached the summit
            } else {
                $going = 'down';
            }
        
        # going down
        } else {
            
            # next step isn't downward
            if ($height[$j] >= $height[$j - 1]) {
                $result = 'false';
                last STEP;
            }
        }   
    }
    
    # report
    say qq[\nInput:  (] . join(', ', @height) . ')';
    say qq[Output: ] . ($going eq 'down' ? $result : 'false');
}

Output


Input:  (1, 2, 3, 4, 5)
Output: false

Input:  (0, 2, 4, 6, 4, 2, 0)
Output: true

Input:  (5, 4, 3, 2, 1)
Output: false

Input:  (1, 3, 2)
Output: true

Input:  (1, 3, 5, 5, 4, 2)
Output: false

Input:  (1, 2, 3, 2, 1, 1)
Output: false

Input:  (1, 1, 2, 3, 2, 1)
Output: false

 

Any content of this website which has been created by Peter Campbell Smith is in the public domain