Wednesday, July 3, 2024
HomeLanguagesPhpPhp Program to Rearrange array such that arr >= arr if i...

Php 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

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 1

Input : arr[] = {1, 2, 1, 4, 5, 6, 8, 8} 
Output : 4 5 2 6 1 8 1 8

The 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: 
 

PHP




<?php
// PHP program to rearrange the array
// as per the given condition
 
// function to rearrange the array
function rearrangeArr(&$arr, $n)
{
    // total even positions
    $evenPos = intval($n / 2);
 
    // total odd positions
    $oddPos = $n - $evenPos;
 
    $tempArr = array_fill(0, $n, NULL);
 
    // copy original array in an
    // auxiliary array
    for ($i = 0; $i < $n; $i++)
        $tempArr[$i] = $arr[$i];
 
    // sort the auxiliary array
    sort($tempArr);
 
    $j = $oddPos - 1;
 
    // fill up odd position in
    // original array
    for ($i = 0; $i < $n; $i += 2)
    {
        $arr[$i] = $tempArr[$j];
        $j--;
    }
 
    $j = $oddPos;
 
    // fill up even positions in
    // original array
    for ($i = 1; $i < $n; $i += 2)
    {
        $arr[$i] = $tempArr[$j];
        $j++;
    }
 
    // display array
    for ($i = 0; $i < $n; $i++)
        echo $arr[$i] ." ";
}
 
// Driver code
$arr = array(1, 2, 3, 4, 5, 6, 7 );
$size = sizeof($arr);
rearrangeArr($arr, $size);
 
// This code is contributed
// by ChitraNayal
?>


Output: 
 

4 5 3 6 2 7 1

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!
 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Nicole Veronica Rubhabha
Nicole Veronica Rubhabha
A highly competent and organized individual DotNet developer with a track record of architecting and developing web client-server applications. Recognized as a personable, dedicated performer who demonstrates innovation, communication, and teamwork to ensure quality and timely project completion. Expertise in C#, ASP.Net, MVC, LINQ, EF 6, Web Services, SQL Server, MySql, Web development,
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments