Minimize Steps required to obtain Sorted Order of an Array
Last Updated :
13 Dec, 2021
Given an array arr[] consisting of a permutation of integers [1, N], derived by rearranging the sorted order [1, N], the task is to find the minimum number of steps after which the sorted order [1, N] is repeated, by repeating the same process by which arr[] is obtained from the sorted sequence at each step.
Examples:
Input: arr[ ] = {3, 6, 5, 4, 1, 2}
Output: 6
Explanation:
Increasing Permutation: {1, 2, 3, 4, 5, 6}
Step 1 : arr[] = {3, 6, 5, 4, 1, 2} (Given array)
Step 2 : arr[] = {5, 2, 1, 4, 3, 6}
Step 3 : arr[] = {1, 6, 3, 4, 5, 2}
Step 4 : arr[] = {3, 2, 5, 4, 1, 6}
Step 5 : arr[] = {5, 6, 1, 4, 3, 2}
Step 6 : arr[] = {1, 2, 3, 4, 5, 6} (Increasing Permutation)
Therefore, the total number of steps required are 6.
Input: arr[ ] = [5, 1, 4, 3, 2]
Output: 6
Approach:
This problem can be solved simply by using the concept of Direct Addressing. Follow the steps given below to solve the problem:
- Initialize an array dat[] for direct addressing.
- Iterate over [1, N] and calculate the difference of the current index of every element from its index in the sorted sequence.
- Calculate the LCM of the array dat[].
- Now, print the obtained LCM as the minimum steps required to obtain the sorted order.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
int gcd( int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
int findlcm( int arr[], int n)
{
int ans = 1;
for ( int i = 1; i <= n; i++)
ans = (((arr[i] * ans))
/ (gcd(arr[i], ans)));
return ans;
}
void minimumSteps( int arr[], int n)
{
int i, dat[n + 1];
for (i = 1; i <= n; i++)
dat[arr[i - 1]] = i;
int b[n + 1], j = 0, c;
for (i = 1; i <= n; i++) {
c = 1;
j = dat[i];
while (j != i) {
c++;
j = dat[j];
}
b[i] = c;
}
cout << findlcm(b, n);
}
int main()
{
int arr[] = { 5, 1, 4, 3, 2, 7, 6 };
int N = sizeof (arr) / sizeof (arr[0]);
minimumSteps(arr, N);
return 0;
}
|
Java
class GFG{
static int gcd( int a, int b)
{
if (b == 0 )
return a;
return gcd(b, a % b);
}
static int findlcm( int arr[], int n)
{
int ans = 1 ;
for ( int i = 1 ; i <= n; i++)
ans = (((arr[i] * ans)) /
(gcd(arr[i], ans)));
return ans;
}
static void minimumSteps( int arr[], int n)
{
int i;
int dat[] = new int [n + 1 ];
for (i = 1 ; i <= n; i++)
dat[arr[i - 1 ]] = i;
int b[] = new int [n + 1 ];
int j = 0 , c;
for (i = 1 ; i <= n; i++)
{
c = 1 ;
j = dat[i];
while (j != i)
{
c++;
j = dat[j];
}
b[i] = c;
}
System.out.println(findlcm(b, n));
}
public static void main(String[] args)
{
int arr[] = { 5 , 1 , 4 , 3 , 2 , 7 , 6 };
int N = arr.length;
minimumSteps(arr, N);
}
}
|
Python3
def gcd(a, b):
if (b = = 0 ):
return a
return gcd(b, a % b)
def findlcm(arr, n):
ans = 1
for i in range ( 1 , n + 1 ):
ans = ((arr[i] * ans) / /
(gcd(arr[i], ans)))
return ans
def minimumSteps(arr, n):
dat = [ 0 ] * (n + 1 )
for i in range ( 1 , n + 1 ):
dat[arr[i - 1 ]] = i
b = [ 0 ] * (n + 1 )
j = 0
for i in range ( 1 , n + 1 ):
c = 1
j = dat[i]
while (j ! = i):
c + = 1
j = dat[j]
b[i] = c
print (findlcm(b, n))
arr = [ 5 , 1 , 4 , 3 , 2 , 7 , 6 ]
N = len (arr)
minimumSteps(arr, N)
|
C#
using System;
class GFG{
static int gcd( int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static int findlcm( int []arr, int n)
{
int ans = 1;
for ( int i = 1; i <= n; i++)
ans = (((arr[i] * ans)) /
(gcd(arr[i], ans)));
return ans;
}
static void minimumSteps( int []arr, int n)
{
int i;
int []dat = new int [n + 1];
for (i = 1; i <= n; i++)
dat[arr[i - 1]] = i;
int []b = new int [n + 1];
int j = 0, c;
for (i = 1; i <= n; i++)
{
c = 1;
j = dat[i];
while (j != i)
{
c++;
j = dat[j];
}
b[i] = c;
}
Console.WriteLine(findlcm(b, n));
}
public static void Main(String[] args)
{
int []arr = { 5, 1, 4, 3, 2, 7, 6 };
int N = arr.Length;
minimumSteps(arr, N);
}
}
|
Javascript
<script>
function gcd(a, b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
function findlcm(arr, n)
{
let ans = 1;
for (let i = 1; i <= n; i++)
ans = (((arr[i] * ans)) /
(gcd(arr[i], ans)));
return ans;
}
function minimumSteps(arr, n)
{
let i;
let dat = Array.from({length: n+1}, (_, i) => 0);
for (i = 1; i <= n; i++)
dat[arr[i - 1]] = i;
let b = Array.from({length: n+1}, (_, i) => 0);
let j = 0, c;
for (i = 1; i <= n; i++)
{
c = 1;
j = dat[i];
while (j != i)
{
c++;
j = dat[j];
}
b[i] = c;
}
document.write(findlcm(b, n));
}
let arr = [ 5, 1, 4, 3, 2, 7, 6 ];
let N = arr.length;
minimumSteps(arr, N);
</script>
|
Time Complexity: O(NlogN)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...