Kotlinlearncs.online LogoJava
Return to List

Solve: Array Is Valley

Created By: Pranav Narayanan
/ Version: 2025.2.0

Write a function isValley that takes an IntArray? and returns true if and only if it is a valley. An array is a valley if it consists of a single run of decreasing numbers joined with a single run of increasing numbers. For example {1, 0, 1} is a valley, since it has the decreasing run {1, 0} joined with the increasing run {0, 1}.

The runs need not be strictly increasing or strictly decreasing. For example, {1, 1, 0, 0, 1, 1} is a valley, since it consists of {1, 1, 0, 0} joined with {0, 0, 1, 1}. However, there must be at least one increase and one decrease in the array for it to qualify. {1, 1, 0}, {0, 1, 1}, and {1, 1, 1} should all fail under this condition.

Notice that these conditions require arrays of at least length 3. If your array is shorter than this or null, return false.

Related Lessons

Stuck? You may find these lessons helpful:

Report a Problem

Attribution must link to this page: https://www.learncs.online/solve/kotlin/array-is-valley/[email protected]