Given an array, **arr[]** of **N** integers, the task is to rearrange the array elements such that the smallest element is at the **0th** position, second smallest element is at the **(N-1)th** position, the third smallest element at **1st** position, **4th** smallest element is at the **(N-2)th** position, and so on for all integers in **arr[]**.

**Examples:**

Input:arr[] = {10, 23, 12, 17, 9}Output:9 12 23 17 10Explanation:

The smallest element is 9 which is put in the index 0.

Then the second smallest element is 10 which is put in the last position.

The third smallest element is put in the second position from the start.

The fourth smallest in the second position from the last and so on.

Input:arr[] = {1, 3, 3, 4, 5}Output:1 3 5 4 3

**Approach:** We will be using the Two Pointer Technique. The idea is to iterate from the start marked by variable * i* to end marked by variable

*of the array alternatively until they meet in the middle and keep updating the minimum values at these indices. Below are the steps:*

**j**- Set the element at index
**i**as the*minimum value.*Iterate overand compare each value in this range with**[i, j]****arr[i]**, and if the value in the range is less than**arr[i]**then swap the value between**arr[i]**and**current element**. Increment the value of**i**. - Set the element at index
**j**as the*minimum value.*Iterate overand compare each value in this range with**[i, j]****arr[j]**, and if the value in the range is less than**arr[j]**then swap the value between**arr[j]**and**current element**. Decrement the value of**j**. - Place the smallest element in the
**i**position at the first iteration and the next smallest element in the^{th}**j**position. Do this until both^{th}and**i**points to the same position.**j**

Below is the implementation of the above approach:

## Java

`// Java program for the above approach ` `import` `java.io.*; ` `import` `java.util.*; ` ` ` `public` `class` `GFG { ` ` ` ` ` `// Function to perform the rearrangement ` ` ` `static` `void` `rearrange(` `int` `[] a) ` ` ` `{ ` ` ` `// Initialize variables ` ` ` `int` `i = ` `0` `, j = a.length - ` `1` `; ` ` ` `int` `min = ` `0` `, k, x = ` `0` `, temp; ` ` ` ` ` `// Loop until i crosses j ` ` ` `while` `(i < j) { ` ` ` ` ` `// This check is to find the ` ` ` `// minimum values in the ` ` ` `// ascending order ` ` ` ` ` `for` `(k = i; k <= j; k++) { ` ` ` `if` `(a[k] < a[min]) ` ` ` `min = k; ` ` ` `} ` ` ` ` ` `// Condition to alternatively ` ` ` `// iterate variable i and j ` ` ` `if` `(x % ` `2` `== ` `0` `) { ` ` ` ` ` `// Perform swap operation ` ` ` `temp = a[i]; ` ` ` `a[i] = a[min]; ` ` ` `a[min] = temp; ` ` ` ` ` `// Increment i ` ` ` `i++; ` ` ` ` ` `// Assign the value of min ` ` ` `min = i; ` ` ` `} ` ` ` `else` `{ ` ` ` ` ` `// Perform swap ` ` ` `temp = a[j]; ` ` ` `a[j] = a[min]; ` ` ` `a[min] = temp; ` ` ` ` ` `// Decrement i ` ` ` `j--; ` ` ` ` ` `// Assign the value of min ` ` ` `min = j; ` ` ` `} ` ` ` `x++; ` ` ` `} ` ` ` ` ` `// Print the array ` ` ` `for` `(i = ` `0` `; i < a.length; i++) ` ` ` `System.out.print(a[i] + ` `" "` `); ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `// Given Array arr[] ` ` ` `int` `arr[] = { ` `1` `, ` `3` `, ` `3` `, ` `4` `, ` `5` `}; ` ` ` ` ` `// Function Call ` ` ` `rearrange(arr); ` ` ` `} ` `}` |

*chevron_right*

*filter_none*

**Output:**

1 3 5 4 3

**Time Complexity:** *O(N ^{2})*

**Auxiliary Space:**

*O(1)*

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Smallest index in the given array that satisfies the given condition
- Smallest index such that there are no 0 or 1 to its right
- Smallest index in given range of indices which is not equal to X
- Sum of elements till the smallest index such that there are no even numbers to its right
- Queries for number of distinct elements from a given index till last index in an array
- Sort the array in a given index range
- Find the smallest and second smallest elements in an array
- Rearrange array such that even index elements are smaller and odd index elements are greater
- Maximum sum of smallest and second smallest in an array
- Count of contiguous subarrays possible for every index by including the element at that index
- Number of permutations such that sum of elements at odd index and even index are equal
- Check if every index i has an index j such that sum of elements in both directions are equal
- Rearrange an array in order - smallest, largest, 2nd smallest, 2nd largest, ..
- Check if it is possible to reach to the index with value K when start index is given
- Minimum cost to reach end of array array when a maximum jump of K index is allowed
- Equilibrium index of an array
- Sum of the elements from index L to R in an array when arr[i] = i * (-1)^i
- Why array index starts from zero ?
- Find starting index for every occurrence of given array B in array A using Z-Algorithm
- Longest alternating sub-array starting from every index in a Binary Array

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.