Write a function named removeBadPairs
that accepts as a parameter an array of numbers, and removes any
adjacent pair of numbers in the array where the left element of the pair is larger than the right element of the pair.
Every pair's left element is an even-numbered index in the array, and every pair's right element is an odd index in the array.
For example, suppose a variable named arr
stores the following element values:
[3, 7, 9.5, 2, 5, 5, 8.25, 5, 6, 3, 4, 7, 3, 1]
We can think of this array as a sequence of pairs:
[3, 7, 9, 2, 5, 5, 8, 5, 6, 3, 4, 7, 3, 1]
The pairs 9-2, 8-5, 6-3, and 3-1 are "bad" because the left element is larger than the right one, so these pairs should be removed. So the call of removeBadPairs(arr)
would change the array to store:
[3, 7, 5, 5, 4, 7]
If the array has an odd length, the last element is not part of a pair and is also considered "bad;" it should therefore be removed by your function.
If an empty array is passed in, the array should still be empty at the end of the call.
Constraints:
Do not use any other arrays to help solve this problem, though you can create as many simple variables as you like.