Arrays January 13 ,2026

Rearrange Array Alternately (Max–Min)

Problem Statement

Given a sorted array, rearrange its elements in-place in such a way that:

  • The first element is the maximum
  • The second element is the minimum
  • The third element is the second maximum
  • The fourth element is the second minimum
  • And so on…

Constraint: Must be done in O(n) time and O(1) extra space if possible.

Example 1

Input

arr = [1, 2, 3, 4, 5, 6]

Output

[6, 1, 5, 2, 4, 3]

Explanation

Max → 6, Min → 1, 2nd Max → 5, 2nd Min → 2, ...

Example 2

Input

arr = [10, 20, 30, 40, 50]

Output

[50, 10, 40, 20, 30]

Why This Problem Is Important

  • Tests understanding of array indexing
  • Evaluates in-place modification skills
  • Introduces the concept of encoding multiple numbers in a single array slot
  • Common in interview and competitive programming

Approaches to Solve the Problem

  1. Using Extra Array (Simplest)
  2. In-Place Encoding Method (Optimized, O(1) space)

Approach 1: Using Extra Array

Idea

  • Use two pointers:
    • start = 0 → minimum element
    • end = n-1 → maximum element
  • Place elements alternatively in a new array
  • Copy back to original array

Algorithm

  1. Initialize start = 0, end = n-1
  2. Create result[n]
  3. Loop i = 0 to n-1:
    • If i is even → result[i] = arr[end--]
    • Else → result[i] = arr[start++]
  4. Copy result back to arr

Time and Space Complexity

  • Time Complexity: O(n)
  • Space Complexity: O(n)

C++ Implementation

#include <iostream>
using namespace std;

int main() {
    int arr[] = {1, 2, 3, 4, 5, 6};
    int n = 6;
    int result[n];
    int start = 0, end = n - 1;

    for(int i = 0; i < n; i++) {
        if(i % 2 == 0)
            result[i] = arr[end--];
        else
            result[i] = arr[start++];
    }

    for(int i = 0; i < n; i++)
        arr[i] = result[i];

    for(int i = 0; i < n; i++)
        cout << arr[i] << " ";

    return 0;
}

Output

6 1 5 2 4 3


JavaScript

let arr = [1, 2, 3, 4, 5, 6];
let n = arr.length;

let result = [];
let start = 0;
let end = n - 1;

for (let i = 0; i < n; i++) {
    if (i % 2 === 0) {
        result[i] = arr[end--]; // Pick max
    } else {
        result[i] = arr[start++]; // Pick min
    }
}

// Copy back to original array
for (let i = 0; i < n; i++) {
    arr[i] = result[i];
}

console.log(arr);

Output

[6, 1, 5, 2, 4, 3]
ReArrange Array Alternately Solution

Approach 2: In-Place Encoding (O(1) Space)

Idea

  • Store two numbers in one element using modulus encoding
  • Formula:
arr[i] = arr[i] + (arr[max_idx] % max_element) * max_element   // For max positions
arr[i] = arr[i] + (arr[min_idx] % max_element) * max_element   // For min positions
  • After encoding, divide each element by max_element to decode

Step 1: Find max_element = arr[n-1] + 1
Step 2: Use two pointers min_idx and max_idx
Step 3: Encode values
Step 4: Decode by dividing each element by max_element

Algorithm

  1. max_idx = n-1, min_idx = 0
  2. max_elem = arr[n-1] + 1
  3. For i = 0 to n-1:
    • If i % 2 == 0 → arr[i] += (arr[max_idx] % max_elem) * max_elem; max_idx--
    • Else → arr[i] += (arr[min_idx] % max_elem) * max_elem; min_idx++
  4. Divide each element by max_elem

Time and Space Complexity

  • Time Complexity: O(n)
  • Space Complexity: O(1)

C++ Implementation (In-Place)

#include <iostream>
using namespace std;

