Lexicographically smallest permutation of the array possible by at most one swap
Given an array arr[] representing a permutation of first N natural numbers, the task is to find the lexicographically smallest permutation of the given array arr[] possible by swapping at most one pair of array elements. If it is not possible to make the array lexicographically smaller, then print “-1”.
Examples:
Input: arr[] = {3, 2, 1, 4}
Output: 1 2 3 4
Explanation: Swapping elements at index 2 and 0, the modified array is {1, 2, 3, 4}, which is lexicographically the smallest permutation of the given array arr[].
Input: arr[] = {1, 2, 3, 4}
Output: -1
Approach: The idea is to find the first array element which is not at its correct position i.e., arr[i] is not the same as the index (i + 1), and swap it with the element at its correct position. Follow the steps below to solve this problem:
- Traverse the array arr[] and find the index i such that arr[i] is not equal to (i + 1), say idx, and store (i + 1) in a variable, say ele.
- Now, find the index of ele, say newIdx.
- After completing the above steps, there exists two indices idx and newIdx. The lexicographically smallest permutation of the array can be formed by swapping elements at indices idx and newIdx. Now, print the array arr[]. Otherwise, print “-1”.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
void print( int arr[], int N)
{
for ( int i = 0; i < N; i++) {
cout << arr[i] << " " ;
}
}
void makeLexicographically( int arr[], int N)
{
int index = 0;
int temp = 0;
int check = 0;
int condition = 0;
int element = 0;
for ( int i = 0; i < N; ++i) {
if (element == arr[i]) {
check = i;
break ;
}
else if (arr[i] != i + 1 && check == 0) {
index = i;
check = 1;
condition = -1;
element = i + 1;
}
}
if (condition == -1) {
temp = arr[index];
arr[index] = arr[check];
arr[check] = temp;
}
print(arr, N);
}
int main()
{
int arr[] = { 1, 2, 3, 4 };
int N = sizeof (arr) / sizeof (arr[0]);
makeLexicographically(arr, N);
return 0;
}
|
Java
import java.io.*;
class GFG {
static void print( int arr[])
{
for ( int element : arr) {
System.out.print(element + " " );
}
}
static void makeLexicographically(
int arr[], int length)
{
int index = 0 ;
int temp = 0 ;
int check = 0 ;
int condition = 0 ;
int element = 0 ;
for ( int i = 0 ; i < length; ++i) {
if (element == arr[i]) {
check = i;
break ;
}
else if (arr[i] != i + 1
&& check == 0 ) {
index = i;
check = 1 ;
condition = - 1 ;
element = i + 1 ;
}
}
if (condition == - 1 ) {
temp = arr[index];
arr[index] = arr[check];
arr[check] = temp;
}
print(arr);
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 3 , 4 };
int N = arr.length;
makeLexicographically(arr, N);
}
}
|
Python3
def printt(arr, N) :
for i in range (N):
print (arr[i], end = " " )
def makeLexicographically(arr, N) :
index = 0
temp = 0
check = 0
condition = 0
element = 0
for i in range (N):
if (element = = arr[i]) :
check = i
break
elif (arr[i] ! = i + 1 and check = = 0 ) :
index = i
check = 1
condition = - 1
element = i + 1
if (condition = = - 1 ) :
temp = arr[index]
arr[index] = arr[check]
arr[check] = temp
printt(arr, N)
arr = [ 1 , 2 , 3 , 4 ]
N = len (arr)
makeLexicographically(arr, N)
|
C#
using System;
class GFG {
static void print( int [] arr)
{
foreach ( int element in arr) {
Console.Write(element + " " );
}
}
static void makeLexicographically(
int []arr, int length)
{
int index = 0;
int temp = 0;
int check = 0;
int condition = 0;
int element = 0;
for ( int i = 0; i < length; ++i) {
if (element == arr[i]) {
check = i;
break ;
}
else if (arr[i] != i + 1
&& check == 0) {
index = i;
check = 1;
condition = -1;
element = i + 1;
}
}
if (condition == -1) {
temp = arr[index];
arr[index] = arr[check];
arr[check] = temp;
}
print(arr);
}
public static void Main( string [] args)
{
int [] arr = { 1, 2, 3, 4 };
int N = arr.Length;
makeLexicographically(arr, N);
}
}
|
Javascript
<script>
function print(arr, N)
{
for (i = 0; i < N; i++) {
document.write(arr[i]+ " " );
}
}
function makeLexicographically(arr, N)
{
var index = 0;
var temp = 0;
var check = 0;
var condition = 0;
var element = 0;
for (i = 0; i < N; ++i) {
if (element == arr[i]) {
check = i;
break ;
}
else if (arr[i] != i + 1 && check == 0) {
index = i;
check = 1;
condition = -1;
element = i + 1;
}
}
if (condition == -1) {
temp = arr[index];
arr[index] = arr[check];
arr[check] = temp;
}
print(arr, N);
}
var arr = [1, 2, 3, 4]
var N = arr.length;
makeLexicographically(arr, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
10 May, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...