Straight to the Point !
Are you landed to this page and rushing for the immediate solution in javascript. here you go !!
- Step 1: Iterate Array A and Store them in Hash Map Value as occurence count.
- Step 2: Iterate Array B.
- Step 2a: Check Array B element is exist in array A.
- Step 2b: Decrement the occurence value, if its exist.
1function areTheyEqual(array_a, array_b) {2 const data = {};3 for (let targetArrayIndex = 0; targetArrayIndex < array_a.length; ++targetArrayIndex) {4 data[array_a[targetArrayIndex]] = data[array_a[targetArrayIndex]] + 1 || 15 }6 // Iterating Array B.7 for (let arrayIndex = 0; arrayIndex < array_b.length; ++arrayIndex) {8 const arrayBValueInDataHashMap = data[array_b[arrayIndex]];9 if (!arrayBValueInDataHashMap) return false;10 if (!!arrayBValueInDataHashMap) {11 if (arrayBValueInDataHashMap < 1) {12 return false;13 }14 data[array_b[arrayIndex]] = data[array_b[arrayIndex]] - 115 }16 }17 return true;18}
On High level note, It worth to read in detail for a better understanding.
This is one of the Facebook interview question and Leet code problem as well - 1460. Make Two Arrays Equal by Reversing Sub-arrays
Table of Contents
Problem Statement
Given two arrays A and B of length N, determine if there is a way to make A equal to B by reversing any subarrays from array B any number of times.
1bool areTheyEqual(int[] arr_a, int[] arr_b)
Constraints
All integers in array are in the range [0, 1,000,000,000].
As per, Leet Code
- target.length == arr.length
- 1 <= target.length <= 1000
- 1 <= target[i] <= 1000
- 1 <= arr[i] <= 1000
Expected
Return true if B can be made equal to A, return false otherwise.
Test Cases
- Input: [1, 2, 3, 4] and [1, 4, 3, 2] and Output: true
- Input: [1, 2, 3, 4] and [1, 4, 3, 3] and Output: false
- (Leet Code) Input: [392,360] and [392,360] and Output: true, because leet code problem check index as well.
Foot Note
- Each element of target should have a corresponding element in arr, and if it doesn't have a corresponding element, return false.
- To solve it easiely you can sort the two arrays and check if they are equal.
- Leet code based expect both array to be sorted as a hint
Solution Intro
Lets see the list of approaches and their complexities.
Approach | Time Complexity | Space Complexity | |
---|---|---|---|
1 | Brute Force (Sort Array B and Iterate Array B then check both Index) | O(n+m) | O(m) |
2 | Sort B and Stringify both Array | O(2n+2m) | O(n+m) |
3 | Use HashMap with Two for...loop | O(2n+m) | O(n+m) |
4 | Time Optimized (2nd Solution) | O(2n+2m) | O(n+m) |
5 | Memory Optimized (3rd Solution) | O(2n+m) | O(n+m) |
Solutions
With no further due, lets take a example of code solutions.
Brute Force
Below solution sort both array then iterate the first array to check each index value is same, method returns false if there is no match with same index in both array.
1function areTheyEqual(array_a, array_b) {2 if (array_a.length !== array_b.length) return false;3 // Sorting array_b in ascending.4 const sortedArray = array_b.sort((a,b) => a - b);5 // Iterate Target Elements.6 for(let arrayAIndex = 0;arrayAIndex<array_a.length;arrayAIndex++) {7 if(array_a[arrayAIndex] !== sortedArray[arrayAIndex]) {8 return false;9 }10 }11 return true;12};
Sort and Stringify both Array
Using JS predefined method sort and join, basically this Sort and Stringify both array elements.
1function(target, arr) {2 return target.sort().join("") === arr.sort().join("");3};
Use HashMap with Two for...loop
Basically, this stores the elements value in hashmap as property name and value as number of occurence and then iterate the second array to check the hashmap is exist with greater than zero and then decrement the hashmap property value when occurence if exist in second array.
1function areTheyEqual(array_a, array_b) {2 const data = {};3 for (let targetArrayIndex = 0; targetArrayIndex < array_a.length; ++targetArrayIndex) {4 data[array_a[targetArrayIndex]] = data[array_a[targetArrayIndex]] + 1 || 15 }6 // Iterating Array B.7 for (let arrayIndex = 0; arrayIndex < array_b.length; ++arrayIndex) {8 const arrayBValueInDataHashMap = data[array_b[arrayIndex]];9 if (!arrayBValueInDataHashMap) return false;10 if (!!arrayBValueInDataHashMap) {11 if (arrayBValueInDataHashMap < 1) {12 return false;13 }14 data[array_b[arrayIndex]] = data[array_b[arrayIndex]] - 115 }16 }17 return true;18}
Time Optimized
Time Optimized is Sort and Stringify both Array which is second solution.
Space Optimized
Space Optimized is Brute Force Code solution.