Arrange the array such that upon performing given operations an increasing order is obtained | C ++ program to find the desired output after performing given operations ; Function to arrange array in such a way that after performing given operation We get increasing sorted array ; Size of given array ; Sort the given array ; Start erasing last element and place it at ith index ; While we reach at starting ; Store last element ; Shift all elements by 1 position in right ; insert last element at ith position ; print desired Array ; Driver Code ; Given Array
#include <bits/stdc++.h> NEW_LINE 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 << " STRNEWLINE " ; } 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 ; }