Given an array arr containing N positive integers, the task is to check if the given array arr represents a permutation or not.
A sequence of N integers is called a permutation if it contains all integers from 1 to N exactly once.
Examples:
Input: arr[] = {1, 2, 5, 3, 2}
Output: No
Explanation: The given array is not a permutation of numbers from 1 to N, because it contains 2 twice, and 4 is missing for the array to represent a permutation of length 5.
Input: arr[] = {1, 2, 5, 3, 4}
Output: Yes
Explanation:
Given array contains all integers from 1 to 5 exactly once. Hence, it represents a permutation of length 5.
Naive Approach: Clearly, the given array will represent a permutation of length N only, where N is the length of the array. So we have to search for each element from 1 to N in the given array. If all the elements are found then the array represents a permutation else it does not.
Algorithm:
- Initialize a flag variable “isPermutation” to true.
- Initialize a variable “N” to the length of the array.
- Loop through integers from 1 to N:
- Initialize a flag variable “found” to false.
- Loop through the array elements and If the integer “i” is found in the array, set “found” to true and break the loop.
- If “found” is false, set “isPermutation” to false and break the loop.
- If “isPermutation” is true, print “Array represents a permutation”.
- Else, print “Array does not represent a permutation”.
Below is the implementation of the approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isPermutation( int arr[], int n) {
for ( int i=1; i<=n; i++) {
bool found = false ;
for ( int j=0; j<n; j++) {
if (arr[j] == i) {
found = true ;
break ;
}
}
if (!found) {
return false ;
}
}
return true ;
}
int main() {
int arr[] = { 1, 2, 5, 3, 2 };
int n = sizeof (arr)/ sizeof (arr[0]);
if (isPermutation(arr, n)) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
return 0;
}
|
Java
import java.util.*;
public class GFG {
public static boolean isPermutation( int [] arr, int n)
{
for ( int i = 1 ; i <= n; i++) {
boolean found = false ;
for ( int j = 0 ; j < n; j++) {
if (arr[j] == i) {
found = true ;
break ;
}
}
if (!found) {
return false ;
}
}
return true ;
}
public static void main(String[] args)
{
int [] arr = { 1 , 2 , 5 , 3 , 2 };
int n = arr.length;
if (isPermutation(arr, n)) {
System.out.println( "Yes" );
}
else {
System.out.println( "No" );
}
}
}
|
C#
using System;
public class GFG {
public static bool IsPermutation( int [] arr, int n)
{
for ( int i = 1; i <= n; i++) {
bool found = false ;
for ( int j = 0; j < n; j++) {
if (arr[j] == i) {
found = true ;
break ;
}
}
if (!found) {
return false ;
}
}
return true ;
}
public static void Main( string [] args)
{
int [] arr = { 1, 2, 5, 3, 2 };
int n = arr.Length;
if (IsPermutation(arr, n)) {
Console.WriteLine( "Yes" );
}
else {
Console.WriteLine( "No" );
}
}
}
|
Time Complexity: O(N2)
Efficient Approach:
The above method can be optimized using a set data structure.
- Traverse the given array and insert every element in the set data structure.
- Also, find the maximum element in the array. This maximum element will be value N which will represent the size of the set.
- After traversal of the array, check if the size of the set is equal to N.
- If the size of the set is equal to N then the array represents a permutation else it doesn’t.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool permutation( int arr[], int n)
{
set< int > hash;
int maxEle = 0;
for ( int i = 0; i < n; i++) {
hash.insert(arr[i]);
maxEle = max(maxEle, arr[i]);
}
if (maxEle != n)
return false ;
if (hash.size() == n)
return true ;
return false ;
}
int main()
{
int arr[] = { 1, 2, 5, 3, 2 };
int n = sizeof (arr) / sizeof ( int );
if (permutation(arr, n))
cout << "Yes" << endl;
else
cout << "No" << endl;
return 0;
}
|
Java
import java.util.*;
class GFG{
static boolean permutation( int []arr, int n)
{
Set<Integer> hash = new HashSet<Integer>();
int maxEle = 0 ;
for ( int i = 0 ; i < n; i++) {
hash.add(arr[i]);
maxEle = Math.max(maxEle, arr[i]);
}
if (maxEle != n)
return false ;
if (hash.size() == n)
return true ;
return false ;
}
public static void main(String args[])
{
int arr[] = { 1 , 2 , 5 , 3 , 2 };
int n = arr.length;
if (permutation(arr, n))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def permutation(arr, n):
s = set ()
maxEle = 0 ;
for i in range (n):
s.add(arr[i]);
maxEle = max (maxEle, arr[i]);
if (maxEle ! = n):
return False
if ( len (s) = = n):
return True ;
return False ;
if __name__ = = '__main__' :
arr = [ 1 , 2 , 5 , 3 , 2 ]
n = len (arr)
if (permutation(arr, n)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
using System.Collections.Generic;
class GFG{
static bool permutation( int []arr, int n)
{
HashSet< int > hash = new HashSet< int >();
int maxEle = 0;
for ( int i = 0; i < n; i++) {
hash.Add(arr[i]);
maxEle = Math.Max(maxEle, arr[i]);
}
if (maxEle != n)
return false ;
if (hash.Count == n)
return true ;
return false ;
}
public static void Main(String []args)
{
int []arr = { 1, 2, 5, 3, 2 };
int n = arr.Length;
if (permutation(arr, n))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
<script>
function permutation(arr, n)
{
let hash = new Set();
let maxEle = 0;
for (let i = 0; i < n; i++) {
hash.add(arr[i]);
maxEle = Math.max(maxEle, arr[i]);
}
if (maxEle != n)
return false ;
if (hash.length == n)
return true ;
return false ;
}
let arr = [ 1, 2, 5, 3, 2 ];
let n = arr.length;
if (permutation(arr, n))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(N log N), Since every insert operation in the set is an O(log N) operation. There will be N such operations hence O(N log N).
Auxiliary Space: O(N)
Efficient Approach:-
- As we have to check all elements from 1 to N in the array
- So think that if we just sort the array then if the array element will be from 1 to N then the sequence will be like 1,2,3_____,N.
- So we can just sort the array and can check is all the elements are like 1,2,3,____,N or not.
Implementation:-
C++
#include <bits/stdc++.h>
using namespace std;
bool permutation( int arr[], int n)
{
sort(arr,arr+n);
for ( int i=0;i<n;i++)
{
if (arr[i]!=i+1) return false ;
}
return true ;
}
int main()
{
int arr[] = { 1, 2, 5, 3, 2 };
int n = sizeof (arr) / sizeof ( int );
if (permutation(arr, n))
cout << "Yes" << endl;
else
cout << "No" << endl;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static boolean permutation( int arr[], int n)
{
Arrays.sort(arr);
for ( int i = 0 ; i < n; i++)
{
if (arr[i]!=i+ 1 ) return false ;
}
return true ;
}
public static void main(String[] args) {
int arr[] = { 1 , 2 , 5 , 3 , 2 };
int n = arr.length;
if (permutation(arr, n))
System.out.println( "Yes" );
else
System.out.println( "No" );
return ;
}
}
|
Python3
def permutation(arr, n):
arr.sort()
for i in range (n):
if arr[i] ! = i + 1 :
return False
return True
if __name__ = = '__main__' :
arr = [ 1 , 2 , 5 , 3 , 2 ]
n = len (arr)
if permutation(arr, n):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
public class GFG
{
public static bool permutation( int [] arr, int n)
{
Array.Sort(arr);
for ( int i = 0;i < n;i++)
{
if (arr[i] != i + 1)
{
return false ;
}
}
return true ;
}
internal static void Main()
{
int [] arr = {1, 2, 5, 3, 2};
int n = arr.Length;
if (permutation(arr, n))
{
Console.Write( "Yes" );
Console.Write( "\n" );
}
else
{
Console.Write( "No" );
Console.Write( "\n" );
}
}
}
|
Javascript
function permutation(arr, n) {
arr.sort();
for (let i = 0; i < n; i++) {
if (arr[i] !== i + 1) {
return false ;
}
}
return true ;
}
const arr = [1, 2, 5, 3, 2];
const n = arr.length;
if (permutation(arr, n)) {
console.log( "Yes" );
} else {
console.log( "No" );
}
|
Time Complexity:- O(NLogN)
Space Complexity:- O(1)
Another Efficient Approach: create a boolean array that help in if we already visited that element return False
else Traverse the Whole array
Below is the implementation of above approach
C++
#include <cstring>
#include <iostream>
using namespace std;
bool permutation( int arr[], int n)
{
bool x[n];
memset (x, false , sizeof (x));
for ( int i = 0; i < n; i++) {
if (x[arr[i] - 1] == false ) {
x[arr[i] - 1] = true ;
}
else {
return false ;
}
}
for ( int i = 0; i < n; i++) {
if (x[i] == false ) {
return false ;
}
}
return true ;
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5 };
int n = sizeof (arr) / sizeof (arr[0]);
if (permutation(arr, n)) {
cout << "YES" << endl;
}
else {
cout << "NO" << endl;
}
return 0;
}
|
Java
import java.util.Arrays;
public class Main {
public static boolean permutation( int [] arr, int n) {
boolean [] x = new boolean [n];
Arrays.fill(x, false );
for ( int i = 0 ; i < n; i++) {
if (x[arr[i] - 1 ] == false ) {
x[arr[i] - 1 ] = true ;
}
else {
return false ;
}
}
for ( int i = 0 ; i < n; i++) {
if (x[i] == false ) {
return false ;
}
}
return true ;
}
public static void main(String[] args) {
int [] arr = { 1 , 2 , 3 , 4 , 5 };
int n = arr.length;
if (permutation(arr, n)) {
System.out.println( "YES" );
} else {
System.out.println( "NO" );
}
}
}
|
Python3
def permutation(arr, n):
x = [ 0 ] * n
for i in range (n):
if x[arr[i] - 1 ] = = 0 :
x[arr[i] - 1 ] = 1
else :
return False
for i in range (n):
if x[i] = = 0 :
return False
return True
if __name__ = = "__main__" :
arr = [ 1 , 2 , 3 , 4 , 5 ]
n = len (arr)
if (permutation(arr, n)):
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
public class Gfg
{
public static bool permutation( int [] arr, int n)
{
bool [] x = new bool [n];
for ( int i = 0; i < n; i++)
{
x[i] = false ;
}
for ( int i = 0; i < n; i++)
{
if (x[arr[i] - 1] == false )
{
x[arr[i] - 1] = true ;
}
else
{
return false ;
}
}
for ( int i = 0; i < n; i++)
{
if (x[i] == false )
{
return false ;
}
}
return true ;
}
public static void Main()
{
int [] arr = { 1, 2, 3, 4, 5 };
int n = arr.Length;
if (permutation(arr, n))
{
Console.WriteLine( "YES" );
}
else
{
Console.WriteLine( "NO" );
}
}
}
|
Javascript
function permutation(arr, n) {
let x = new Array(n).fill( false );
for (let i = 0; i < n; i++) {
if (x[arr[i] - 1] == false ) {
x[arr[i] - 1] = true ;
} else {
return false ;
}
}
for (let i = 0; i < n; i++) {
if (x[i] == false ) {
return false ;
}
}
return true ;
}
let arr = [1, 2, 3, 4, 5];
let n = arr.length;
if (permutation(arr, n)) {
console.log( "YES" );
} else {
console.log( "NO" );
}
|
Time Complexity: O(N)
Auxiliary Space: O(N)