Check if an Array is a permutation of numbers from 1 to N : Set 2 | C ++ program to decide if an array represents a permutation or not ; Function to check if an array represents a permutation or not ; Counting the frequency ; Check if each frequency is 1 only ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string permutation ( int arr [ ] , int N ) { int hash [ N + 1 ] = { 0 } ; for ( int i = 0 ; i < N ; i ++ ) { hash [ arr [ i ] ] ++ ; } for ( int i = 1 ; i <= N ; i ++ ) { if ( hash [ i ] != 1 ) return " No " ; } return " Yes " ; } int main ( ) { int arr [ ] = { 1 , 1 , 5 , 5 , 3 } ; int n = sizeof ( arr ) / sizeof ( int ) ; cout << permutation ( arr , n ) << endl ; return 0 ; }