int main() {
    int arr[] = {1, 2, 3, 4, 5, 6};
    int n = 6;

    int max_idx = n - 1, min_idx = 0;
    int max_elem = arr[n-1] + 1;

    for(int i = 0; i < n; i++) {
        if(i % 2 == 0) {
            arr[i] += (arr[max_idx] % max_elem) * max_elem;
            max_idx--;
        } else {
            arr[i] += (arr[min_idx] % max_elem) * max_elem;
            min_idx++;
        }
    }

    for(int i = 0; i < n; i++)
        arr[i] = arr[i] / max_elem;

    for(int i = 0; i < n; i++)
        cout << arr[i] << " ";

    return 0;
}

Output

6 1 5 2 4 3

Python Implementation (In-Place)

arr = [1, 2, 3, 4, 5, 6]
n = len(arr)
max_idx = n - 1
min_idx = 0
max_elem = arr[-1] + 1

for i in range(n):
    if i % 2 == 0:
        arr[i] += (arr[max_idx] % max_elem) * max_elem
        max_idx -= 1
    else:
        arr[i] += (arr[min_idx] % max_elem) * max_elem
        min_idx += 1

for i in range(n):
    arr[i] = arr[i] // max_elem

print(arr)

Output

[6, 1, 5, 2, 4, 3]

Javascript

let arr2 = [1, 2, 3, 4, 5, 6];
let n2 = arr2.length;

let max_idx = n2 - 1;
let min_idx = 0;
let max_elem = arr2[n2 - 1] + 1; // Any number greater than max element

for (let i = 0; i < n2; i++) {
    if (i % 2 === 0) {
        // Place max element at even index
        arr2[i] += (arr2[max_idx] % max_elem) * max_elem;
        max_idx--;
    } else {
        // Place min element at odd index
        arr2[i] += (arr2[min_idx] % max_elem) * max_elem;
        min_idx++;
    }
}

// Decode elements
for (let i = 0; i < n2; i++) {
    arr2[i] = Math.floor(arr2[i] / max_elem);
}

console.log(arr2);

Output

[6, 1, 5, 2, 4, 3]

 

Dry Run (Encoding Method)

iOriginalEncoded (temp)Max/Min Pick
011 + 6*7 = 43max → 6
122 + 1*7 = 9min → 1
233 + 5*7 = 38max → 5
344 + 2*7 = 18min → 2
455 + 4*7 = 33max → 4
566 + 3*7 = 27min → 3
  • Divide each by 7 → [6,1,5,2,4,3]

Summary

  • Two approaches:
    1. Extra Array → Simple, O(n) space
    2. In-place Encoding → Optimal, O(1) space
  • Useful in interviews for in-place rearrangement problems
  • Works only on sorted arrays

Next Problems in the series.

Leaders in an Array

Sanjiv
0

You must logged in to post comments.

Related Blogs

Find the S...
Arrays February 02 ,2026

Find the Second Smal...

Find the S...
Arrays January 01 ,2026

Find the Sum of All...

Find the M...
Arrays January 01 ,2026

Find the Maximum Ele...

Find the M...
Arrays January 01 ,2026

Find the Minimum Ele...

Count Even...
Arrays January 01 ,2026

Count Even and Odd N...

Search an...
Arrays January 01 ,2026

Search an Element in...

Copy One A...
Arrays January 01 ,2026

Copy One Array into...

Reverse an...
Arrays January 01 ,2026

Reverse an Array

Print Alte...
Arrays January 01 ,2026

Print Alternate Elem...

Find the L...
Arrays January 01 ,2026

Find the Length of a...

Check if a...
Arrays January 01 ,2026

Check if an Array is...

Find the F...
Arrays January 01 ,2026

Find the First Eleme...

Find the L...
Arrays January 01 ,2026

Find the Last Elemen...

Count the...
Arrays January 01 ,2026

Count the Number of...

Replace Al...
Arrays January 01 ,2026

Replace All Elements...

Sum of Ele...
Arrays January 01 ,2026

Sum of Elements at E...

Sum of Ele...
Arrays January 01 ,2026

Sum of Elements at O...

Find the A...
Arrays January 01 ,2026

Find the Average of...

Count the...
Arrays January 01 ,2026

Count the Number of...

Remove Dup...
Arrays January 01 ,2026

Remove Duplicate Ele...

Move All Z...
Arrays January 01 ,2026

Move All Zeros to th...

Rotate an...
Arrays January 01 ,2026

Rotate an Array by K...

