Absolute difference between given permutations of N natural numbers
Last Updated :
14 Jan, 2022
Given two permutations arrays arr[] and brr[] of the first N Natural Numbers from 1 to N, the task is to find the absolute difference between the positions of their order in lexicographical order.
Examples:
Input: arr[] = {1, 3, 2}, brr[] = {3, 1, 2}
Output: 3
Explanation:
There are 6 possible permutations of the first N(= 3) natural numbers. They are {{1, 2, 3}, {1, 3, 2}, {2, 1, 3}, {2, 3, 1}, {3, 1, 2}, {3, 2, 1}}. The position of arr[] is 2 and the position of brr[] is 5. Therefore, the answer is |2 – 5| = 3.
Input: arr[] = {1, 3, 2, 4}, brr[] = {1, 3, 2, 4}
Output: 0
Approach: The given problem can be solved by generating the next permutation of the first N natural numbers using the function Next Permutation STL. The idea is to consider arr[] as the base permutation and perform the next_permutation() until arr[] is not equal to brr[]. After this step, the count of steps required to convert arr[] to brr[] is the resultant value of the absolute difference between their positions in lexicographical order.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int permutationDiff(vector< int > arr,
vector< int > brr)
{
if (arr == brr) {
return 0;
}
int count = 0;
while (arr != brr) {
count++;
next_permutation(brr.begin(),
brr.end());
}
return count;
}
int main()
{
vector< int > arr = { 1, 3, 2 };
vector< int > brr = { 3, 1, 2 };
cout << permutationDiff(arr, brr);
return 0;
}
|
Java
import java.util.*;
public class GFG
{
public static void next_permutation( int nums[])
{
int mark = - 1 ;
for ( int i = nums.length - 1 ; i > 0 ; i--) {
if (nums[i] > nums[i - 1 ]) {
mark = i - 1 ;
break ;
}
}
if (mark == - 1 ) {
reverse(nums, 0 , nums.length - 1 );
return ;
}
int idx = nums.length- 1 ;
for ( int i = nums.length- 1 ; i >= mark+ 1 ; i--) {
if (nums[i] > nums[mark]) {
idx = i;
break ;
}
}
swap(nums, mark, idx);
reverse(nums, mark + 1 , nums.length - 1 );
}
public static void swap( int nums[], int i, int j) {
int t = nums[i];
nums[i] = nums[j];
nums[j] = t;
}
public static void reverse( int nums[], int i, int j) {
while (i < j) {
swap(nums, i, j);
i++;
j--;
}
}
public static int permutationDiff( int arr[], int brr[])
{
if (Arrays.equals(arr, brr)) {
return 0 ;
}
int count = 0 , flag = 0 ;
while (!Arrays.equals(arr, brr))
{
count++;
next_permutation(brr);
}
return count;
}
public static void main(String args[])
{
int []arr = { 1 , 3 , 2 };
int []brr = { 3 , 1 , 2 };
System.out.println(permutationDiff(arr, brr));
}
}
|
Python3
def next_permutation(arr):
n = len (arr)
k = n - 2
while k > = 0 :
if arr[k] < arr[k + 1 ]:
break
k - = 1
if k < 0 :
arr = arr[:: - 1 ]
else :
for l in range (n - 1 , k, - 1 ):
if arr[l] > arr[k]:
break
arr[l], arr[k] = arr[k], arr[l]
arr[k + 1 :] = reversed (arr[k + 1 :])
return arr
def permutationDiff(arr,
brr):
if (arr = = brr):
return 0
count = 0
while (arr ! = brr):
count = count + 1
brr = next_permutation(brr)
return count
arr = [ 1 , 3 , 2 ]
brr = [ 3 , 1 , 2 ]
print (permutationDiff(arr, brr))
|
C#
using System;
using System.Linq;
class GFG
{
static void next_permutation( int []nums)
{
int mark = -1;
for ( int i = nums.Length - 1; i > 0; i--) {
if (nums[i] > nums[i - 1]) {
mark = i - 1;
break ;
}
}
if (mark == -1) {
reverse(nums, 0, nums.Length - 1);
return ;
}
int idx = nums.Length-1;
for ( int i = nums.Length-1; i >= mark+1; i--) {
if (nums[i] > nums[mark]) {
idx = i;
break ;
}
}
swap(nums, mark, idx);
reverse(nums, mark + 1, nums.Length - 1);
}
static void swap( int []nums, int i, int j) {
int t = nums[i];
nums[i] = nums[j];
nums[j] = t;
}
public static void reverse( int []nums, int i, int j) {
while (i < j) {
swap(nums, i, j);
i++;
j--;
}
}
public static int permutationDiff( int []arr, int []brr)
{
if (arr.SequenceEqual(brr)) {
return 0;
}
int count = 0, flag = 0;
while (!arr.SequenceEqual(brr))
{
count++;
next_permutation(brr);
}
return count;
}
public static void Main()
{
int []arr = { 1, 3, 2 };
int []brr = { 3, 1, 2 };
Console.Write(permutationDiff(arr, brr));
}
}
|
Javascript
<script>
function next_permutation(nums)
{
let mark = -1;
for (let i = nums.length - 1; i > 0; i--) {
if (nums[i] > nums[i - 1]) {
mark = i - 1;
break ;
}
}
if (mark == -1) {
reverse(nums, 0, nums.length - 1);
return ;
}
let idx = nums.length-1;
for (let i = nums.length-1; i >= mark+1; i--) {
if (nums[i] > nums[mark]) {
idx = i;
break ;
}
}
swap(nums, mark, idx);
reverse(nums, mark + 1, nums.length - 1);
}
function swap(nums, i, j) {
let t = nums[i];
nums[i] = nums[j];
nums[j] = t;
}
function reverse(nums, i, j) {
while (i < j) {
swap(nums, i, j);
i++;
j--;
}
}
function arraysEqual(a, b)
{
if (a === b) return true ;
if (a == null || b == null ) return false ;
if (a.length !== b.length) return false ;
for ( var i = 0; i < a.length; ++i) {
if (a[i] !== b[i]) return false ;
}
return true ;
}
function permutationDiff(arr, brr)
{
if (arraysEqual(arr, brr)) {
return 0;
}
let count = 0, flag = 0;
while (!arraysEqual(arr, brr))
{
count++;
next_permutation(brr);
}
return count;
}
let arr = [ 1, 3, 2 ];
let brr = [ 3, 1, 2 ];
document.write(permutationDiff(arr, brr));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...