Check two array are equal or not in O(n):π
Vishal Yadav
Posted on January 22, 2022
Map is the solution of that problem
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a[]={1,2,5,4,0};
int b[]={2,4,5,0,1};
int n=5;
//find that both array are equal in O(n)
map<int,int>mp1;
map<int,int>mp2;
for(int i=0;i<n;i++)
{
mp1[a[i]]++;
mp2[b[i]]++;
}
if(mp1==mp2)
cout<<"both same";
else
cout<<"different";
return 0;
}
π πͺ π
π©
Vishal Yadav
Posted on January 22, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.