-
Notifications
You must be signed in to change notification settings - Fork 0
/
The problem of identical arrays.cpp
83 lines (70 loc) · 1.36 KB
/
The problem of identical arrays.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
PROBLEM:
Two arrays of size N are called identical arrays if they contain the same elements. The order of elements in both arrays could be different; however, both the arrays must contain same elements. You are given two arrays of size N.
You need to determine if the arrays are identical or not.
Input:
The first line of the input contains a single integer T, denoting the number oftest cases. Then T test case follows. Each test case contains 3 lines:-
size of array N
elements of array1
elements of array2
Output:
Print 1 if the arrays are identical, and print 0 if they are not identical.
Constraints:
1<=T<=100
1<=N<=1000
0<=A[i]<=9
Example:
Input:
3
5
1 2 3 4 5
3 4 1 2 5
4
1 2 3 4
1 2 3 9
1
8
8
Output:
1
0
1
SOLUTION:
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin>>t;
while(t--)
{
int n,i,flag=1;
cin>>n;
int a[n],b[n];
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(i=0;i<n;i++)
cin>>b[i];
unordered_map<int,int> m;
for(i=0;i<n;i++)
{
m[a[i]]++;
}
for(i=0;i<n;i++)
{
if(m.find(b[i])==m.end())
{
flag=0;
break;
}
else
{
m[b[i]]--;
if(m[b[i]]==0)
m.erase(b[i]);
}
}
cout<<flag<<endl;
}
return 0;
}