Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3a15e08

Browse files
Merge pull request #3 from nirmalnishant645/problem
Check for equality of two arrays
2 parents a49152e + a214c7f commit 3a15e08

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
'''
2+
Given two arrays A and B of equal size N, the task is to find if given arrays are equal or not. Two arrays are said to be equal if both of them contain same set of elements, arrangements (or permutation) of elements may be different though.
3+
Note : If there are repetitions, then counts of repeated elements must also be same for two array to be equal.
4+
5+
Input:
6+
The first line of input contains an integer T denoting the no of test cases. Then T test cases follow. Each test case contains 3 lines of input. The first line contains an integer N denoting the size of the array. The second line contains element of array A[]. The third line contains elements of the array B[].
7+
8+
Output:
9+
For each test case, print 1 if the arrays are equal else print 0.
10+
11+
Constraints:
12+
1<=T<=100
13+
1<=N<=107
14+
1<=A[],B[]<=1018
15+
16+
Example:
17+
Input:
18+
2
19+
5
20+
1 2 5 4 0
21+
2 4 5 0 1
22+
3
23+
1 2 5
24+
2 4 15
25+
26+
Output:
27+
1
28+
0
29+
30+
Explanation:
31+
Testcase1:
32+
Input : A[] = {1, 2, 5, 4, 0}; B[] = {2, 4, 5, 0, 1};
33+
Output : 1
34+
35+
Testcase2:
36+
Input : A[] = {1, 2, 5}; B[] = {2, 4, 15};
37+
Output : 0
38+
'''
39+
def equal(arr1, arr2):
40+
res = sum1 = sum2 = 0
41+
for i in range(len(arr1)):
42+
res ^= arr1[i]
43+
res ^= arr2[i]
44+
sum1 += arr1[i]
45+
sum2 += arr2[i]
46+
return 1 if not res and sum1 == sum2 else 0
47+
48+
t = int(input())
49+
while t > 0:
50+
n = int(input())
51+
arr1 = list(map(int, input().split()))
52+
arr2 = list(map(int, input().split()))
53+
print(equal(arr1, arr2))
54+
t -= 1

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /