LeetCode 1460: Make Two Arrays Equal by Reversing Subarrays Solution

Master LeetCode problem 1460 (Make Two Arrays Equal by Reversing Subarrays), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

1460. Make Two Arrays Equal by Reversing Subarrays

Problem Explanation

Explanation

To solve this problem, we need to check if we can transform array arr into array target by reversing any subarrays within arr. We can achieve this by sorting both arrays and then comparing them. If the arrays are equal after sorting, we can return true; otherwise, we return false.

  • Sort both target and arr arrays.
  • Compare the sorted arrays element by element.
  • If all elements are the same, return true; otherwise, return false.

Time complexity: O(n log n) where n is the length of the input arrays. Space complexity: O(n) where n is the length of the input arrays.

Solution Code

import java.util.Arrays;

class Solution {
    public boolean canBeEqual(int[] target, int[] arr) {
        Arrays.sort(target);
        Arrays.sort(arr);
        
        for (int i = 0; i < target.length; i++) {
            if (target[i] != arr[i]) {
                return false;
            }
        }
        
        return true;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1460 (Make Two Arrays Equal by Reversing Subarrays)?

This page provides optimized solutions for LeetCode problem 1460 (Make Two Arrays Equal by Reversing Subarrays) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 1460 (Make Two Arrays Equal by Reversing Subarrays)?

The time complexity for LeetCode 1460 (Make Two Arrays Equal by Reversing Subarrays) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1460 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1460 in Java, C++, or Python.

Back to LeetCode Solutions