Lexicographically smallest Permutation of Array by reversing at most one Subarray
Last Updated :
11 Jul, 2022
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++
// C++ code for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the
// Lexicographically smallest
// Permutation by one subarray reversal
void lexsmallest(vector<int>& arr, int n)
{
// Initialize the variables
// To store the first and last
// Position of the subarray
int first = -1, flag = 0, find = -1, last = -1;
// Traverse the array
// And check if arr[i]!=i+1
for (int i = 0; i < n; i++) {
if (arr[i] != i + 1) {
flag = 1;
// Mark the first position
// Of the Subarray to be reversed
first = i;
find = i + 1;
break;
}
}
// If flag == 0, it is the
// Smallest permutation,
// So print the array
if (flag == 0) {
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
}
// Check where the minimum element is present
else {
for (int i = 0; i < n; i++) {
// It is the last position
// Of the subarray to be
// Reversed
if (arr[i] == find) {
last = i;
break;
}
}
// Reverse the subarray
// And print the array
reverse(arr.begin() + first,
arr.begin() + last + 1);
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
}
}
// Driver Code
int main()
{
// Initialize the array arr[]
vector<int> arr = { 1, 3, 4, 2, 5 };
int N = arr.size();
// Function call
lexsmallest(arr, N);
return 0;
}
Java
// Java code for the above approach
import java.util.*;
class GFG{
// Function to find the
// Lexicographically smallest
// Permutation by one subarray reversal
static void lexsmallest(int []arr, int n)
{
// Initialize the variables
// To store the first and last
// Position of the subarray
int first = -1, flag = 0, find = -1, last = -1;
// Traverse the array
// And check if arr[i]!=i+1
for (int i = 0; i < n; i++) {
if (arr[i] != i + 1) {
flag = 1;
// Mark the first position
// Of the Subarray to be reversed
first = i;
find = i + 1;
break;
}
}
// If flag == 0, it is the
// Smallest permutation,
// So print the array
if (flag == 0) {
for (int i = 0; i < n; i++) {
System.out.print(arr[i]+ " ");
}
}
// Check where the minimum element is present
else {
for (int i = 0; i < n; i++) {
// It is the last position
// Of the subarray to be
// Reversed
if (arr[i] == find) {
last = i;
break;
}
}
// Reverse the subarray
// And print the array
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) {
// Temporary variable to store character
int temp;
while (start <= end) {
// Swapping the first and last character
temp = str[start];
str[start] = str[end];
str[end] = temp;
start++;
end--;
}
return str;
}
// Driver Code
public static void main(String[] args)
{
// Initialize the array arr[]
int []arr = { 1, 3, 4, 2, 5 };
int N = arr.length;
// Function call
lexsmallest(arr, N);
}
}
// This code contributed by shikhasingrajput
Python3
# Python code for the above approach
# Function to find the
# Lexicographically smallest
# Permutation by one subarray reversal
def lexsmallest(arr, n):
# Initialize the variables
# To store the first and last
# Position of the subarray
first = -1
flag = 0
find = -1
last = -1
# Traverse the array
# And check if arr[i]!=i+1
for i in range(0, n):
if (arr[i] != i + 1):
flag = 1
# Mark the first position
# Of the Subarray to be reversed
first = i
find = i + 1
break
# If flag == 0, it is the
# Smallest permutation,
# So print the array
if (flag == 0):
for i in range(0, n):
print(arr[i], end=" ")
# Check where the minimum element is present
else:
for i in range(0, n):
# It is the last position
# Of the subarray to be
# Reversed
if (arr[i] == find):
last = i
break
# Reverse the subarray
# And print the array
arr[first: last + 1] = arr[first: last + 1][::-1]
print(*arr)
# Driver Code
# Initialize the array arr[]
arr = [1, 3, 4, 2, 5]
N = len(arr)
# Function call
lexsmallest(arr, N)
# This code is contributed by Samim Hossain Mondal.
C#
// C# code for the above approach
using System;
class GFG{
// Function to find the
// Lexicographically smallest
// Permutation by one subarray reversal
static void lexsmallest(int []arr, int n)
{
// Initialize the variables
// To store the first and last
// Position of the subarray
int first = -1, flag = 0, find = -1, last = -1;
// Traverse the array
// And check if arr[i]!=i+1
for (int i = 0; i < n; i++) {
if (arr[i] != i + 1) {
flag = 1;
// Mark the first position
// Of the Subarray to be reversed
first = i;
find = i + 1;
break;
}
}
// If flag == 0, it is the
// Smallest permutation,
// So print the array
if (flag == 0) {
for (int i = 0; i < n; i++) {
Console.Write(arr[i]+ " ");
}
}
// Check where the minimum element is present
else {
for (int i = 0; i < n; i++) {
// It is the last position
// Of the subarray to be
// Reversed
if (arr[i] == find) {
last = i;
break;
}
}
// Reverse the subarray
// And print the array
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) {
// Temporary variable to store character
int temp;
while (start <= end)
{
// Swapping the first and last character
temp = str[start];
str[start] = str[end];
str[end] = temp;
start++;
end--;
}
return str;
}
// Driver Code
static public void Main (){
// Initialize the array arr[]
int [] arr = { 1, 3, 4, 2, 5 };
int N = arr.Length;
// Function call
lexsmallest(arr, N);
}
}
// This code is contributed by hrithikgarg03188.
JavaScript
<script>
// JavaScript code for the above approach
// Function to find the
// Lexicographically smallest
// Permutation by one subarray reversal
const lexsmallest = (arr, n) => {
// Initialize the variables
// To store the first and last
// Position of the subarray
let first = -1, flag = 0, find = -1, last = -1;
// Traverse the array
// And check if arr[i]!=i+1
for (let i = 0; i < n; i++) {
if (arr[i] != i + 1) {
flag = 1;
// Mark the first position
// Of the Subarray to be reversed
first = i;
find = i + 1;
break;
}
}
// If flag == 0, it is the
// Smallest permutation,
// So print the array
if (flag == 0) {
for (let i = 0; i < n; i++) {
document.write(`${arr[i]} `);
}
}
// Check where the minimum element is present
else {
for (let i = 0; i < n; i++) {
// It is the last position
// Of the subarray to be
// Reversed
if (arr[i] == find) {
last = i;
break;
}
}
// Reverse the subarray
// And print the array
arr.splice(first, last, ...arr.slice(first, last + 1).reverse());
for (let i = 0; i < n; i++) {
document.write(`${arr[i]} `);
}
}
}
// Driver Code
// Initialize the array arr[]
let arr = [1, 3, 4, 2, 5];
let N = arr.length;
// Function call
lexsmallest(arr, N);
// This code is contributed by rakeshsahni
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Related Topic: Subarrays, Subsequences, and Subsets in Array
Similar Reads
DSA Tutorial - Learn Data Structures and Algorithms DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on
7 min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Quick Sort QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s
12 min read
Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge
14 min read
Data Structures Tutorial Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st
2 min read
Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir
8 min read
Breadth First Search or BFS for a Graph Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta
15+ min read
Binary Search Algorithm - Iterative and Recursive Implementation Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc
15 min read
Insertion Sort Algorithm Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T
9 min read
Array Data Structure Guide In this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
4 min read