Check if two arrays are cyclic permutations
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Cyclic permutations of arrays are a common challenge in computer science, where two arrays are essentially the same if one can be rotated into the other. Understanding how to determine whether two arrays are cyclic permutations of each other is essential for tasks in data analysis, gaming, and other computational fields. This article will delve into the technical explanations and examples of how to check if two arrays are cyclic permutations in an efficient manner.
Definition and Explanation
A cyclic permutation of an array involves rotating the array's elements. Formally, an array of length can be cyclically rotated to the right times, resulting in a new array .
For example, consider an array . A cyclic permutation to the right by one position would result in .
Key Properties
To determine if two arrays $ A $ and $ B $ are cyclic permutations of one another, the following properties must hold:
- Same Length: Arrays must be of the same length.
- Same Elements: Arrays must contain the same set of elements when considered in a circular manner.
While these conditions are necessary, they are not sufficient alone to determine if two arrays are cyclic permutations. It is also important that the elements are in an order that one can be rotated to form the other.
Technical Approach
Naive Method
A straightforward method to check if two arrays $ A $ and $ B $ are cyclic permutations is to generate all cyclic permutations of and compare each with . Though conceptually simple, this approach is inefficient with a time complexity of , and impractical for large arrays.
Optimized Method
To efficiently check for cyclic permutations, we can utilize string concatenation:
- Concatenate with itself: Form a new array . This is equivalent to duplicating .
- Substring Check: Check if is a substring of .
This method provides an efficient solution with a time complexity of , leveraging the ability to use substring searching algorithms, such as the Knuth-Morris-Pratt algorithm.
Example
Given $ A = [1, 2, 3, 4] $ and $ B = [3, 4, 1, 2] $:
- Concatenate with itself:
- Check if is a substring of . Since appears as a substring, the arrays are cyclic permutations.
Summary Table
| Key Points | Description |
| Naive Method | Generate all cyclic permutations and compare. |
| Optimized Method | Duplicate one array and check substring. |
| Time Complexity | Naive: Optimized: |
| Space Complexity | Naive: Optimized: |
Additional Considerations
Special Cases
- Empty Arrays: Two empty arrays are trivially cyclic permutations of each other.
- Single Element Arrays: Arrays with a single element are always cyclic permutations.
- Identical Arrays: An array is always a cyclic permutation of itself.
Algorithms for Substring Search
The substring search component can be optimized using algorithms such as:
- Knuth-Morris-Pratt (KMP) Algorithm: Efficiently finds the starting index of a substring within a string.
- Boyer-Moore Algorithm: Utilized for faster matching in practice by skipping sections of the text.
Applications
- Music and Audio Processing: Cyclic patterns in audio signals.
- Cryptography: Analysis of cyclic properties of sequences.
- Robotics: Path planning where paths can loop around.
Conclusion
Determining if two arrays are cyclic permutations involves understanding both the properties and efficient methods for performing this check. While naive strategies may suffice for small datasets, optimized approaches using string concatenation and efficient substring search algorithms are recommended for practical applications. This exploration of cyclic permutations underscores the intersection of theoretical concepts and practical solutions in computer science.
Related reading
- Check if two line segments are colliding only check if they are intersecting, not where
- Check if two linked lists merge. If so, where?
- Check if two unordered lists are equal
- Check whether a path is valid
- Check if value already exists within list of dictionaries in Python?
- Check RabbitMQ queue size from client
- Check that triangle is right?
- Checking a line segment is within a distance from a point

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.