Difference between lexicographical ranks of two given permutations
Last Updated :
18 Jan, 2022
Given two arrays P[] and Q[] which permutations of first N natural numbers. If P[] and Q[] are the ath and bth lexicographically smallest permutations of [1, N] respectively, the task is to find | a ? b |.
Examples :
Input: P[] = {1, 3, 2}, Q[] = {3, 1, 2}
Output: 3
Explanation: 6 permutations of [1, 3] arranged lexicographically are {{1, 2, 3}, {1, 3, 2}, {2, 1, 3}, {2, 3, 1}, {3, 1, 2}, {3, 2, 1}}. Therefore, rank of P[] is 2 and rank of Q[] is 5. Therefore, difference is |2 – 5| = 3.
Input: P[] = {1, 2}, Q[] = {1, 2}
Output: 0
Explanation: 2 permutations of [1, 2] arranged lexicographically are {{1, 2}, {2, 1}}. Therefore, rank of P[] is 1 and rank of Q[] is 1. Therefore, difference is |2-2| = 0.
Approach: Follow the below steps to solve the problem:
- Use the next_permutation() function to find the ranks of both the permutations.
- Initialize an array temp[] to store the smallest permutation of first N natural numbers. Also, initialize two variables a and b with 0, to store the lexicographical ranks of the two permutations.
- Check if temp[] is equal to P[] or not. If found to be true, break out of the loop. Otherwise, increment a by 1 and check for the next permutation
- Similarly, find the lexicographical rank of the permutation Q[] and store it in b.
- Finally, print the absolute difference between a and b as the answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findDifference(vector< int >& p,
vector< int >& q, int N)
{
vector< int > A(N);
for ( int i = 0; i < N; i++)
A[i] = i + 1;
bool IsCorrect;
int a = 1, b = 1;
do {
IsCorrect = true ;
for ( int i = 0; i < N; i++) {
if (A[i] != p[i]) {
IsCorrect = false ;
break ;
}
}
if (IsCorrect)
break ;
a++;
} while (next_permutation(A.begin(),
A.end()));
for ( int i = 0; i < N; i++)
A[i] = i + 1;
do {
IsCorrect = true ;
for ( int i = 0; i < N; i++) {
if (A[i] != q[i]) {
IsCorrect = false ;
break ;
}
}
if (IsCorrect)
break ;
b++;
} while (next_permutation(A.begin(),
A.end()));
cout << abs (a - b) << endl;
}
int main()
{
vector< int > p = { 1, 3, 2 };
vector< int > q = { 3, 1, 2 };
int n = p.size();
findDifference(p, q, n);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void findDifference( int [] p,
int [] q,
int N)
{
int [] A = new int [N];
for ( int i = 0 ; i < N; i++)
A[i] = i + 1 ;
boolean IsCorrect;
int a = 1 , b = 1 ;
do
{
IsCorrect = true ;
for ( int i = 0 ; i < N; i++)
{
if (A[i] != p[i])
{
IsCorrect = false ;
break ;
}
}
if (IsCorrect)
break ;
a++;
} while (next_permutation(A));
for ( int i = 0 ; i < N; i++)
A[i] = i + 1 ;
do
{
IsCorrect = true ;
for ( int i = 0 ; i < N; i++)
{
if (A[i] != q[i])
{
IsCorrect = false ;
break ;
}
}
if (IsCorrect)
break ;
b++;
} while (next_permutation(A));
System.out.print(Math.abs(a - b) + "\n" );
}
static boolean next_permutation( int [] p)
{
for ( int a = p.length - 2 ; a >= 0 ; --a)
if (p[a] < p[a + 1 ])
for ( int b = p.length - 1 ;; --b)
if (p[b] > p[a])
{
int t = p[a];
p[a] = p[b];
p[b] = t;
for (++a, b = p.length - 1 ;
a < b; ++a, --b)
{
t = p[a];
p[a] = p[b];
p[b] = t;
}
return true ;
}
return false ;
}
public static void main(String[] args)
{
int [] p = { 1 , 3 , 2 };
int [] q = { 3 , 1 , 2 };
int n = p.length;
findDifference(p, q, n);
}
}
|
Python3
def findDifference(p, q, N):
A = [ 0 ] * N
for i in range (N):
A[i] = i + 1
IsCorrect = False
a, b = 1 , 1
while True :
IsCorrect = True
for i in range (N):
if (A[i] ! = p[i]):
IsCorrect = False
break
if (IsCorrect):
break
a + = 1
if ( not next_permutation(A)):
break
for i in range (N):
A[i] = i + 1
while True :
IsCorrect = True
for i in range (N):
if (A[i] ! = q[i]):
IsCorrect = False
break
if (IsCorrect):
break
b + = 1
if ( not next_permutation(A)):
break
print ( abs (a - b))
def next_permutation(p):
for a in range ( len (p) - 2 , - 1 , - 1 ):
if (p[a] < p[a + 1 ]):
b = len (p) - 1
while True :
if (p[b] > p[a]):
t = p[a]
p[a] = p[b]
p[b] = t
a + = 1
b = len (p) - 1
while a < b:
t = p[a]
p[a] = p[b]
p[b] = t
a + = 1
b - = 1
return True
b - = 1
return False
p = [ 1 , 3 , 2 ]
q = [ 3 , 1 , 2 ]
n = len (p)
findDifference(p, q, n)
|
C#
using System;
class GFG{
static void findDifference( int [] p,
int [] q,
int N)
{
int [] A = new int [N];
for ( int i = 0; i < N; i++)
A[i] = i + 1;
bool IsCorrect;
int a = 1, b = 1;
do
{
IsCorrect = true ;
for ( int i = 0; i < N; i++)
{
if (A[i] != p[i])
{
IsCorrect = false ;
break ;
}
}
if (IsCorrect)
break ;
a++;
} while (next_permutation(A));
for ( int i = 0; i < N; i++)
A[i] = i + 1;
do
{
IsCorrect = true ;
for ( int i = 0; i < N; i++)
{
if (A[i] != q[i])
{
IsCorrect = false ;
break ;
}
}
if (IsCorrect)
break ;
b++;
} while (next_permutation(A));
Console.Write(Math.Abs(a - b) + "\n" );
}
static bool next_permutation( int [] p)
{
for ( int a = p.Length - 2; a >= 0; --a)
if (p[a] < p[a + 1])
for ( int b = p.Length - 1;; --b)
if (p[b] > p[a])
{
int t = p[a];
p[a] = p[b];
p[b] = t;
for (++a, b = p.Length - 1;
a < b; ++a, --b)
{
t = p[a];
p[a] = p[b];
p[b] = t;
}
return true ;
}
return false ;
}
public static void Main(String[] args)
{
int [] p = { 1, 3, 2 };
int [] q = { 3, 1, 2 };
int n = p.Length;
findDifference(p, q, n);
}
}
|
Javascript
<script>
function findDifference(p, q , N)
{
var A = Array(N).fill(0);
for ( var i = 0; i < N; i++)
A[i] = i + 1;
var IsCorrect;
var a = 1, b = 1;
do {
IsCorrect = true ;
for (i = 0; i < N; i++) {
if (A[i] != p[i]) {
IsCorrect = false ;
break ;
}
}
if (IsCorrect)
break ;
a++;
} while (next_permutation(A));
for (i = 0; i < N; i++)
A[i] = i + 1;
do {
IsCorrect = true ;
for (i = 0; i < N; i++) {
if (A[i] != q[i]) {
IsCorrect = false ;
break ;
}
}
if (IsCorrect)
break ;
b++;
} while (next_permutation(A));
document.write(Math.abs(a - b) + "\n" );
}
function next_permutation(p) {
for (a = p.length - 2; a >= 0; --a)
if (p[a] < p[a + 1])
for (b = p.length - 1;; --b)
if (p[b] > p[a]) {
var t = p[a];
p[a] = p[b];
p[b] = t;
for (++a, b = p.length - 1; a < b; ++a, --b) {
t = p[a];
p[a] = p[b];
p[b] = t;
}
return true ;
}
return false ;
}
var p = [ 1, 3, 2 ];
var q = [ 3, 1, 2 ];
var n = p.length;
findDifference(p, q, n);
</script>
|
Time Complexity: O(N * max(a, b)) where N is the given size of the permutations and a and b are the lexicographical ranks of the permutations.
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...