Kotlinlearncs.online LogoJava
Return to List

Solve: Count Decreasing Sequences

Created By: Rohith Sanjay
/ Version: 2024.9.0

Write a method caled numDecrease which takes in one argument: an IntArray? called input. Your solution returns the number (an Int) of decreasing sequences in input. A decreasing sequence is a sequence (2 or more consecutive elements) in input where element n+1 has a value strictly less than element n. Things to note: if element n+1 is equal to element n, then this represents two different sequences. If input is null or empty, return 0. For example, the array [1, 2, 3, 2, 1, 2, 3, 4, 3, 2, 1, 2] has 2 decreasing sequences (decreasing sequences in bold). The array [1, 2, 3, 3, 4, 5, 5, 5, 6] has no decreasing sequences. The array [8, 7, 6, 6, 5, 4, 3, 3, 2] has 3 decreasing sequences ([8, 7, 6], [6, 5, 4, 3], and [3, 2]). The array [1] has no decreasing sequences, but [3, 2] has 1 decreasing sequence. Some Tips: Loop through the array in pairs of elements. Look at element n and element n+1 to determine if the current pair is increasing or decreasing. Also, keep track of when a decreasing sequence starts, meaning you need to keep track of whether the current sequence is decreasing or not.

Related Lessons

Stuck? You may find these lessons helpful:

Report a Problem

Attribution must link to this page: https://www.learncs.online/solve/kotlin/count-decreasing-sequences/[email protected]