For this challenge, you will determine if numbers in an array can be sorted in a particular way. Have the function WaveSorting(arr)
take the array of positive integers (can contain duplicate) stored in arr
and return the string true if the numbers can be arranged in a wave pattern: a1 > a2 < a3 > a4 < a5 > ...
, otherwise, return the string false. For example, if arr
is: [0, 1, 2, 4, 1, 4]
, then a possible wave ordering of the numbers is: [2, 0, 4, 1, 4, 1]
. So for this input your program should return the string true. The input array will always contain at least 2 elements.
How to solve this problem? How to prove the correctness?