The Longest Spike
Learn this problemProblem statement
We will call a sequence of integers a spike if they first
increase (strictly) and then decrease (also strictly, including the last element of the increasing
part). For example (4, 5, 7, 6, 3, 2) is a spike, but
(1, 1, 5, 4, 3 and (1, 4, 3, 5) are not.
Note that the increasing and decreasing parts always intersect, e.g.: for spike
(3, 5, 2) sequence (3, 5) is an increasing
part and sequence (5, 2) is a decreasing part, and for
spike (2) sequence (2) is both an
increasing and a descreasing part.
You are given an array A of N integers. Your task is to calculate the length of the longest possible spike, which can be created from numbers from array A. Note that you are NOT supposed to find the longest spike as a sub-sequence of A, but rather choose some numbers from A and reorder them to create the longest spike.
Given an array A of integers of length N, returns the length of the longest spike which can be created from the numbers from A.
Desmond rocks! 🤘
Function
longestSpike(A: int[]) → intExamples
Example 1
A = [1, 2]return = 2Example 2
A = [2, 5, 3, 2, 4, 1]return = 6More Microsoft problems
- Authentication SystemOA · Seen Jul 2026
- Binary String Swap TimeOA · Seen Jul 2026
- Minimum Effort Task ScheduleOA · Seen Jul 2026
- Maximum Pipeline ThroughputOA · Seen Jul 2026
- Maximum Strong Team SubarrayOA · Seen Jul 2026
- Minimum Cost K-Capable ModelsOA · Seen Jul 2026
- Alphabetically Smallest PalindromeOA · Seen Jul 2026
- Maximum Reward PointsOA · Seen Jul 2026