# Arrange the array such that upon performing given operations an increasing order is obtained

Given an array **arr[]** of size **N**, the task is to print the arrangement of the array such that upon performing following operations on this arrangement, an increasing order is obtained as the output:

- Take the first (0
^{th}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 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; ` ` ` `// Function to print the arrangement ` `vector<` `int` `> arrangement(vector<` `int` `> arr) ` `{ ` ` ` `// Sorting the list ` ` ` `sort(arr.begin(),arr.end()); ` ` ` ` ` `//Finding Length of the List ` ` ` `int` `length = arr.size(); ` ` ` ` ` `// Initializing the result array ` ` ` `vector<` `int` `> ans(length,0); ` ` ` ` ` `// Initializing the Queue ` ` ` `deque <` `int` `> Q; ` ` ` `for` `(` `int` `i = 0; i < length; i++) ` ` ` `Q.push_back(i); ` ` ` ` ` `// Adding current array element to the ` ` ` `// result at an index which is at the ` ` ` `// front of the Q and then if still ` ` ` `// elements are left then putting the next ` ` ` `// top element the bottom of the array. ` ` ` `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; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `vector<` `int` `> arr = {1, 2, 3, 4, 5, 6, 7, 8}; ` ` ` ` ` `vector<` `int` `> answer = arrangement(arr); ` ` ` ` ` `for` `(` `int` `i:answer) cout << i << ` `" "` `; ` `} ` ` ` `// This code is contributed by mohit kumar 29 ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the above approach ` ` ` `import` `java.util.*; ` ` ` `public` `class` `GfG { ` ` ` ` ` `// Function to find the array ` ` ` `// arrangement ` ` ` `static` `public` `int` `[] arrayIncreasing(` `int` `[] arr) ` ` ` `{ ` ` ` ` ` `// Sorting the array ` ` ` `Arrays.sort(arr); ` ` ` ` ` `// Finding size of array ` ` ` `int` `length = arr.length; ` ` ` ` ` `// Empty array to store resultant order ` ` ` `int` `answer[] = ` `new` `int` `[length]; ` ` ` ` ` `// Doubly Ended Queue to ` ` ` `// simulate the process ` ` ` `Deque<Integer> dq = ` `new` `LinkedList<>(); ` ` ` ` ` `// Loop to initialize queue with indexes ` ` ` `for` `(` `int` `i = ` `0` `; i < length; i++) { ` ` ` `dq.add(i); ` ` ` `} ` ` ` ` ` `// Adding current array element to the ` ` ` `// result at an index which is at the ` ` ` `// front of the queue and then if still ` ` ` `// elements are left then putting the next ` ` ` `// top element the bottom of the array. ` ` ` `for` `(` `int` `i = ` `0` `; i < length; i++) { ` ` ` ` ` `answer[dq.pollFirst()] = arr[i]; ` ` ` ` ` `if` `(!dq.isEmpty()) ` ` ` `dq.addLast(dq.pollFirst()); ` ` ` `} ` ` ` ` ` `// Returning the resultant order ` ` ` `return` `answer; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `int` `A[] = { ` `1` `, ` `2` `, ` `3` `, ` `4` `, ` `5` `, ` `6` `, ` `7` `, ` `8` `}; ` ` ` ` ` `// Calling the function ` ` ` `int` `ans[] = arrayIncreasing(A); ` ` ` ` ` `// Printing the obtained pattern ` ` ` `for` `(` `int` `i = ` `0` `; i < A.length; i++) ` ` ` `System.out.print(ans[i] + ` `" "` `); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python

`# Python3 Code for the approach ` ` ` `# Importing Queue from Collections Module ` `from` `collections ` `import` `deque ` ` ` `# Function to print the arrangement ` `def` `arrangement(arr): ` ` ` `# Sorting the list ` ` ` `arr.sort() ` ` ` ` ` `# Finding Length of the List ` ` ` `length ` `=` `len` `(arr) ` ` ` ` ` `# Initializing the result array ` ` ` `answer ` `=` `[` `0` `for` `x ` `in` `range` `(` `len` `(arr))] ` ` ` ` ` `# Initializing the Queue ` ` ` `queue ` `=` `deque() ` ` ` `for` `i ` `in` `range` `(length): ` ` ` `queue.append(i) ` ` ` ` ` `# Adding current array element to the ` ` ` `# result at an index which is at the ` ` ` `# front of the queue and then if still ` ` ` `# elements are left then putting the next ` ` ` `# top element the bottom of the array. ` ` ` `for` `i ` `in` `range` `(length): ` ` ` ` ` `answer[queue.popleft()] ` `=` `arr[i] ` ` ` ` ` `if` `len` `(queue) !` `=` `0` `: ` ` ` `queue.append(queue.popleft()) ` ` ` `return` `answer ` ` ` `# Driver code ` `arr ` `=` `[` `1` `, ` `2` `, ` `3` `, ` `4` `, ` `5` `, ` `6` `, ` `7` `, ` `8` `] ` `answer ` `=` `arrangement(arr) ` `# Printing the obtained result ` `print` `(` `*` `answer, sep ` `=` `' '` `) ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the above approach ` `using` `System; ` `using` `System.Collections.Generic; ` ` ` `class` `GfG ` `{ ` ` ` ` ` `// Function to find the array ` ` ` `// arrangement ` ` ` `static` `public` `int` `[] arrayIncreasing(` `int` `[] arr) ` ` ` `{ ` ` ` ` ` `// Sorting the array ` ` ` `Array.Sort(arr); ` ` ` ` ` `// Finding size of array ` ` ` `int` `length = arr.Length; ` ` ` ` ` `// Empty array to store resultant order ` ` ` `int` `[]answer = ` `new` `int` `[length]; ` ` ` ` ` `// Doubly Ended Queue to ` ` ` `// simulate the process ` ` ` `List<` `int` `> dq = ` `new` `List<` `int` `>(); ` ` ` ` ` `// Loop to initialize queue with indexes ` ` ` `for` `(` `int` `i = 0; i < length; i++) ` ` ` `{ ` ` ` `dq.Add(i); ` ` ` `} ` ` ` ` ` `// Adding current array element to the ` ` ` `// result at an index which is at the ` ` ` `// front of the queue and then if still ` ` ` `// elements are left then putting the next ` ` ` `// top element the bottom of the array. ` ` ` `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); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Returning the resultant order ` ` ` `return` `answer; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main(String []args) ` ` ` `{ ` ` ` `int` `[]A = { 1, 2, 3, 4, 5, 6, 7, 8 }; ` ` ` ` ` `// Calling the function ` ` ` `int` `[]ans = arrayIncreasing(A); ` ` ` ` ` `// Printing the obtained pattern ` ` ` `for` `(` `int` `i = 0; i < A.Length; i++) ` ` ` `Console.Write(ans[i] + ` `" "` `); ` ` ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

**Output:**

1 5 2 7 3 6 4 8

**Time Complexity:** O(NlogN)

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

## Recommended Posts:

- Maximum value obtained by performing given operations in an Array
- Minimum increment operations to make the array in increasing order
- Maximum score possible after performing given operations on an Array
- Maximum Possible Product in Array after performing given Operations
- Maximum sum of all elements of array after performing given operations
- Find the modified array after performing k operations of given type
- Find the final sequence of the array after performing given operations
- Check if at least half array is reducible to zero by performing some operations
- Number of array elements derivable from D after performing certain operations
- Minimum element left from the array after performing given operations
- Number of elements from the array which are reachable after performing given operations on D
- Maximum number of unique values in the array after performing given operations
- Maximum count of equal numbers in an array after performing given operations
- Count of operations to make all elements of array a[] equal to its min element by performing a[i] – b[i]
- Find the final number obtained after performing the given operation
- Sort only non-prime numbers of an array in increasing order
- Rearrange all elements of array which are multiples of x in increasing order
- Print array elements in alternatively increasing and decreasing order
- Divide array into increasing and decreasing subsequence without changing the order
- Sort array such that absolute difference of adjacent elements is in increasing order

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.