Javascript Program to Rearrange array such that arr[i] >= arr[j] if i is even and arr[i]<=arr[j] if i is odd and j < i Last Updated : 19 Sep, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Given an array of n elements. Our task is to write a program to rearrange the array such that elements at even positions are greater than all elements before it and elements at odd positions are less than all elements before it.Examples: Input : arr[] = {1, 2, 3, 4, 5, 6, 7}Output : 4 5 3 6 2 7 1Input : arr[] = {1, 2, 1, 4, 5, 6, 8, 8} Output : 4 5 2 6 1 8 1 8The idea to solve this problem is to first create an auxiliary copy of the original array and sort the copied array. Now total number of even position in array with n elements will be floor(n/2) and remaining is the number of odd positions. Now fill the odd and even positions in the original array using the sorted array in the below manner: Total odd positions will be n - floor(n/2). Start from (n-floor(n/2))th position in the sorted array and copy the element to 1st position of sorted array. Start traversing the sorted array from this position towards left and keep filling the odd positions in the original array towards right.Start traversing the sorted array starting from (n-floor(n/2)+1)th position towards right and keep filling the original array starting from 2nd position. Below is the implementation of above idea: JavaScript // Javascript program to rearrange the array // as per the given condition // function to rearrange the array function rearrangeArr(arr, n) { // total even positions let evenPos = Math.floor(n / 2); // total odd positions let oddPos = n - evenPos; let tempArr = new Array(n); // copy original array in an // auxiliary array for (let i = 0; i < n; i++) tempArr[i] = arr[i]; // sort the auxiliary array tempArr.sort(); let j = oddPos - 1; // fill up odd position in original // array for (let i = 0; i < n; i += 2) { arr[i] = tempArr[j]; j--; } j = oddPos; // fill up even positions in original // array for (let i = 1; i < n; i += 2) { arr[i] = tempArr[j]; j++; } //print array in one line console.log(arr.join(' ')) } // Driver code let arr = [1, 2, 3, 4, 5, 6, 7]; let size = arr.length; rearrangeArr(arr, size); Output4 5 3 6 2 7 1 Complexity Analysis:Time Complexity: O(N*logN), as we are using a sort function.Auxiliary Space: O(N), as we are using extra space.Please refer complete article on Rearrange array such that arr[i] >= arr[j] if i is even and arr[i]<=arr[j] if i is odd and j < i for more details! Comment More infoAdvertise with us Next Article Check if an array arr[] can be rearranged such that arr[2 à i + 1] = 2* arr[2 à i] for every ith index K kartik Follow Improve Article Tags : JavaScript Sorting Quiz array-rearrange Similar Reads Javascript Program For Rearranging A Linked List Such That All Even And Odd Positioned Nodes Are Together Rearrange a linked list in such a way that all odd position nodes are together and all even positions node are together, Examples: Input: 1->2->3->4Output: 1->3->2->4Input: 10->22->30->43->56->70Output: 10->30->56->22->43->70The important thing in this 3 min read Check if an array arr[] can be rearranged such that arr[2 à i + 1] = 2* arr[2 à i] for every ith index Given an array arr[] consisting of 2*N integers, the task is to check if it is possible to rearrange the array elements such that arr[2 * i + 1] = 2* arr[2 * i] for every ith index. If it is possible to do so, then print "Yes. Otherwise, print "No". Examples: Input: arr[] = {4, -2, 2, -4}, N = 2Outp 7 min read Javascript Program to Rotate all odd numbers right and all even numbers left in an Array of 1 to N Given a permutation arrays A[] consisting of N numbers in range [1, N], the task is to left rotate all the even numbers and right rotate all the odd numbers of the permutation and print the updated permutation. Note: N is always even.Examples: Input: A = {1, 2, 3, 4, 5, 6, 7, 8} Output: {7, 4, 1, 6 2 min read Rearrange given Array such that each element raised to its index is odd Given an array arr of length N, the task is to rearrange the elements of given array such that for each element, its bitwise XOR with its index is an odd value. If no rearrangement is possible return -1. Example: Input: arr[] = {1 2 4 3 5}Output: 1 2 3 4 5Explanation: In the above array:for 1st elem 13 min read Javascript Program For Rearranging An Array In Maximum Minimum Form - Set 2 (O(1) extra space) Given a sorted array of positive integers, rearrange the array alternately i.e first element should be the maximum value, second minimum value, third-second max, fourth-second min and so on. Examples:Input: arr[] = {1, 2, 3, 4, 5, 6, 7} Output: arr[] = {7, 1, 6, 2, 5, 3, 4}Input: arr[] = {1, 2, 3, 4 5 min read Javascript Program to Rearrange positive and negative numbers in O(n) time and O(1) extra space An array contains both positive and negative numbers in random order. Rearrange the array elements so that positive and negative numbers are placed alternatively. Number of positive and negative numbers need not be equal. If there are more positive numbers they appear at the end of the array. If the 3 min read Like