Lexicographically smallest Permutation of Array by reversing at most one Subarray
Given an array arr[] of size N which is a permutation from 1 to N, the task is to find the lexicographically smallest permutation that can be formed by reversing at most one subarray.
Examples:
Input : arr[] = {1, 3, 4, 2, 5}
Output : 1 2 4 3 5
Explanation: The subarray from index 1 to index 3 can be reversed to get the lexicographically smallest permutation.
Input : arr[] = {4, 3, 1, 2}
Output: 1 3 4 2
Approach: The idea to solve the problem is based on the traversal of the array.
- In the given problem the lexicographically smallest permutation can be obtained by placing the least number at its correct place by one reversal by traversing from left and checking (i+1) is equal to arr[i]
- If it is not equal find the index of that i+1 in the array and reverse the subarray from ith index to the position (i+1).
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
void lexsmallest(vector< int >& arr, int n)
{
int first = -1, flag = 0, find = -1, last = -1;
for ( int i = 0; i < n; i++) {
if (arr[i] != i + 1) {
flag = 1;
first = i;
find = i + 1;
break ;
}
}
if (flag == 0) {
for ( int i = 0; i < n; i++) {
cout << arr[i] << " " ;
}
}
else {
for ( int i = 0; i < n; i++) {
if (arr[i] == find) {
last = i;
break ;
}
}
reverse(arr.begin() + first,
arr.begin() + last + 1);
for ( int i = 0; i < n; i++) {
cout << arr[i] << " " ;
}
}
}
int main()
{
vector< int > arr = { 1, 3, 4, 2, 5 };
int N = arr.size();
lexsmallest(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void lexsmallest( int []arr, int n)
{
int first = - 1 , flag = 0 , find = - 1 , last = - 1 ;
for ( int i = 0 ; i < n; i++) {
if (arr[i] != i + 1 ) {
flag = 1 ;
first = i;
find = i + 1 ;
break ;
}
}
if (flag == 0 ) {
for ( int i = 0 ; i < n; i++) {
System.out.print(arr[i]+ " " );
}
}
else {
for ( int i = 0 ; i < n; i++) {
if (arr[i] == find) {
last = i;
break ;
}
}
arr = reverse(arr,first,last);
for ( int i = 0 ; i < n; i++) {
System.out.print(arr[i]+ " " );
}
}
}
static int [] reverse( int str[], int start, int end) {
int temp;
while (start <= end) {
temp = str[start];
str[start] = str[end];
str[end] = temp;
start++;
end--;
}
return str;
}
public static void main(String[] args)
{
int []arr = { 1 , 3 , 4 , 2 , 5 };
int N = arr.length;
lexsmallest(arr, N);
}
}
|
Python3
def lexsmallest(arr, n):
first = - 1
flag = 0
find = - 1
last = - 1
for i in range ( 0 , n):
if (arr[i] ! = i + 1 ):
flag = 1
first = i
find = i + 1
break
if (flag = = 0 ):
for i in range ( 0 , n):
print (arr[i], end = " " )
else :
for i in range ( 0 , n):
if (arr[i] = = find):
last = i
break
arr[first: last + 1 ] = arr[first: last + 1 ][:: - 1 ]
print ( * arr)
arr = [ 1 , 3 , 4 , 2 , 5 ]
N = len (arr)
lexsmallest(arr, N)
|
C#
using System;
class GFG{
static void lexsmallest( int []arr, int n)
{
int first = -1, flag = 0, find = -1, last = -1;
for ( int i = 0; i < n; i++) {
if (arr[i] != i + 1) {
flag = 1;
first = i;
find = i + 1;
break ;
}
}
if (flag == 0) {
for ( int i = 0; i < n; i++) {
Console.Write(arr[i]+ " " );
}
}
else {
for ( int i = 0; i < n; i++) {
if (arr[i] == find) {
last = i;
break ;
}
}
arr = reverse(arr,first,last);
for ( int i = 0; i < n; i++) {
Console.Write(arr[i]+ " " );
}
}
}
static int [] reverse( int [] str, int start, int end) {
int temp;
while (start <= end)
{
temp = str[start];
str[start] = str[end];
str[end] = temp;
start++;
end--;
}
return str;
}
static public void Main (){
int [] arr = { 1, 3, 4, 2, 5 };
int N = arr.Length;
lexsmallest(arr, N);
}
}
|
Javascript
<script>
const lexsmallest = (arr, n) => {
let first = -1, flag = 0, find = -1, last = -1;
for (let i = 0; i < n; i++) {
if (arr[i] != i + 1) {
flag = 1;
first = i;
find = i + 1;
break ;
}
}
if (flag == 0) {
for (let i = 0; i < n; i++) {
document.write(`${arr[i]} `);
}
}
else {
for (let i = 0; i < n; i++) {
if (arr[i] == find) {
last = i;
break ;
}
}
arr.splice(first, last, ...arr.slice(first, last + 1).reverse());
for (let i = 0; i < n; i++) {
document.write(`${arr[i]} `);
}
}
}
let arr = [1, 3, 4, 2, 5];
let N = arr.length;
lexsmallest(arr, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Related Topic: Subarrays, Subsequences, and Subsets in Array
Last Updated :
11 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...