What is a Permutation Array in DSA?
Last Updated :
03 Nov, 2023
A permutation array, often called a permutation or permuted array, is an arrangement of elements from a source array in a specific order different from their original placement.
The critical characteristic of a permutation array is that it contains all the elements from the source array but in a different order.
Imagine you have an array of elements, such as [1, 2, 3]
. A permutation of this array, like [3, 1, 2]
, represents a different order of the original elements.
How to Create a Permutation Array
Creating a permutation array involves rearranging the elements of a source array in a different order. Various methods and algorithms can be used to generate permutations, including recursive algorithms, iterative approaches, and mathematical techniques like the Lehmer code.
Here, we’ll look at a simple example of how to create a permutation of a one-dimensional array using recursive approach.
Below is the implementation of the above idea:
C++
#include <bits/stdc++.h>
using namespace std;
void swap( int & x, int & y)
{
int temp = x;
x = y;
y = temp;
}
void permutations(vector<vector< int > >& res,
vector< int > nums, int l, int h)
{
if (l == h) {
res.push_back(nums);
return ;
}
for ( int i = l; i <= h; i++) {
swap(nums[l], nums[i]);
permutations(res, nums, l + 1, h);
swap(nums[l], nums[i]);
}
}
vector<vector< int > > permute(vector< int >& nums)
{
vector<vector< int > > res;
int x = nums.size() - 1;
permutations(res, nums, 0, x);
return res;
}
int main()
{
vector< int > nums = { 1, 2, 3 };
vector<vector< int > > res = permute(nums);
for ( auto x : res) {
for ( auto y : x) {
cout << y << " " ;
}
cout << endl;
}
return 0;
}
|
Java
import java.util.ArrayList;
import java.util.Arrays;
public class GFG
{
static void swap( int nums[], int l, int i)
{
int temp = nums[l];
nums[l] = nums[i];
nums[i] = temp;
}
static void permutations(ArrayList< int []> res,
int [] nums, int l, int h)
{
if (l == h) {
res.add(Arrays.copyOf(nums, nums.length));
return ;
}
for ( int i = l; i <= h; i++) {
swap(nums, l, i);
permutations(res, nums, l + 1 , h);
swap(nums, l, i);
}
}
static ArrayList< int []> permute( int [] nums)
{
ArrayList< int []> res = new ArrayList< int []>();
int x = nums.length - 1 ;
permutations(res, nums, 0 , x);
return res;
}
public static void main(String[] args)
{
int [] nums = { 1 , 2 , 3 };
ArrayList< int []> res = permute(nums);
for ( int [] x : res) {
for ( int y : x) {
System.out.print(y + " " );
}
System.out.println();
}
}
}
|
Python3
def permutations(res, nums, l, h) :
if (l = = h) :
res.append(nums);
for i in range ( len (nums)):
print (nums[i], end = ' ' );
print ('')
return ;
for i in range (l, h + 1 ):
temp = nums[l];
nums[l] = nums[i];
nums[i] = temp;
permutations(res, nums, l + 1 , h);
temp = nums[l];
nums[l] = nums[i];
nums[i] = temp;
def permute(nums):
x = len (nums) - 1 ;
res = [];
permutations(res, nums, 0 , x);
return res;
nums = [ 1 , 2 , 3 ];
res = permute(nums);
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void swap( int [] nums, int l, int i)
{
int temp = nums[l];
nums[l] = nums[i];
nums[i] = temp;
}
static void permutations(List< int []> res, int [] nums, int l, int h)
{
if (l == h)
{
res.Add(( int [])nums.Clone());
return ;
}
for ( int i = l; i <= h; i++)
{
swap(nums, l, i);
permutations(res, nums, l + 1, h);
swap(nums, l, i);
}
}
static List< int []> permute( int [] nums)
{
List< int []> res = new List< int []>();
int x = nums.Length - 1;
permutations(res, nums, 0, x);
return res;
}
static void Main( string [] args)
{
int [] nums = { 1, 2, 3 };
List< int []> res = permute(nums);
foreach ( int [] x in res)
{
foreach ( int y in x)
{
Console.Write(y + " " );
}
Console.WriteLine();
}
}
}
|
Javascript
<script>
function permutations(res, nums, l, h)
{
if (l == h)
{
res.push(nums);
for (let i = 0; i < nums.length; i++)
document.write(nums[i] + ' ' );
document.write( '<br>' )
return ;
}
for (let i = l; i <= h; i++)
{
let temp = nums[l];
nums[l] = nums[i];
nums[i] = temp;
permutations(res, nums, l + 1, h);
temp = nums[l];
nums[l] = nums[i];
nums[i] = temp;
}
}
function permute(nums)
{
let x = nums.length - 1;
let res = [];
permutations(res, nums, 0, x);
return res;
}
let nums = [ 1, 2, 3 ];
let res = permute(nums);
</script>
|
Output
1 2 3
1 3 2
2 1 3
2 3 1
3 2 1
3 1 2
Problems Based on Permutation Arrays
Finding the kth permutation of an array |
Read |
Inverting a permutation array |
Read |
Reordering elements of an array based on a permutation |
Read |
Sorting an array using a permutation array |
Read |
Generating all permutations of an array |
Read |
Finding the rank of a permutation within all permutations |
Read |
Testing if two permutations are inverses of each other |
Read |
Shuffling an array randomly using a permutation array |
Read |
Permuting elements in a matrix based on row/column permutations |
Read |
These problems involve various operations and applications related to permutation arrays, ranging from basic tasks like finding permutations to more complex operations such as inverting permutations or sorting using a permutation array.
Uses of Permutation Arrays
Permutation arrays find applications in a wide range of fields, including:
- Cryptography: permutations are used to create secure keys, encrypt data, and ensure the confidentiality and integrity of information.
- Game Development: Permutations are used in game development to randomize game elements, create random mazes, and introduce unpredictability to gameplay.
- Statistics and Sampling: permutation tests are employed to assess the significance of observed data and draw meaningful conclusions.
- Data Compression: Permutations play a role in data compression algorithms, helping reduce data size without significant loss of information.
Conclusion
Permutation arrays are a fundamental concept with a multitude of applications. Whether you’re exploring combinatorial problems, securing data, or adding randomness to your projects, understanding permutations and their uses is essential in various domains of computer science and mathematics. Permutations provide the tools to rearrange and manipulate elements, offering creative solutions to an array of challenges.
Share your thoughts in the comments
Please Login to comment...