Rotate an...
Arrays January 01 ,2026

Rotate an Array by O...

Check if T...
Arrays January 01 ,2026

Check if Two Arrays...

Merge Two...
Arrays January 01 ,2026

Merge Two Sorted Arr...

Find Missi...
Arrays January 01 ,2026

Find Missing Number...

Find Dupli...
Arrays January 01 ,2026

Find Duplicate Eleme...

Count Freq...
Arrays January 01 ,2026

Count Frequency of E...

Find the M...
Arrays January 01 ,2026

Find the Majority El...

Find All U...
Arrays January 01 ,2026

Find All Unique Elem...

Insert an...
Arrays January 01 ,2026

Insert an Element at...

Delete an...
Arrays January 01 ,2026

Delete an Element fr...

Find the I...
Arrays January 01 ,2026

Find the Index of an...

Find Union...
Arrays January 01 ,2026

Find Union of Two Ar...

Find Inter...
Arrays January 01 ,2026

Find Intersection of...

Sort an Ar...
Arrays January 01 ,2026

Sort an Array of 0s...

Find the L...
Arrays January 01 ,2026

Find the Largest Sum...

Kadane’s A...
Arrays January 01 ,2026

Kadane’s Algorithm (...

Two Sum Pr...
Arrays January 01 ,2026

Two Sum Problem

Subarray w...
Arrays January 01 ,2026

Subarray with Given...

Longest Su...
Arrays January 01 ,2026

Longest Subarray wit...

Leaders in...
Arrays January 01 ,2026

Leaders in an Array

Equilibriu...
Arrays January 01 ,2026

Equilibrium Index of...

Stock Buy...
Arrays January 01 ,2026

Stock Buy and Sell (...

Stock Buy...
Arrays January 01 ,2026

Stock Buy and Sell (...

Sort an Ar...
Arrays January 01 ,2026

Sort an Array of 0s,...

Find the M...
Arrays January 01 ,2026

Find the Majority El...

Find All P...
Arrays January 01 ,2026

Find All Pairs with...

Longest Co...
Arrays January 01 ,2026

Longest Consecutive...

Product of...
Arrays January 01 ,2026

Product of Array Exc...

Maximum Pr...
Arrays January 01 ,2026

Maximum Product Suba...

Find the F...
Arrays January 01 ,2026

Find the First Missi...

Count Inve...
Arrays January 01 ,2026

Count Inversions in...

Rearrange...
Arrays January 01 ,2026

Rearrange Array by S...

Check if A...
Arrays January 01 ,2026

Check if Array Can B...

Trapping R...
Arrays January 01 ,2026

Trapping Rain Water

Find Minim...
Arrays January 01 ,2026

Find Minimum in Rota...

Search in...
Arrays January 01 ,2026

Search in Rotated So...

Median of...
Arrays January 01 ,2026

Median of Two Sorted...

Merge Inte...
Arrays January 01 ,2026

Merge Intervals

Count Reve...
Arrays January 01 ,2026

Count Reverse Pairs

Longest Su...
Arrays January 01 ,2026

Longest Subarray wit...

Largest Re...
Arrays January 01 ,2026

Largest Rectangle in...

Maximum Su...
Arrays January 01 ,2026

Maximum Sum Rectangl...

Subarray S...
Arrays January 01 ,2026

Subarray Sum Equals...

Count Dist...
Arrays January 01 ,2026

Count Distinct Eleme...

Sliding Wi...
Arrays January 01 ,2026

Sliding Window Maxim...

Find K Max...
Arrays January 01 ,2026

Find K Maximum Eleme...

Minimum Nu...
Arrays January 01 ,2026

Minimum Number of Ju...

Chocolate...
Arrays January 01 ,2026

Chocolate Distributi...

Find All T...
Arrays January 01 ,2026

Find All Triplets Wi...

Kth Smalle...
Arrays January 01 ,2026

Kth Smallest Element...

Maximum Le...
Arrays January 01 ,2026

Maximum Length Biton...

Find the S...
Arrays February 02 ,2026

Find the Second Larg...

Get In Touch

Kurki bazar Uttar Pradesh

+91-8808946970

techiefreak87@gmail.com