Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Naive Approach - Program to check if two Arrays are Equal or not #30

Merged
merged 1 commit into from
Oct 8, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions Program to check if two Arrays are Equal or not.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// C++ program to implement the approach

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

// Function to check if both arrays are equal
bool checkArrays(int arr1[], int arr2[],
int n, int m)
{
// If lengths of array are not equal
// means array are not equal
if (n != m)
return false;

// Sort both arrays
sort(arr1, arr1 + n);
sort(arr2, arr2 + m);

// Linearly compare elements
for (int i = 0; i < n; i++)
if (arr1[i] != arr2[i])
return false;

// If elements are same
return true;
}

// Driver Code
int main()
{
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);

// Function call
if (checkArrays(arr1, arr2, N, M))
cout << "Equal";
else
cout << "Not Equal";
return 0;
}