How to tell if an array is a permutation in On?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
A permutation of the first n
natural numbers is an arrangement or sequence where each number from 1
to n
appears exactly once. To determine if an array is a permutation, the goal is to verify that it contains all numbers from 1
to n
without duplicates. This can be done efficiently in O(n)
time complexity. Here’s how:
Understanding Permutations
A permutation of an array [1, 2, 3, ..., n]
means that the array is a rearrangement of these numbers. Thus, each number should appear exactly once.
Key Properties of a Permutation:
- Length Check: The length of the array should be
n. - Element Range: Every element should be between
1andn. - Uniqueness: Each element should be unique.
Approach: Efficiently Checking Permutation in O(n)
To check if an array is a permutation, follow these steps:
- Length Verification:
- Ensure the array has
nelements.
- Counting Occurrences with a
HashSet:- Utilize a hash set to record numbers as they appear.
- Loop through each element in the array:
- If the element is outside the range
[1, n], it is not a permutation. - If the element is already in the set, it means there are duplicates, so it is not a permutation.
- Otherwise, add the element to the set.
- Final Check:
- If the hash set contains exactly
nelements, the array is a permutation.
Algorithm Implementation
Here’s the pseudocode for a better understanding:
- Step 1: The length is
4, which matches the number of distinct natural numbers[1, 2, 3, 4]. - Step 2: Insert elements into a set:
\{4, 3, 2, 1\}. - Step 3: The set has
4elements, confirming it's a permutation. - Empty Array: An array with zero elements is not a permutation.
- Array with Duplicate and Out of Range Elements: For example, an array
[1, 2, 3, 3]or[0, 1, 2, 3]fails the permutation test. - Space Optimization: Instead of using a hash set, a boolean array of size
ncould also be used to track occurrences, reducing additional space from handling complex data types. - Real-world Applications: Permutation checks are useful in cryptography, competitive programming, and whenever order-sensitive operations occur in algorithms.
Related reading
- How to tell if greedy algorithm suffices for finding minimum coin change?
- How to test a hash function?
- How to test if one string is a subsequence of another?
- How to think in recursive way?
- How to test if a dictionary contains a specific key?
- How to trace the path in a Breadth-First Search?
- How to tell if tensorflow is using gpu acceleration from inside python shell?
- How to throttle writes request to cassandra when working with executeAsync?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.