Categories
General

Reverse to Make Equal Facebook

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.Signature bool areTheyEqual(int[] arr_a, int[] arr_b)Input All integers in array are in the range [0, 1,000,000,000].Output Return true if B can be made equal to A, return false otherwise.Example A = [1, 2, 3, 4] B = [1, 4, 3, 2] output = trueAfter reversing the subarray of B from indices 1 to 3, array B will equal array A.

function areTheyEqual($s, $t) {
  
  // Write your code here
  $arr_a = $s;
  $arr_b = $t;
  
  $isEqual = false;
            $hashMapArrayA = array();
            $hashMapArrayB = array();

            #sanity check 
            $sizeArr_a = count($arr_a); #4
            $sizeArr_b = count($arr_b); #4
            if ($sizeArr_a !== $sizeArr_b)
            {
                return false;

            }
            
            #building hashMapA
            #hashMapA[1] = 1
            #hashMapA[2] = 1
            #hashMapA[3] = 1
            #hashMapA[4] = 1
            foreach ($arr_a as $key => $value) {
                if(array_key_exists($value, $hashMapArrayA)){
                    $hashMapArrayA[$value] = $hashMapArrayA[$value] + 1;     
                } else {
                    $hashMapArrayA[$value] = 1;
                }
            }



            #building hashMapB
            #hashMapB[1] = 1
            #hashMapB[2] = 1
            #hashMapB[3] = 1
            #hashMapAB[4] = 1

            foreach ($arr_b as $key => $value) {
                if(array_key_exists($value, $hashMapArrayB)){
                    $hashMapArrayB[$value] = $hashMapArrayB[$value] + 1;     
                } else {
                    $hashMapArrayB[$value] = 1;
                }
            }

            
            #sanity check 
            $sizehashMapArrayB = count($hashMapArrayB); #4
            $sizehashMapArrayA = count($hashMapArrayA); #4
            if ($sizehashMapArrayA !== $sizehashMapArrayB)
            {
                return false;
            }
            

            foreach($hashMapArrayA as $key => $valueA) {
                #hashMapA[1] exist
                #hashMapA[1] == hashMapB[1] equals
                #hashMapA[1] exist
                #hashMapA[1] == hashMapB[1] equals

                if(!array_key_exists($key, $hashMapArrayB))
                {
                    return false;
                }
                    
                if ($hashMapArrayA[$key] !== $hashMapArrayA[$key]) 
                {
                    return false;;
                 }

            }
            
                foreach($hashMapArrayB as $key => $valueB) {
                #hashMapA[1] exist
                #hashMapA[1] == hashMapB[1] equals
                #hashMapA[1] exist
                #hashMapA[1] == hashMapB[1] equals
                    


                if(!array_key_exists($key, $hashMapArrayA))
                {
                    return false;
                }
                    
                if ($hashMapArrayB[$key] !== $hashMapArrayA[$key]) 
                {
                    return false;;
                 }
            }
            

  
            $isEqual = true;
            return $isEqual;
}  

Leave a Reply