Watch 10 video solutions for Make Two Arrays Equal by Reversing Subarrays, a easy level problem involving Array, Hash Table, Sorting. This walkthrough by NeetCodeIO has 9,430 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given two integer arrays of equal length target and arr. In one step, you can select any non-empty subarray of arr and reverse it. You are allowed to make any number of steps.
Return true if you can make arr equal to target or false otherwise.
Example 1:
Input: target = [1,2,3,4], arr = [2,4,1,3] Output: true Explanation: You can follow the next steps to convert arr to target: 1- Reverse subarray [2,4,1], arr becomes [1,4,2,3] 2- Reverse subarray [4,2], arr becomes [1,2,4,3] 3- Reverse subarray [4,3], arr becomes [1,2,3,4] There are multiple ways to convert arr to target, this is not the only way to do so.
Example 2:
Input: target = [7], arr = [7] Output: true Explanation: arr is equal to target without any reverses.
Example 3:
Input: target = [3,7,9], arr = [3,7,11] Output: false Explanation: arr does not have value 9 and it can never be converted to target.
Constraints:
target.length == arr.length1 <= target.length <= 10001 <= target[i] <= 10001 <= arr[i] <= 1000Problem Overview: You are given two integer arrays target and arr. You can reverse any subarray of arr any number of times. The goal is to determine whether these operations can transform arr into target. The key observation: reversing subarrays changes order but never changes the elements themselves.
Approach 1: Sort and Compare (Time: O(n log n), Space: O(1) or O(n))
If reversing subarrays can rearrange elements arbitrarily, the only requirement for equality is that both arrays contain the same multiset of numbers. Sorting both arrays exposes this directly. Sort target and arr, then iterate once to check whether every element matches at the same index. If they match completely, you can reorder arr through subarray reversals to reach target. Sorting dominates the runtime with O(n log n) complexity, while the comparison step is O(n). Many standard library sorting implementations use O(log n) auxiliary stack space or O(n) depending on the language.
This approach is straightforward and reliable. It works well when implementation speed matters and constraints are small to medium. Since the logic is simple—sort then compare—it is often the first solution developers write during interviews.
Approach 2: Use Hashing to Count Frequencies (Time: O(n), Space: O(n))
A more optimal solution avoids sorting by counting element frequencies. Traverse target and store counts in a hash map (or frequency array if values are bounded). Then iterate through arr and decrement the corresponding count for each element. If any element appears more times than expected or is missing from the map, the arrays cannot be made equal.
This works because reversing subarrays only changes ordering, not frequency. If both arrays contain the same elements with identical counts, any permutation is achievable through a sequence of reversals. Hash table lookups and updates run in constant time on average, so the full traversal remains O(n).
This method uses concepts from hash tables and arrays. Compared with sorting, it improves runtime but uses additional memory to store counts.
Recommended for interviews: The hashing approach is typically preferred because it achieves O(n) time while clearly demonstrating understanding of element frequency comparison. The sorting solution still shows correct reasoning and knowledge of sorting, but interviewers often expect you to recognize that order is irrelevant and only counts matter.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Sort and Compare | O(n log n) | O(1) to O(n) | Simple implementation when sorting cost is acceptable |
| Hash Frequency Counting | O(n) | O(n) | Optimal solution when linear time comparison is preferred |