[LeetCode] Make Two Arrays Equal by Reversing Subarrays

1460. 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.

1
2
3
4
5
6
7
8
class Solution {
public:
bool canBeEqual(vector<int>& target, vector<int>& arr) {
sort(begin(target), end(target));
sort(begin(arr), end(arr));
return arr == target;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2024/08/03/PS/LeetCode/make-two-arrays-equal-by-reversing-subarrays/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.