Make Two Arrays Equal by Reversing Subarrays
EasyPrompt
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] <= 1000
Approaches
3 approaches with complexity analysis and trade-offs.
This approach is an optimization of the frequency counting method. Given the problem's constraint that all numbers are between 1 and 1000, we can use a simple array as a direct-access table instead of a hash map. This eliminates the overhead of hashing and object creation, making it the most efficient solution in terms of both time and space.
Algorithm
- Create an integer array
countsof size 1001, initialized to zeros. - Iterate through the input arrays from
i = 0toN-1. - In each iteration, increment the count for the
targetelement (counts[target[i]]++) and decrement the count for thearrelement (counts[arr[i]]--). - After the loop, iterate through the
countsarray. If any value is non-zero, returnfalse. - If all counts are zero, return
true.
Walkthrough
The core idea remains the same: check if the arrays are permutations by comparing element frequencies.
- We create an integer array,
counts, of size 1001 (to accommodate numbers from 1 to 1000) and initialize it with zeros. - We can process both arrays in a single loop. For each index
i, we treat the appearance oftarget[i]as a credit and the appearance ofarr[i]as a debit. We do this by incrementingcounts[target[i]]and decrementingcounts[arr[i]]. - If the two arrays are permutations, then for any number
x, it must appear the same number of times intargetas inarr. Therefore, after the loop, the net count forxin ourcountsarray should be zero. - We perform a final check by iterating through the
countsarray. If any entry is not zero, it signifies a mismatch in frequencies, and we returnfalse. - If all counts are zero, it confirms the arrays are permutations, and we return
true.
class Solution { public boolean canBeEqual(int[] target, int[] arr) { if (target.length != arr.length) { return false; } int[] counts = new int[1001]; for (int i = 0; i < target.length; i++) { counts[target[i]]++; counts[arr[i]]--; } for (int count : counts) { if (count != 0) { return false; } } return true; }}Complexity
Time
O(N + M), where N is the length of the arrays and M is the range of possible values (1001). Since M is a constant, the complexity simplifies to `O(N)`.
Space
O(1). We use an array of a fixed size (1001) based on the problem's constraints on element values. This space usage does not scale with the input array length N.
Trade-offs
Pros
Optimal time complexity of
O(N).Optimal space complexity of
O(1), as thecountsarray size is constant and does not depend on the input size N.Extremely fast in practice due to the use of array indexing instead of hash map operations.
Cons
This approach is only suitable when the range of values in the input is known and small enough to be used as array indices.
Solutions
Solution
class Solution {public boolean canBeEqual(int[] target, int[] arr) { Arrays.sort(target); Arrays.sort(arr); return Arrays.equals(target, arr); }}Video walkthrough
Newsletter
One sharp idea, every week
System design and interview prep — short enough to finish.
No spam. Unsubscribe anytime.
Practice
Same difficulty — related problems to reinforce the pattern.