Make Two Arrays Equal by Reversing Subarrays (LeetCode 1460): Solution and Explanation

Easy
Array
Hash Table
Sorting

You may reverse any subarray of arr as often as you like. Decide whether arr can be turned into target.

Intuition

Reversing a subarray of length 2 swaps two neighbours. With neighbour swaps alone you can sort an array into any order you want, which is exactly what bubble sort does.

So the reversal rule is not really a restriction. The only thing it cannot do is change which values are in the array or how many copies of each there are.

That turns the question into: do target and arr hold the same multiset of values? Count target up, count arr down, and check that every count ends at zero.

Approach: Compare value counts

  1. Make count, an array of 1001 zeros (values are between 1 and 1000).
  2. For every x in target, do count[x] += 1.
  3. For every x in arr, do count[x] -= 1.
  4. If any count is not 0, return false. Otherwise return true.

Step-by-step example

target = [1,2,3,4], arr = [2,4,1,3]
  1. Counting target gives count[1] = count[2] = count[3] = count[4] = 1.
  2. arr subtracts one from 2, then 4, then 1, then 3, so all four counts drop back to 0.
  3. Every count is 0, so arr is a rearrangement of target.
  4. Return true. One possible sequence of reversals is [2,4,1] -> [1,4,2], then [4,2] -> [2,4], then [4,3] -> [3,4].

Code: Python, Java, C++

1class Solution:
2    def canBeEqual(self, target: List[int], arr: List[int]) -> bool:
3        count = [0] * 1001
4        for x in target:
5            count[x] += 1
6        for x in arr:
7            count[x] -= 1
8        for c in count:
9            if c != 0:
10                return False
11        return True
Press play on the visualizer and the running line lights up here.

Time and space complexity

Work as the input growsinput size n →steps
Compare value counts O(n + k)
Hash map counts O(n)
Sort both arrays O(n log n)
Time

O(n + k)

One pass over each array (n each) and one pass over the k = 1001 counts.

Space

O(k)

The count array has a fixed 1001 entries.

Other approaches

Sort both arrays (time O(n log n), space O(n))

Sort copies of target and arr and compare them element by element. Short and works for any value range.

Hash map counts (time O(n), space O(n))

Same counting idea with a hash map, useful when values are not bounded by a small number.

Common mistakes

  • Trying to simulate actual reversals. You never need to find the steps, only decide whether they exist.
  • Checking only that both arrays contain the same set of values. [1,1,2] and [1,2,2] share the set {1,2} but have different counts.
  • Sizing the count array 1000 instead of 1001, which breaks on the value 1000.

Frequently asked questions

Why is any order reachable with reversals?

A reversal of length 2 is a swap of neighbours, and repeated neighbour swaps can move any element to any position, as bubble sort shows.

Do I need the arrays to be the same length?

The constraints guarantee it. If they could differ, equal counts would already imply equal lengths.

Is sorting a fine answer in an interview?

Yes. Mention the counting version as the linear-time improvement because the values are small.