Lexicographically smallest permutation of the array possible by at most one swap
Last Updated :
10 May, 2021
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
// C++ implementation of the above approach
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
// Function to print the
// elements of the array arr[]
void print(int arr[], int N)
{
// Traverse the array arr[]
for (int i = 0; i < N; i++) {
cout << arr[i] << " ";
}
}
// Function to convert given array to
// lexicographically smallest permutation
// possible by swapping at most one pair
void makeLexicographically(int arr[], int N)
{
// Stores the index of the first
// element which is not at its
// correct position
int index = 0;
int temp = 0;
// Checks if any such array
// element exists or not
int check = 0;
int condition = 0;
int element = 0;
// Traverse the given array
for (int i = 0; i < N; ++i) {
// If element is found at i
if (element == arr[i]) {
check = i;
break;
}
// If the first array is
// not in correct position
else if (arr[i] != i + 1 && check == 0) {
// Store the index of
// the first elements
index = i;
check = 1;
condition = -1;
// Store the index of
// the first element
element = i + 1;
}
}
// Swap the pairs
if (condition == -1) {
temp = arr[index];
arr[index] = arr[check];
arr[check] = temp;
}
// Print the array
print(arr, N);
}
// Driver code
int main()
{
// Given array
int arr[] = { 1, 2, 3, 4 };
// Store the size of the array
int N = sizeof(arr) / sizeof(arr[0]);
makeLexicographically(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG {
// Function to print the
// elements of the array arr[]
static void print(int arr[])
{
// Traverse the array arr[]
for (int element : arr) {
System.out.print(element + " ");
}
}
// Function to convert given array to
// lexicographically smallest permutation
// possible by swapping at most one pair
static void makeLexicographically(
int arr[], int length)
{
// Stores the index of the first
// element which is not at its
// correct position
int index = 0;
int temp = 0;
// Checks if any such array
// element exists or not
int check = 0;
int condition = 0;
int element = 0;
// Traverse the given array
for (int i = 0; i < length; ++i) {
// If element is found at i
if (element == arr[i]) {
check = i;
break;
}
// If the first array is
// not in correct position
else if (arr[i] != i + 1
&& check == 0) {
// Store the index of
// the first elements
index = i;
check = 1;
condition = -1;
// Store the index of
// the first element
element = i + 1;
}
}
// Swap the pairs
if (condition == -1) {
temp = arr[index];
arr[index] = arr[check];
arr[check] = temp;
}
// Print the array
print(arr);
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 1, 2, 3, 4 };
int N = arr.length;
makeLexicographically(arr, N);
}
}
Python3
# Python3 program for the above approach
# Function to print the
# elements of the array arr[]
def printt(arr, N) :
# Traverse the array arr[]
for i in range(N):
print(arr[i], end = " ")
# Function to convert given array to
# lexicographically smallest permutation
# possible by swapping at most one pair
def makeLexicographically(arr, N) :
# Stores the index of the first
# element which is not at its
# correct position
index = 0
temp = 0
# Checks if any such array
# element exists or not
check = 0
condition = 0
element = 0
# Traverse the given array
for i in range(N):
# If element is found at i
if (element == arr[i]) :
check = i
break
# If the first array is
# not in correct position
elif (arr[i] != i + 1 and check == 0) :
# Store the index of
# the first elements
index = i
check = 1
condition = -1
# Store the index of
# the first element
element = i + 1
# Swap the pairs
if (condition == -1) :
temp = arr[index]
arr[index] = arr[check]
arr[check] = temp
# Print the array
printt(arr, N)
# Driver code
# Given array
arr = [ 1, 2, 3, 4 ]
# Store the size of the array
N = len(arr)
makeLexicographically(arr, N)
# This code is contributed by code_hunt.
C#
// C# program for the above approach
using System;
class GFG {
// Function to print the
// elements of the array arr[]
static void print(int[] arr)
{
// Traverse the array arr[]
foreach (int element in arr) {
Console.Write(element + " ");
}
}
// Function to convert given array to
// lexicographically smallest permutation
// possible by swapping at most one pair
static void makeLexicographically(
int []arr, int length)
{
// Stores the index of the first
// element which is not at its
// correct position
int index = 0;
int temp = 0;
// Checks if any such array
// element exists or not
int check = 0;
int condition = 0;
int element = 0;
// Traverse the given array
for (int i = 0; i < length; ++i) {
// If element is found at i
if (element == arr[i]) {
check = i;
break;
}
// If the first array is
// not in correct position
else if (arr[i] != i + 1
&& check == 0) {
// Store the index of
// the first elements
index = i;
check = 1;
condition = -1;
// Store the index of
// the first element
element = i + 1;
}
}
// Swap the pairs
if (condition == -1) {
temp = arr[index];
arr[index] = arr[check];
arr[check] = temp;
}
// Print the array
print(arr);
}
// Driver Code
public static void Main(string[] args)
{
int[] arr = { 1, 2, 3, 4 };
int N = arr.Length;
makeLexicographically(arr, N);
}
}
// This code is contributed by ukasp.
JavaScript
<script>
// Javascript implementation of the above approach
// Function to print the
// elements of the array arr[]
function print(arr, N)
{
// Traverse the array arr[]
for (i = 0; i < N; i++) {
document.write(arr[i]+ " ");
}
}
// Function to convert given array to
// lexicographically smallest permutation
// possible by swapping at most one pair
function makeLexicographically(arr, N)
{
// Stores the index of the first
// element which is not at its
// correct position
var index = 0;
var temp = 0;
// Checks if any such array
// element exists or not
var check = 0;
var condition = 0;
var element = 0;
// Traverse the given array
for (i = 0; i < N; ++i) {
// If element is found at i
if (element == arr[i]) {
check = i;
break;
}
// If the first array is
// not in correct position
else if (arr[i] != i + 1 && check == 0) {
// Store the index of
// the first elements
index = i;
check = 1;
condition = -1;
// Store the index of
// the first element
element = i + 1;
}
}
// Swap the pairs
if (condition == -1) {
temp = arr[index];
arr[index] = arr[check];
arr[check] = temp;
}
// Print the array
print(arr, N);
}
// Driver code
// Given array
var arr = [1, 2, 3, 4]
// Store the size of the array
var N = arr.length;
makeLexicographically(arr, N);
// This code is contributed by SURENDRA_GANGWAR.
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
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 5Explanation: The subarray from index 1 to index 3 can be
8 min read
Lexicographically largest permutation possible by a swap that is smaller than a given array Given an array arr[] consisting of N integers, the task is to find the lexicographically largest permutation of the given array possible by exactly one swap, which is smaller than the given array. If it is possible to obtain such a permutation, then print that permutation. Otherwise, print "-1". Exa
7 min read
Lexicographically smallest permutation of [1, N] based on given Binary string Given a binary string S of size (N - 1), the task is to find the lexicographically smallest permutation P of the first N natural numbers such that for every index i, if S[i] equals '0' then P[i + 1] must be greater than P[i] and if S[i] equals '1' then P[i + 1] must be less than P[i]. Examples: Inpu
6 min read
Lexicographically smallest permutation where no element is in original position Given a permutation of first N positive integers, the task is to form the lexicographically smallest permutation such that the new permutation does not have any element which is at the same index as of the old one. If it is impossible to make such permutation then print -1. Examples: Input: N = 5, a
13 min read
Lexicographically smallest permutation with no digits at Original Index Given an integer N. The task is to find the lexicographically smallest permutation of integer of the form: 12345...N such that no digit occurs at the index as in the original number, i.e. if P1P2P3...PN is our permutation then Pi must not be equal to i. Note : N is greater than 1 and less than 10. E
7 min read