Skip to content

Commit 37d8e25

Browse files
committed
Day 29: Bitwise AND
1 parent a77b8b2 commit 37d8e25

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed
+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import java.io.BufferedReader;
2+
import java.io.BufferedWriter;
3+
import java.io.FileWriter;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.util.stream.IntStream;
7+
8+
public class Solution000030 {
9+
10+
// Day 29: Bitwise AND
11+
// https://www.hackerrank.com/challenges/30-bitwise-and/problem?isFullScreen=true
12+
13+
static class Result {
14+
15+
public static int bitwiseAnd(int n, int k) {
16+
int max = 0;
17+
for (int i = 1; i <= n; i++) {
18+
for (int j = i + 1; j <= n; j++) {
19+
int andResult = i & j;
20+
if (andResult < k && andResult > max) {
21+
max = andResult;
22+
}
23+
}
24+
}
25+
return max;
26+
}
27+
28+
}
29+
30+
public static void main(String[] args) throws IOException {
31+
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
32+
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
33+
34+
int t = Integer.parseInt(bufferedReader.readLine().trim());
35+
36+
IntStream.range(0, t).forEach(tItr -> {
37+
try {
38+
String[] firstMultipleInput = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");
39+
40+
int count = Integer.parseInt(firstMultipleInput[0]);
41+
42+
int lim = Integer.parseInt(firstMultipleInput[1]);
43+
44+
int res = Result.bitwiseAnd(count, lim);
45+
46+
bufferedWriter.write(String.valueOf(res));
47+
bufferedWriter.newLine();
48+
} catch (IOException ex) {
49+
throw new RuntimeException(ex);
50+
}
51+
});
52+
53+
bufferedReader.close();
54+
bufferedWriter.close();
55+
}
56+
57+
}

0 commit comments

Comments
 (0)