Extra 5% OFF Use Code: OL05
Free Shipping over ₹999

Check two array are equal or not

In this example , we check two array are equal or not

#include <iostream>
#include <bits/stdc++.h>
using namespace std;


bool checkarray(int arr1[], int arr2[], int size_arr1, int size_arr2){
    if(size_arr1 != size_arr2)
        return false;
    
    //sort both array 
    sort (arr1, arr1+size_arr1);
    sort (arr2, arr2+size_arr2);

    
    for(int i=0; i< size_arr1; i++)
        if(arr1[i] != arr2[i])
            return false;
    
    return true;
}

int main(){
    int arr1[] = {1,2,4,7,9};
    int arr2[] = {2,4,6,9,1};

    int size_arr1 = sizeof(arr1)/sizeof(int);
    int size_arr2 = sizeof(arr2)/sizeof(int);

    //Function call
    if(checkarray(arr1,arr2,size_arr1,size_arr2))
        cout<<"Equal"<<endl;
    else
        cout<<"Not Equal";
}

Output:

Not Equal

Explanation:

Step 1: Including Libraries and Namespace

The code includes essential libraries for input/output operations and algorithm functions (like sorting):

#include <iostream>
#include <bits/stdc++.h>  // Includes all standard C++ libraries (commonly used in competitive programming)
using namespace std;

Step 2: Function: checkarray

The checkarray function takes two arrays (arr1 and arr2) and their sizes as arguments and checks whether the arrays are equal in terms of their elements (regardless of the order).

Steps of the Function:

  1. Check if Arrays Have Equal Size:
    • The function first checks if the two arrays have the same size. If not, it returns false, because arrays of different sizes cannot be equal.
if (size_arr1 != size_arr2)
    return false;

2. Sorting Both Arrays:

  • The function sorts both arrays using the sort() function from the <algorithm> library. Sorting ensures that if the arrays have the same elements but in different orders, they will become identical after sorting.
sort(arr1, arr1 + size_arr1);  // Sort arr1
sort(arr2, arr2 + size_arr2);  // Sort arr2

Error in Sorting: There is an issue with the sorting line for arr1. It should be:

sort(arr1, arr1 + size_arr1);

The original code mistakenly tries to sort arr1 using the range [arr1, arr2+size_arr1], which mixes both arrays. You should sort arr1 in the range arr1, arr1+size_arr1.

  1. Compare the Sorted Arrays:
    • After sorting, the function compares both arrays element by element. If any corresponding elements differ, the function returns false, indicating the arrays are not equal.
for (int i = 0; i < size_arr1; i++)
    if (arr1[i] != arr2[i])  // If any element is not equal, return false
        return false;

4. Return true:

  • If all the elements are identical, the function returns true, meaning the arrays are equal.
return true;

Step 3: Main Function

The main() function initializes two arrays, calculates their sizes, and calls the checkarray function to determine if the arrays are equal.

int main(){
    int arr1[] = {1, 2, 4, 7, 9};  // First array
    int arr2[] = {2, 4, 6, 9, 1};  // Second array (with a different element)

    // Calculating the sizes of the arrays
    int size_arr1 = sizeof(arr1) / sizeof(int);  // Number of elements in arr1
    int size_arr2 = sizeof(arr2) / sizeof(int);  // Number of elements in arr2

    // Function call to check if arrays are equal
    if (checkarray(arr1, arr2, size_arr1, size_arr2))
        cout << "Equal" << endl;
    else
        cout << "Not Equal" << endl;
    
    return 0;
}

Explanation of Key Parts:

  1. Array Initialization: Two arrays are initialized with hardcoded values:
    • arr1: {1, 2, 4, 7, 9}
    • arr2: {2, 4, 6, 9, 1}
    These arrays have the same size (5 elements) but contain different elements, as arr2 contains 6 instead of 7.
int arr1[] = {1, 2, 4, 7, 9};
int arr2[] = {2, 4, 6, 9, 1};

2. Size Calculation: The size of each array is calculated by dividing the total memory allocated for the array (sizeof(arr1)) by the memory size of a single element (sizeof(int)). This gives the number of elements in each array.

int size_arr1 = sizeof(arr1) / sizeof(int);
int size_arr2 = sizeof(arr2) / sizeof(int);

3. Function Call: The checkarray() function is called with arr1, arr2, and their respective sizes. The result is used to print whether the arrays are equal or not.

if (checkarray(arr1, arr2, size_arr1, size_arr2))
    cout << "Equal" << endl;
else
    cout << "Not Equal" << endl;

    Leave a Reply

    Your email address will not be published.

    Need Help?