Sunday, June 26, 2022
HomeWordPress DevelopmentC++ Program to verify if two Arrays are Equal or not

C++ Program to verify if two Arrays are Equal or not


Given two arrays arr1[] and arr2[] of size N and M respectively, the duty is to verify if the 2 arrays are equal or not. 

Be aware: Arrays are mentioned to be equal if and provided that each arrays include the identical parts and the frequencies of every ingredient in each arrays are the identical.

Examples:

Enter: arr1[] = {1, 2, 3, 4, 5}, arr2[] = {5, 4, 3, 2, 1}
Output : Equal
Rationalization: As each the arrays include identical parts.

Enter: arr1[] = {1, 5, 2, 7, 3, 8}, arr2[] = {8, 2, 3, 5, 7, 1}
Output : Equal

 

Naive Method: The essential technique to resolve the issue is as follows:

Apply sorting on each the arrays after which match every ingredient of 1 array with the ingredient at identical index of the opposite array.

Comply with the steps talked about beneath to implement the concept.

  • Test if the size of each the arrays are equal or not
  • Then type each the arrays, in order that we are able to evaluate each equal ingredient.
  • Linearaly iterate over each the arrays and verify if the weather are equal or not,
  • If equal then print Equal and if not then print Not equal.

Under is the implementation of the above strategy:

C++

  

#embrace <bits/stdc++.h>

utilizing namespace std;

  

bool checkArrays(int arr1[], int arr2[],

                 int n, int m)

{

    

    

    if (n != m)

        return false;

  

    

    type(arr1, arr1 + n);

    type(arr2, arr2 + m);

  

    

    for (int i = 0; i < n; i++)

        if (arr1[i] != arr2[i])

            return false;

  

    

    return true;

}

  

int primary()

{

    int arr1[] = { 1, 2, 3, 4, 5 };

    int arr2[] = { 5, 4, 3, 2, 1 };

    int N = sizeof(arr1) / sizeof(int);

    int M = sizeof(arr2) / sizeof(int);

  

    

    if (checkArrays(arr1, arr2, N, M))

        cout << "Equal";

    else

        cout << "Not Equal";

    return 0;

}

Time Complexity: O(N * logN) 
Auxiliary Area: O(1)

Environment friendly Method: The issue could be solved effectively utilizing Hashing (unordered_map),

Use hashing to rely the frequency of every ingredient of each arrays. Then traverse via the hashtables of the arrays and match the frequencies of the weather.

Comply with the beneath talked about steps to unravel the issue:

  • Test if the size of each the arrays are equal or not
  • Create an unordered map and retailer all parts and frequency of parts of arr1[] within the map.
  • Traverse over arr2[] and verify if rely of every ingredient in arr2[] matches with the rely in arr1[]. This may be accomplished by decrementing the frequency whereas traversing in arr2[].

Under is the implementation of the above strategy:

C++

  

#embrace <bits/stdc++.h>

utilizing namespace std;

  

bool checkArrays(int arr1[], int arr2[],

                 int n, int m)

{

    

    if (n != m)

        return false;

  

    

    

    unordered_map<int, int> mp;

    for (int i = 0; i < n; i++)

  

        mp[arr1[i]]++;

  

    

    

    

    for (int i = 0; i < n; i++) {

  

        

        

        if (mp.discover(arr2[i]) == mp.finish())

            return false;

  

        

        

        if (mp[arr2[i]] == 0)

            return false;

  

        

        

        mp[arr2[i]]--;

    }

    return true;

}

  

int primary()

{

    int arr1[] = { 1, 2, 3, 4, 5 };

    int arr2[] = { 4, 3, 1, 5, 2 };

    int N = sizeof(arr1) / sizeof(int);

    int M = sizeof(arr2) / sizeof(int);

  

    

    if (checkArrays(arr1, arr2, N, M))

        cout << "Equal";

    else

        cout << "Not Equal";

    return 0;

}

Time Complexity: O(N) 
Auxiliary Area: O(N)

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments