Given an unsorted array of integers, sort the array into a wave like array. An array ‘arr[0..n-1]’ is sorted in wave form if arr[0] >= arr[1] <= arr[2] >= arr[3] <= arr[4] >= …..
Input: arr[] = {10, 5, 6, 3, 2, 20, 100, 80}
Output: arr[] = {10, 5, 6, 2, 20, 3, 100, 80} OR
{20, 5, 10, 2, 80, 6, 100, 3} OR
any other array that is in wave form
Input: arr[] = {20, 10, 8, 6, 4, 2}
Output: arr[] = {20, 8, 10, 4, 6, 2} OR
{10, 8, 20, 2, 6, 4} OR
any other array that is in wave form
def sortwave(L): n=len(L) L.sort() for i in range(0,n-1,2): L[i], L[i+1] = L[i+1], L[i] Array_nums = list(map(int,input('Array_nums : ').split(','))) sortwave(Array_nums) print(Array_nums)
Input_1:
Array_nums : 10,90,49,2,1,5,23
Output:
[2, 1, 10, 5, 49, 23, 90]
Input_2:
Array_nums : 10,5,6,3,2,20,100,80
Output:
[3, 2, 6, 5, 20, 10, 100, 80]
Input_3:
Array_nums : 20,10,8,6,4,2
Output:
[4, 2, 8, 6, 20, 10]
Input_4:
Array_nums : 2,4,6,8,10,20
Output:
[4, 2, 8, 6, 20, 10]
Input_5:
Array_nums : 3,6,5,10,7,20
Output:
[5, 3, 7, 6, 20, 10]
Illustration of the execution of code
More Q
- Find the majority element appearing more than n/2 times
- Return an array of the squares of each number.
- Highest factor in addition.
- Find the empty packets(0) of chocolate and push it to the end of the conveyor belt(array).
- Every element in array appears twice.Find that single one.
- Find the duplicate letters in string. Same to do in dictionary way.
- Find minimum number of edits (operations) required to convert ‘str1’ into ‘str2’.
- Return the maximum number you can get by changing at most one digit.
- Sed Commands – Ranges of lines.
- Find the kth smallest element in the given 2D array.
- Find all elements that appear more than [n/3] times.
- Write a function to return true if s2 contains the permutation of s1.
- Sort the array into a wave like array.
- Re-order array, such that
nums[0] < nums[1] > nums[2] < nums[3]...
. - Given the coordinates, return true if the four points construct a square.
- Minimum Distance Between Words of a String.
- Return the shortest distance between these two words.
- Write a program to find the n-th ugly number.
- Check if array could become Non – Decreasing array.
- Find the minimum area of a rectangle that can be formed from given points.