-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPairs_with_Xor.java
83 lines (50 loc) · 1.25 KB
/
Pairs_with_Xor.java
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
/*Pairs With Given Xor
Problem Description
Given an 1D integer array A containing N distinct integers.
Find the number of unique pairs of integers in the array whose XOR is equal to B.
NOTE:
Pair (a, b) and (b, a) is considered to be same and should be counted once.
Problem Constraints
1 <= N <= 105
1 <= A[i], B <= 107
Input Format
First argument is an 1D integer array A.
Second argument is an integer B.
Output Format
Return a single integer denoting the number of unique pairs of integers in the array A whose XOR is equal to B.
Example Input
Input 1:
A = [5, 4, 10, 15, 7, 6]
B = 5
Input 2:
A = [3, 6, 8, 10, 15, 50]
B = 5
Example Output
Output 1:
1
Output 2:
2
Example Explanation
Explanation 1:
(10 ^ 15) = 5
Explanation 2:
(3 ^ 6) = 5 and (10 ^ 15) = 5 */
public class Solution {
public int solve(int[] A, int B) {
HashMap<Integer, Integer> m = new HashMap<Integer, Integer>();
int res = 0;
for (int i = 0; i <A.length; i++)
{
int t =B^A[i];
if (m.containsKey(t))
{
res++;
}
if(!m.containsKey(A[i]))
{
m.put(A[i],i);
}
}
return res;
}
}