Skip to main content

Command Palette

Search for a command to run...

Make Two Arrays Equal by Reversing Subarrays

Published
1 min read
Make Two Arrays Equal by Reversing Subarrays
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

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.

LeetCode Problem - 1460: Link | Click Here

class Solution{
    // Method to check if two arrays can be made equal
    public boolean canBeEqual(int[] target, int[] arr) {
        // Temporary array to store matching elements
        int[] resultArr = new int[arr.length];

        // Loop through each element in the target array
        for (int i=0; i<target.length; i++){
            // Loop through each element in the input array
            for (int j=0; j<arr.length; j++){
                // Check if the current element in target matches any element in arr
                if (target[i]==arr[j]){
                    // Store the matching element in the resultArr
                    resultArr[i] = target[i];
                    // Mark the matched element in arr with a placeholder value (-1)
                    arr[j] = -1;
                    // Break out of the inner loop as the match is found
                    break;
                }
            }

            // Check if the current element in resultArr is not equal to the original element in target
            if (resultArr[i]!=target[i]){
                // If not equal, arrays cannot be made equal, return false
                return false;
            }
        }

        // If all elements are successfully matched and verified, return true
        return true;
    }
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.