-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitManipulation.java
More file actions
158 lines (127 loc) · 3.89 KB
/
Copy pathBitManipulation.java
File metadata and controls
158 lines (127 loc) · 3.89 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import java.util.*;
public class BitManipulation {
// (1) Given an array where all numbers appear 2 times except exactly 1 number
// which is present only once. Find the single number.
// TC: O(N) SC: O(1)
public static int unique(int[] A) {
int ans = 0;
for (int i = 0; i < A.length; i++) {
ans ^= A[i];
}
return ans;
}
// (2) Check if the ith bit is set(1) or unset(0).
public static boolean checkBit(int A, int i) {
return ((1 << i) & A) > 0;
}
// (3) Set the ith bit.
public static int setBit(int A, int i) {
return ((1 << i) | A);
}
// (4) Given an array of N-1 numbers having all the numbers in the range [1,N]
// except one number. Find the missing number.
// TC: O(N)
public static int missingNum(int[] A) {
int N = A.length;
int missing = 0;
for (int i = 0; i < N; i++) {
missing ^= A[i];
}
for (int i = 1; i <= N; i++) {
missing ^= i;
}
return missing;
}
// (5) Given an array where all the numbers are present two times except two
// single numbers. Return both the numbers.
public static int getSetBit(int N) {
for (int i = 0; i < 32; i++) {
if (((1 << i) & N) > 0)
return i;
}
return -1;
}
public static int[] uniqueNums2(int[] A) {
int N = A.length;
int xor = 0;
for (int i = 0; i < N; i++) {
xor ^= A[i];
}
int p = getSetBit(xor);
int ans1 = 0, ans2 = 0;
for (int i = 0; i < N; i++) {
if (((1 << p) & A[i]) > 0) {
ans1 ^= A[i];
} else {
ans2 ^= A[i];
}
}
return new int[] { ans1, ans2 };
}
// (6) Given an array where all the elements appear 3 times except 1 element
// which is present only once. Find the single number.
public static int single(int[] A) {
int N = A.length;
int ans = 0;
for (int i = 0; i < 32; i++) {
int count = 0;
for (int j = 0; j < N; j++) {
if (((1 << i) & A[j]) > 0)
count++;
}
if (count % 3 != 0) {
ans = ans | (1 << i);
}
}
return ans;
}
// (7) HAMMING DISTANCE
// TC: O(N)
public static int HD(int[] A) {
int N = A.length;
int ans = 0;
for (int i = 0; i < 32; i++) {
int count = 0;
for (int j = 0; j < N; j++) {
if (((1 << i) & A[j]) > 0) {
count++;
}
}
ans += count * (N - count);
}
return ans;
}
// (8) Given an array. Return the min XOR of any two numbers in the array.
// TC: O(N)
public static int minXOR(int[] A) {
Arrays.sort(A);
int N = A.length;
int ans = Integer.MAX_VALUE;
for (int i = 0; i < N - 1; i++) {
ans = Math.min(ans, (A[i] ^ A[i + 1]));
}
return ans;
}
// (9) Given an array. Return the max & pair.
public static int maxAnd(int[] A) {
int N = A.length;
int ans = 0;
for (int i = 31; i >= 0; i--) {
int count = 0;
for (int j = 0; j < N; j++) {
if (((1 << i) & A[j]) > 0) {
count++;
}
}
if (count >= 2) {
ans = ((1 << i) | ans);
for (int j = 0; j < N; j++) {
if (!(((1 << i) & A[j]) > 0)) {
A[j] = 0;
}
}
}
}
return ans;
}
}