Make Two Arrays Equal by Reversing Subarrays

Easy Topics Company Tags

You are given equal-length integer arrays target and arr. In one operation, choose a nonempty contiguous part of arr and reverse its order. You may repeat this operation any number of times, including zero. Return true if arr can become exactly equal to target, and false otherwise.

Example 1:

Input: target = [1,2,3]
arr = [3,2,1]

Output: true

Explanation: Reverse all three elements of arr to obtain [1,2,3], which matches target.

Example 2:

Input: target = [1,1,2]
arr = [1,2,2]

Output: false

Explanation: The arrays contain different numbers of 1s and 2s. Reversals only change positions, so they cannot make the arrays equal.

Example 3:

Input: target = [4]
arr = [4]

Output: true

Explanation: The arrays already match. No operation is needed.

Constraints:

  • 1 <= target.length <= 1000
  • target.length == arr.length
  • 1 <= target[i], arr[i] <= 1000


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.