Rearrange a given list such that it consists of alternating minimum maximum elements - GeeksforGeeks
Given a list of integers, rearrange the list such that it consists of alternating minimum maximum elements using only list operations. The first element of the list should be minimum and second element should be maximum of all elements present in the list. Similarly, third element will be next minimum element and fourth element is next maximum element and so on. Use of extra space is not permitted.
Rearrange an array in maximum minimum form | Set 1
http://www.geeksforgeeks.org/rearrange-array-maximum-minimum-form-set-2-o1-extra-space/
Read full article from Rearrange a given list such that it consists of alternating minimum maximum elements - GeeksforGeeks
Given a list of integers, rearrange the list such that it consists of alternating minimum maximum elements using only list operations. The first element of the list should be minimum and second element should be maximum of all elements present in the list. Similarly, third element will be next minimum element and fourth element is next maximum element and so on. Use of extra space is not permitted.
Input: [1 3 8 2 7 5 6 4] Output: [1 8 2 7 3 6 4 5]
The idea is to sort the list in ascending order first. Then we start popping elements from the end of the list and insert them into their correct position in the list.
void
alternateSort(list<
int
>& inp)
{
// sort the list in ascending order
inp.sort();
// get iterator to first element of the list
list<
int
>::iterator it = inp.begin();
it++;
for
(
int
i=1; i<(inp.size() + 1)/2; i++)
{
// pop last element (next greatest)
int
val = inp.back();
inp.pop_back();
// insert it after next minimum element
inp.insert(it, val);
// increment the pointer for next pair
++it;
}
}
Rearrange an array in maximum minimum form | Set 1
Given a sorted array of positive integers, rearrange the array alternately i.e first element should be maximum value, second minimum value, third second max, fourth second min and so on.
void
rearrange(
int
arr[],
int
n)
{
// Auxiliary array to hold modified array
int
temp[n];
// Indexes of smallest and largest elements
// from remaining array.
int
small=0, large=n-1;
// To indicate whether we need to copy rmaining
// largest or remaining smallest at next position
int
flag =
true
;
// Store result in temp[]
for
(
int
i=0; i<n; i++)
{
if
(flag)
temp[i] = arr[large--];
else
temp[i] = arr[small++];
flag = !flag;
}
// Copy temp[] to arr[]
for
(
int
i=0; i<n; i++)
arr[i] = temp[i];
}
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) max_index : Index of remaining minimum element (Moves from left to right) Initialize: max_index = 'n-1' min_index = 0 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-- ELSE // if 'i' is odd 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.
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.
void
rearrange(
int
arr[],
int
n)
{
// initialize index of first minimum and first
// maximum element
int
max_idx = n-1 , min_idx = 0 ;
// store maximum element of array
int
max_elem = arr[n-1] + 1 ;
// traverse array elements
for
(
int
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
(
int
i = 0 ; i < n; i++)
arr[i] = arr[i] / max_elem ;
}
Read full article from Rearrange a given list such that it consists of alternating minimum maximum elements - GeeksforGeeks