Check if two arrays are equal or not

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








STUDYEVERTHING




// { Driver Code Starts
//Initial function template for C++

#include<bits/stdc++.h>
using namespace std;
#define ll long long

 // } Driver Code Ends
//User function template for C++

class Solution{
    public:

    //Function to check if two arrays are equal or not.
    bool check(vector<ll> A, vector<ll> B, int N) {
        bool ans =false;
        sort(A.begin(), A.end());
        sort(B.begin(), B.end());
            int count=0;
               for(int i=0; i<N; i++){
                   if(A[i]==B[i]){
                       count++;
                   }
               }
               if(count == N){
                   ans = true;
               }
               return ans;
    }
};

// { Driver Code Starts.
int main()
 {
    int t;
    cin>>t;
    while(t--) {
        int n;
        cin>>n;
       
        vector<ll> arr(n,0),brr(n,0);
       
        // increase the count of elements in first array
        for(ll i=0;i<n;i++)
            cin >> arr[i];
       
       
        // iterate through another array
        // and decrement the count of elements
        // in the map in which frequency of elements
        // is stored for first array
        for(ll i=0;i<n;i++)
            cin >> brr[i];
        Solution ob;
        cout << ob.check(arr,brr,n) << "\n";
    }
    return 0;
}  // } Driver Code Ends













Happy Coding 😊.





Post a Comment

If you have furthur QUERIES please let us know

Previous Post Next Post