Given an array arr[] of size N, the task is to print the arrangement of the array such that upon performing the following operations on this arrangement, increasing order is obtained as the output:
- Take the first (0th index) element, remove it from the array and print it.
- If there are still elements left in the array, move the next top element to the end of the array.
- Repeat the above steps until the array is not empty.
Examples:
Input: arr = {1, 2, 3, 4, 5, 6, 7, 8}
Output: {1, 5, 2, 7, 3, 6, 4, 8}
Explanation:
Let initial array be {1, 5, 2, 7, 3, 6, 4, 8}, where 1 is the top of the array.
1 is printed, and 5 is moved to the end. The array is now {2, 7, 3, 6, 4, 8, 5}.
2 is printed, and 7 is moved to the end. The array is now {3, 6, 4, 8, 5, 7}.
3 is printed, and 6 is moved to the end. The array is now {4, 8, 5, 7, 6}.
4 is printed, and 8 is moved to the end. The array is now {5, 7, 6, 8}.
5 is printed, and 7 is moved to the end. The array is now {6, 8, 7}.
6 is printed, and 8 is moved to the end. The array is now {7, 8}.
7 is printed, and 8 is moved to the end. The array is now {8}.
8 is printed.
The printing order is 1, 2, 3, 4, 5, 6, 7, 8 which is increasing.
Input: arr = {3, 2, 25, 2, 3, 1, 2, 6, 5, 45, 4, 89, 5}
Output: {1, 45, 2, 5, 2, 25, 2, 5, 3, 89, 3, 6, 4}
Approach:
The idea is to simulate the given process. For this a queue data structure is used.
- The given array is sorted and the queue is prepared by adding array indexes.
- Then the given array is traversed and for each element, the index from the front of the queue is popped and add the current array element is added at the popped index in the resultant array.
- If the queue is still not empty, then the next index (in the queue front) is moved to the back of the queue.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
#define mod 1000000007
using namespace std;
vector< int > arrangement(vector< int > arr)
{
sort(arr.begin(), arr.end());
int length = arr.size();
vector< int > ans(length, 0);
deque< int > Q;
for ( int i = 0; i < length; i++)
Q.push_back(i);
for ( int i = 0; i < length; i++)
{
int j = Q.front();
Q.pop_front();
ans[j] = arr[i];
if (Q.size() != 0)
{
j = Q.front();
Q.pop_front();
Q.push_back(j);
}
}
return ans;
}
int main()
{
vector< int > arr = { 1, 2, 3, 4, 5, 6, 7, 8 };
vector< int > answer = arrangement(arr);
for ( int i : answer)
cout << i << " " ;
}
|
Java
import java.util.*;
public class GfG
{
static public int [] arrayIncreasing( int [] arr)
{
Arrays.sort(arr);
int length = arr.length;
int answer[] = new int [length];
Deque<Integer> dq = new LinkedList<>();
for ( int i = 0 ; i < length; i++) {
dq.add(i);
}
for ( int i = 0 ; i < length; i++)
{
answer[dq.pollFirst()] = arr[i];
if (!dq.isEmpty())
dq.addLast(dq.pollFirst());
}
return answer;
}
public static void main(String args[])
{
int A[] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 };
int ans[] = arrayIncreasing(A);
for ( int i = 0 ; i < A.length; i++)
System.out.print(ans[i] + " " );
}
}
|
Python
from collections import deque
def arrangement(arr):
arr.sort()
length = len (arr)
answer = [ 0 for x in range ( len (arr))]
queue = deque()
for i in range (length):
queue.append(i)
for i in range (length):
answer[queue.popleft()] = arr[i]
if len (queue) ! = 0 :
queue.append(queue.popleft())
return answer
arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ]
answer = arrangement(arr)
print ( * answer, sep = ' ' )
|
C#
using System;
using System.Collections.Generic;
class GfG
{
static public int [] arrayIncreasing( int [] arr)
{
Array.Sort(arr);
int length = arr.Length;
int []answer = new int [length];
List< int > dq = new List< int >();
for ( int i = 0; i < length; i++)
{
dq.Add(i);
}
for ( int i = 0; i < length; i++)
{
answer[dq[0]] = arr[i];
dq.RemoveAt(0);
if (dq.Count != 0)
{
dq.Add(dq[0]);
dq.RemoveAt(0);
}
}
return answer;
}
public static void Main(String []args)
{
int []A = { 1, 2, 3, 4, 5, 6, 7, 8 };
int []ans = arrayIncreasing(A);
for ( int i = 0; i < A.Length; i++)
Console.Write(ans[i] + " " );
}
}
|
Javascript
<script>
function arrayIncreasing(arr)
{
arr.sort( function (a, b){ return a - b});
let length = arr.length;
let answer = new Array(length);
let dq = [];
for (let i = 0; i < length; i++)
{
dq.push(i);
}
for (let i = 0; i < length; i++)
{
answer[dq[0]] = arr[i];
dq.shift();
if (dq.length != 0)
{
dq.push(dq[0]);
dq.shift(0);
}
}
return answer;
}
let A = [ 1, 2, 3, 4, 5, 6, 7, 8 ];
let ans = arrayIncreasing(A);
for (let i = 0; i < A.length; i++)
document.write(ans[i] + " " );
</script>
|
Time Complexity: O(NlogN)
Auxiliary Space: O(N) because it is using extra space for deque q
Another Approach:
If you closely observe it then you will find that here it is following a pattern.
Just Think Reverse.
- Sort the input array and try to reach the previous stage from the given steps.
- Iterate from i=N-1 to 1, by decreasing i by 1 in every step.
- Delete the last element from an array and insert it at ith position.
- Repeat the above two-step till reach to i=1
- After reach i=1 , you will get the final resultant array.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void Desired_Array(vector< int >& v)
{
int n = v.size();
sort(v.begin(), v.end());
int i = n - 1;
while (i > 0) {
int p = v[n - 1];
for ( int j = n - 1; j >= i; j--)
{
v[j + 1] = v[j];
}
v[i] = p;
i--;
}
for ( auto x : v)
cout << x << " " ;
cout << "\n" ;
}
int main()
{
vector< int > v = { 1, 2, 3, 4, 5 };
Desired_Array(v);
vector< int > v1 = { 1, 12, 2, 10, 4, 16, 6 };
Desired_Array(v1);
return 0;
}
|
Java
import java.util.Arrays;
class Main{
public static void Desired_Array( int v[])
{
int n = v.length;
Arrays.sort(v);
int i = n - 1 ;
while (i > 0 )
{
int p = v[n - 1 ];
for ( int j = n - 1 ;
j >= i; j--)
{
v[j] = v[j - 1 ];
}
v[i] = p;
i--;
}
for ( int x = 0 ; x < v.length; x++)
{
System.out.print(v[x] + " " );
}
System.out.println();
}
public static void main(String[] args)
{
int v[] = { 1 , 2 , 3 , 4 , 5 };
Desired_Array(v);
int v1[] = { 1 , 12 , 2 , 10 , 4 , 16 , 6 };
Desired_Array(v1);
}
}
|
Python3
def Desired_Array(v):
n = len (v)
v.sort()
i = n - 1
while (i > 0 ) :
p = v[n - 1 ]
for j in range (n - 1 , i - 1 , - 1 ) :
v[j] = v[j - 1 ]
v[i] = p
i - = 1
for x in v :
print (x, end = " " )
print ()
v = [ 1 , 2 , 3 , 4 , 5 ]
Desired_Array(v)
v1 = [ 1 , 12 , 2 , 10 , 4 , 16 , 6 ]
Desired_Array(v1)
|
C#
using System;
class GFG{
public static void Desired_Array( int [] v)
{
int n = v.Length;
Array.Sort(v);
int i = n - 1;
while (i > 0)
{
int p = v[n - 1];
for ( int j = n - 1;
j >= i; j--)
{
v[j] = v[j - 1];
}
v[i] = p;
i--;
}
for ( int x = 0; x < v.Length; x++)
{
Console.Write(v[x] + " " );
}
Console.WriteLine();
}
static public void Main()
{
int [] v = { 1, 2, 3, 4, 5 };
Desired_Array(v);
int [] v1 = { 1, 12, 2, 10, 4, 16, 6 };
Desired_Array(v1);
}
}
|
Javascript
<script>
function Desired_Array(v)
{
let n = v.length;
v.sort( function (a, b){ return a - b});
let i = n - 1;
while (i > 0)
{
let p = v[n - 1];
for (let j = n - 1; j >= i; j--)
{
v[j] = v[j - 1];
}
v[i] = p;
i--;
}
for (let x = 0; x < v.length; x++)
{
document.write(v[x] + " " );
}
document.write( "</br>" );
}
let v = [ 1, 2, 3, 4, 5 ];
Desired_Array(v);
let v1 = [ 1, 12, 2, 10, 4, 16, 6 ];
Desired_Array(v1);
</script>
|
Output1 5 2 4 3
1 12 2 10 4 16 6
Time Complexity: O(n2)
Auxiliary Space: O(1)