Make Two Arrays Equal by Reversing Subarrays - Problem
Can you transform one array into another using only reversal operations?

You are given two integer arrays of equal length: target and arr. Your goal is to determine whether you can make arr identical to target by performing a series of subarray reversals.

In each operation, you can:
• Select any non-empty contiguous subarray of arr
Reverse all elements in that subarray
• Repeat this process any number of times

For example, if arr = [1,2,3,4], you could reverse the subarray [2,3] to get [1,3,2,4].

Return: true if you can make arr equal to target, or false otherwise.

Input & Output

example_1.py — Basic case
$ Input: target = [1,2,3,4], arr = [2,4,1,3]
Output: true
💡 Note: We can transform arr to target: [2,4,1,3] → reverse [4,1] → [2,1,4,3] → reverse [2,1] → [1,2,4,3] → reverse [4,3] → [1,2,3,4]
example_2.py — Impossible case
$ Input: target = [7], arr = [4]
Output: false
💡 Note: arr contains different elements than target, so no amount of reversals can make them equal
example_3.py — Duplicate elements
$ Input: target = [3,7,9], arr = [3,7,11]
Output: false
💡 Note: Arrays have different elements (9 vs 11), so transformation is impossible

Constraints

  • target.length == arr.length
  • 1 ≤ target.length ≤ 1000
  • 1 ≤ target[i] ≤ 1000
  • 1 ≤ arr[i] ≤ 1000

Visualization

Tap to expand
Array Transformation VisualizationSource: [2,4,1,3]2413Target: [1,2,3,4]1234Frequency AnalysisSource Count:1: ✓, 2: ✓, 3: ✓, 4: ✓Target Count:1: ✓, 2: ✓, 3: ✓, 4: ✓Match: Perfect! ✓Transformation PossibleReturn: true💡 Key Insight: Any permutation can be achieved through subarray reversals⚡ Optimal: O(n) frequency counting beats O(n log n) sorting
Understanding the Visualization
1
Initial Setup
Start with arr=[2,4,1,3] and target=[1,2,3,4]
2
Count Elements
Count frequency: both have one each of 1,2,3,4
3
Verify Match
Since frequencies match, transformation is possible
Key Takeaway
🎯 Key Insight: Since subarray reversals can achieve any permutation, we only need to verify that both arrays contain identical elements with matching frequencies!
Asked in
Google 25 Amazon 18 Meta 12 Microsoft 8
28.4K Views
Medium Frequency
~8 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen