problem no 66: check if two arrays are equal.

 Given two arrays A and B of equal size N, the task is to find if given arrays are equal or not. Two arrays are said to be equal if both of them contain same set of elements, arrangements (or permutation) of elements may be different though.

Note : If there are repetitions, then counts of repeated elements must also be same for two array to be equal.

Example 1:

Input:
N = 5
A[] = {1,2,5,4,0}
B[] = {2,4,5,0,1}
Output: 1
Explanation: Both the array can be 
rearranged to {0,1,2,4,5}

Example 2:

Input:
N = 3
A[] = {1,2,5}
B[] = {2,4,15}
Output: 0
Explanation: A[] and B[] have only 
one common value.







code:


 bool check(vector<ll> A, vector<ll> B, int N) {

        //code here

        map<ll,ll> map1;

        map<ll,ll> map2;

        

        

        for(ll ele:A)

        {

            map1[ele]++;

        }

        for(ll ele: B)

        {

            map2[ele]++;

        }

        

        int flagforequal=1;

     for(auto ele:map1)

     {

         if(ele.second!=map2[ele.first])

         {

             flagforequal=0;

         }

     }

     

      if(flagforequal==1)

      {

          return true;

      }

      return false;

     

     

       

        

    }

Comments

Popular posts from this blog

problem 3: given two integers N and M. The problem is to find the number closest to N and divisible by M. If there are more than one such number, then output the one having maximum absolute value.

problem no 7:Given two numbers A and B, find Kth digit from right of AB.

problem no 18: number of buildings that can see the sun . ( or facing the sun.)