Check if an Array is a permutation of numbers from 1 to N : Set 2
Last Updated :
21 May, 2021
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 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:
The given array contains all integers from 1 to 5 exactly once. Hence, it represents a permutation of length 5.
Naive Approach: in O(N2) Time
This approach is mentioned here
Another Approach: in O(N) Time and O(N) Space
This approach is mentioned here.
Efficient Approach: Using HashTable
- Create a HashTable of N size to store the frequency count of each number from 1 to N
- Traverse through the given array and store the frequency of each number in the HashTable.
- Then traverse the HashTable and check if all the numbers from 1 to N have a frequency of 1 or not.
- Print "Yes" if the above condition is True, Else "No".
Below is the implementation of the above approach:
CPP
// C++ program to decide if an array
// represents a permutation or not
#include <bits/stdc++.h>
using namespace std;
// Function to check if an
// array represents a permutation or not
string permutation(int arr[], int N)
{
int hash[N + 1] = { 0 };
// Counting the frequency
for (int i = 0; i < N; i++) {
hash[arr[i]]++;
}
// Check if each frequency is 1 only
for (int i = 1; i <= N; i++) {
if (hash[i] != 1)
return "No";
}
return "Yes";
}
// Driver code
int main()
{
int arr[] = { 1, 1, 5, 5, 3 };
int n = sizeof(arr) / sizeof(int);
cout << permutation(arr, n) << endl;
return 0;
}
Java
// Java program to decide if an array
// represents a permutation or not
class GFG{
// Function to check if an
// array represents a permutation or not
static String permutation(int arr[], int N)
{
int []hash = new int[N + 1];
// Counting the frequency
for (int i = 0; i < N; i++) {
hash[arr[i]]++;
}
// Check if each frequency is 1 only
for (int i = 1; i <= N; i++) {
if (hash[i] != 1)
return "No";
}
return "Yes";
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 1, 1, 5, 5, 3 };
int n = arr.length;
System.out.print(permutation(arr, n) +"\n");
}
}
// This code is contributed by Princi Singh
Python3
# Python3 program to decide if an array
# represents a permutation or not
# Function to check if an
# array represents a permutation or not
def permutation(arr, N) :
hash = [0]*(N + 1);
# Counting the frequency
for i in range(N) :
hash[arr[i]] += 1;
# Check if each frequency is 1 only
for i in range(1, N + 1) :
if (hash[i] != 1) :
return "No";
return "Yes";
# Driver code
if __name__ == "__main__" :
arr = [ 1, 1, 5, 5, 3 ];
n = len(arr);
print(permutation(arr, n));
# This code is contributed by Yash_R
C#
// C# program to decide if an array
// represents a permutation or not
using System;
class GFG{
// Function to check if an
// array represents a permutation or not
static string permutation(int []arr, int N)
{
int []hash = new int[N + 1];
// Counting the frequency
for (int i = 0; i < N; i++) {
hash[arr[i]]++;
}
// Check if each frequency is 1 only
for (int i = 1; i <= N; i++) {
if (hash[i] != 1)
return "No";
}
return "Yes";
}
// Driver code
public static void Main(string[] args)
{
int []arr = { 1, 1, 5, 5, 3 };
int n = arr.Length;
Console.Write(permutation(arr, n) +"\n");
}
}
// This code is contributed by Yash_R
JavaScript
<script>
// JavaScript program to decide if an array
// represents a permutation or not
// Function to check if an
// array represents a permutation or not
function permutation(arr, N)
{
var hash = Array(N+1).fill(0);
// Counting the frequency
for (var i = 0; i < N; i++) {
hash[arr[i]]++;
}
// Check if each frequency is 1 only
for (var i = 1; i <= N; i++) {
if (hash[i] != 1)
return "No";
}
return "Yes";
}
// Driver code
var arr = [1, 1, 5, 5, 3];
var n = arr.length;
document.write( permutation(arr, n));
</script>
Time Complexity: O(N)
Auxiliary Space Complexity: O(N)
Similar Reads
Check if an Array is a permutation of numbers from 1 to N 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
15+ min read
Change the array into a permutation of numbers from 1 to n Given an array A of n elements. We need to change the array into a permutation of numbers from 1 to n using minimum replacements in the array. Examples: Input : A[] = {2, 2, 3, 3} Output : 2 1 3 4 Explanation: To make it a permutation of 1 to 4, 1 and 4 are missing from the array. So replace 2, 3 wi
5 min read
Find all duplicate and missing numbers in given permutation array of 1 to N Given an array arr[] of size N consisting of the first N natural numbers, the task is to find all the repeating and missing numbers over the range [1, N] in the given array. Examples: Input: arr[] = {1, 1, 2, 3, 3, 5}Output: Missing Numbers: [4, 6]Duplicate Numbers: [1, 3]Explanation:As 4 and 6 are
8 min read
Check if two arrays are permutations of each other Given two unsorted arrays of the same size, write a function that returns true if two arrays are permutations of each other, otherwise false. Examples: Input: arr1[] = {2, 1, 3, 5, 4, 3, 2} arr2[] = {3, 2, 2, 4, 5, 3, 1}Output: YesInput: arr1[] = {2, 1, 3, 5,} arr2[] = {3, 2, 2, 4}Output: NoWe stron
15+ min read
Check if an array can be formed by merging 2 non-empty permutations Given an array arr[] of length N, the task is to check if it can be formed by merging two permutations of the same or different lengths. Print YES if such merging is possible. Otherwise, print NO. Permutations of length 3 are {1, 2, 3}, {2, 3, 1}, {1, 3, 2}, {3, 1, 2}, {3, 2, 1}, {2, 1, 3}. Examples
11 min read