PHP Program For Rearranging An Array In Maximum Minimum Form - Set 2 (O(1) extra space)
Last Updated :
22 Jul, 2024
Given a sorted array of positive integers, this PHP program rearranges the array alternately: the first element is the maximum value, the second is the minimum value, the third is the second maximum, the fourth is the second minimum, and so on.
Examples:
Input: Arr = [ 1, 2, 3, 4, 5, 6, 7 ]
Output: [ 7, 1, 6, 2, 5, 3, 4 ]
Input: Arr = [ 1, 2, 3, 4, 5, 6 ]
Output: [ 6, 1, 5, 2, 4, 3 ]
We have discussed a solution in below post:
Rearrange an array in maximum minimum form | Set 1: The solution discussed here requires extra space, how to solve this problem with O(1) extra space.
In this post a solution that requires O(n) time and O(1) extra space is discussed. The idea is to use multiplication and modular trick to store two elements at an index.
even index : remaining maximum element.
odd index : remaining minimum element.
max_index : Index of remaining maximum element
(Moves from right to left)
min_index : Index of remaining minimum element
(Moves from left to right)
Initialize: max_index = 'n-1'
min_index = 0
// Can be any element which is more than
// the maximum value in array
max_element = arr[max_index] + 1
For i = 0 to n-1
If 'i' is even
arr[i] += (arr[max_index] % max_element *
max_element)
max_index--
// if 'i' is odd
ELSE
arr[i] += (arr[min_index] % max_element *
max_element)
min_index++
How does expression "arr[i] += arr[max_index] % max_element * max_element" work ?
The purpose of this expression is to store two elements at index arr[i]. arr[max_index] is stored as multiplier and "arr[i]" is stored as remainder. For example in {1 2 3 4 5 6 7 8 9}, max_element is 10 and we store 91 at index 0. With 91, we can get original element as 91%10 and new element as 91/10.
Below implementation of above idea:
PHP
<?php
// PHP program to rearrange an
// array in minimum-maximum form
// Prints max at first position, min
// at second position second max at
// third position, second min at fourth
// position and so on.
function rearrange(&$arr, $n) {
// Initialize index of first
// minimum and first maximum element
$max_idx = $n - 1; $min_idx = 0;
// Store maximum element of array
$max_elem = $arr[$n - 1] + 1;
// Traverse array elements
for ($i = 0; $i < $n; $i++) {
// At even index : we have to
// put maximum element
if ($i % 2 == 0) {
$arr[$i] += ($arr[$max_idx] %
$max_elem) * $max_elem;
$max_idx--;
}
// At odd index : we have to
// put minimum element
else {
$arr[$i] += ($arr[$min_idx] %
$max_elem) * $max_elem;
$min_idx++;
}
}
// Array elements back to
// it's original form
for ($i = 0; $i < $n; $i++)
$arr[$i] = (int)($arr[$i] / $max_elem);
}
// Driver Code
$arr = array(1, 2, 3, 4, 5, 6, 7, 8, 9);
$n = sizeof($arr);
echo "Original Array" . "";
for ($i = 0; $i < $n; $i++)
echo $arr[$i] . " ";
rearrange($arr, $n);
echo "\nModified Array";
for ($i = 0; $i < $n; $i++)
echo $arr[$i] . " ";
?>
OutputOriginal Array1 2 3 4 5 6 7 8 9
Modified Array9 1 8 2 7 3 6 4 5
Time Complexity: O(N), as we are using a loop to traverse N times.
Auxiliary Space: O(1), as we are not using any extra space.
Please refer complete article on Rearrange an array in maximum minimum form | Set 2 (O(1) extra space) for more details!
Similar Reads
PHP Program for Maximum and Minimum in a Square Matrix Given a square matrix of order n*n, find the maximum and minimum from the matrix given. Examples: Input : Arr = [ [ 5, 4, 9 ], [ 2, 0, 6 ], [ 3, 1, 8 ] ]; Output : Maximum = 9, Minimum = 0 Input : Arr = [[ -5, 3 ], [ 2, 4 ]]; Output : Maximum = 4, Minimum = -5Naive MethodWe find the maximum and mini
2 min read
PHP Program for Maximum sum of i*arr[i] among all rotations of a given array Given an array arr[] of n integers, find the maximum that maximizes the sum of the value of i*arr[i] where i varies from 0 to n-1.Examples: Input: arr[] = {8, 3, 1, 2} Output: 29 Explanation: Lets look at all the rotations, {8, 3, 1, 2} = 8*0 + 3*1 + 1*2 + 2*3 = 11 {3, 1, 2, 8} = 3*0 + 1*1 + 2*2 + 8
7 min read
PHP Program for Number of local extrema in an array You are given an array on n-elements. An extrema is an element that is either greater than its both of neighbors or less than its both neighbors. You have to calculate the number of local extrema in given array. Note: 1st and last elements are not extrema.Examples : Input : a[] = {1, 5, 2, 5}Output
2 min read
PHP Program for Maximum Equilibrium Sum in an Array Given an array arr[]. Find the maximum value of prefix sum which is also suffix sum for index i in arr[].Examples: Input : arr[] = {-1, 2, 3, 0, 3, 2, -1}Output : 4Prefix sum of arr[0..3] = Suffix sum of arr[3..6]Input : arr[] = {-2, 5, 3, 1, 2, 6, -4, 2}Output : 7Prefix sum of arr[0..3] = Suffix su
3 min read
PHP Program for Minimum Product Subset of an Array Given an array Arr, we have to find the minimum product possible with the subset of elements present in the array. The minimum product can be a single element also.Examples: Input : Arr = [ -1, -1, -2, 4, 3 ] Output : -24 Explanation : Minimum product will be ( -2 * -1 * -1 * 4 * 3 ) = -24 Input : A
3 min read
PHP Program for Range Queries for Frequencies of Array Elements Given an array of n non-negative integers. The task is to find the frequency of a particular element in the arbitrary range of array[]. The range is given as positions (not 0 based indexes) in array. There can be multiple queries of given type. Examples: Input : arr[] = {2, 8, 6, 9, 8, 6, 8, 2, 11};
2 min read
PHP Program For Counting Inversions In An Array - Set 1 (Using Merge Sort) Inversion Count for an array indicates - how far (or close) the array is from being sorted. If the array is already sorted, then the inversion count is 0, but if the array is sorted in the reverse order, the inversion count is the maximum. Formally speaking, two elements a[i] and a[j] form an invers
2 min read
PHP Program for Search an element in a sorted and rotated array An element in a sorted array can be found in O(log n) time via binary search. But suppose we rotate an ascending order sorted array at some pivot unknown to you beforehand. So for instance, 1 2 3 4 5 might become 3 4 5 1 2. Devise a way to find an element in the rotated array in O(log n) time.Exampl
6 min read
PHP Program for Reversal algorithm for right rotation of an array Given an array, right rotate it by k elements. After K=3 rotation Examples: Input: arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} k = 3Output: 8 9 10 1 2 3 4 5 6 7Input: arr[] = {121, 232, 33, 43 ,5} k = 2Output: 43 5 121 232 33Note: In the below solution, k is assumed to be smaller than or equal to n. We
2 min read
PHP Program for Check if an array is sorted and rotated Given an array of N distinct integers. The task is to write a program to check if this array is sorted and rotated counter-clockwise. A sorted array is not considered as sorted and rotated, i.e., there should at least one rotation.Examples: Input : arr[] = { 3, 4, 5, 1, 2 }Output : YESThe above arra
3 min read