1460. Make Two Arrays Equal by Reversing Subarrays #229
-
You are given two integer arrays of equal length Return Example 1:
Example 2:
Example 3:
Constraints:
Hint:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
To solve this problem, we can follow these steps:
Let's implement this solution in PHP: 1460. Make Two Arrays Equal by Reversing Subarrays <?php
function canBeEqual($target, $arr) {
// Sort both arrays
sort($target);
sort($arr);
// Compare the sorted arrays
return $target == $arr;
}
// Test cases
$target1 = [1, 2, 3, 4];
$arr1 = [2, 4, 1, 3];
echo canBeEqual($target1, $arr1) ? 'true' : 'false'; // Output: true
$target2 = [7];
$arr2 = [7];
echo canBeEqual($target2, $arr2) ? 'true' : 'false'; // Output: true
$target3 = [3, 7, 9];
$arr3 = [3, 7, 11];
echo canBeEqual($target3, $arr3) ? 'true' : 'false'; // Output: false
?> Explanation:
Key Points:
This solution leverages the properties of sorting and the comparison of arrays in PHP, making it both simple and efficient. |
Beta Was this translation helpful? Give feedback.
To solve this problem, we can follow these steps:
Let's implement this solution in PHP: 1460. Make Two Arrays Equal by Reversing Subarrays