Reverse to Make Equal

Feb 7, 202010 mins read

Straight to the Point !

Are you landed to this page and rushing for the immediate solution in javascript. here you go !!

  1. Step 1: Iterate Array A and Store them in Hash Map Value as occurence count.
  2. Step 2: Iterate Array B.
  3. Step 2a: Check Array B element is exist in array A.
  4. 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 || 1
5 }
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]] - 1
15 }
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

  1. target.length == arr.length
  2. 1 <= target.length <= 1000
  3. 1 <= target[i] <= 1000
  4. 1 <= arr[i] <= 1000

Expected

Return true if B can be made equal to A, return false otherwise.

Test Cases

  1. Input: [1, 2, 3, 4] and [1, 4, 3, 2] and Output: true
  2. Input: [1, 2, 3, 4] and [1, 4, 3, 3] and Output: false
  3. (Leet Code) Input: [392,360] and [392,360] and Output: true, because leet code problem check index as well.

Foot Note

  1. Each element of target should have a corresponding element in arr, and if it doesn't have a corresponding element, return false.
  2. To solve it easiely you can sort the two arrays and check if they are equal.
  3. Leet code based expect both array to be sorted as a hint

Solution Intro

Lets see the list of approaches and their complexities.


ApproachTime ComplexitySpace Complexity
1Brute Force (Sort Array B and Iterate Array B then check both Index)O(n+m)O(m)
2Sort B and Stringify both ArrayO(2n+2m)O(n+m)
3Use HashMap with Two for...loopO(2n+m)O(n+m)
4Time Optimized (2nd Solution)O(2n+2m)O(n+m)
5Memory 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 || 1
5 }
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]] - 1
15 }
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.