Question
In an array of integers, a peak is an element which is greater than or equal to the adjacent integers in the array, and a
In an array of integers, a "peak" is an element which is greater than or equal to the adjacent integers in the array, and a "valley" is an element which is less than or equal to the adjacent integers. For example, in the array [5, 8, 6, 2, 3, 4, 6], the elements 8 and the final 6 are peaks, the elements 5 and 2 are valleys, and the elements 6, 3 and 4 are neither peaks nor valleys.
Given a Python list of integers, return the list sorted into an alternating sequence of peaks and valleys. Note that there can be more than one possible valid output for a given input.
Example:
Input: [5, 3, 1, 2, 3]
Output: [5, 1, 3, 2, 3]
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started