-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheck if two arrays are equal or not.cpp
66 lines (53 loc) · 1.49 KB
/
Check if two arrays are equal or not.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#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) {
//code here
unordered_map<ll, int> freqA, freqB;
// Count the frequency of elements in the first array.
for (int i = 0; i < N; i++) {
freqA[A[i]]++;
}
// Count the frequency of elements in the second array.
for (int i = 0; i < N; i++) {
freqB[B[i]]++;
}
// Check if the frequencies of each element are the same in both arrays.
for (auto& entry : freqA) {
ll key = entry.first;
if (freqA[key] != freqB[key]) {
return false;
}
}
return true;
}
};
//{ 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 End