Make Two Arrays Equal by Reversing Subarrays

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.

30:00

Make Two Arrays Equal by Reversing Subarrays
easy
Free written solution
Topics
Companies

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
Constraints:
  • target.length==arr.length\text{target.length} == \text{arr.length}

  • 1≤target.length≤10001 \leq \text{target.length} \leq 1000

  • 1≤target[i]≤10001 \leq \text{target}[i] \leq 1000

  • 1≤arr[i]≤10001 \leq \text{arr}[i] \leq 1000

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"target":[1,2,3,4],"arr":[2,4,1,3]}

Reversing two neighbours swaps them, and swaps can reach any order. So only the counts matter: target adds to count, arr takes away.

a
b

reverse →

b
a

neighbour swaps reach every order, so only the counts can block us

target

1
2
3
4

arr

2
4
1
3

count[value]

▲ target has more

▼ arr has more

0

1

0

2

0

3

0

4

Variables
VariableValue
n4
distinct4
DepthFunction Call
Stack empty
0/13