Skip to content

Commit 47f27d2

Browse files
committed
Hackerrank 30 Days Of Code complete
1 parent 0105da4 commit 47f27d2

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package hackerrank.ThirtyDaysOfCode;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
import java.io.*;
7+
import java.math.*;
8+
import java.security.*;
9+
import java.text.*;
10+
import java.util.*;
11+
import java.util.concurrent.*;
12+
import java.util.function.*;
13+
import java.util.regex.*;
14+
import java.util.stream.*;
15+
import static java.util.stream.Collectors.joining;
16+
import static java.util.stream.Collectors.toList;
17+
18+
19+
public class TwentyNine {
20+
public static void main(String[] args) throws IOException {
21+
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
22+
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
23+
24+
int t = Integer.parseInt(bufferedReader.readLine().trim());
25+
26+
IntStream.range(0, t).forEach(tItr -> {
27+
try {
28+
String[] firstMultipleInput = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");
29+
30+
int count = Integer.parseInt(firstMultipleInput[0]);
31+
32+
int lim = Integer.parseInt(firstMultipleInput[1]);
33+
34+
int res = Result.bitwiseAnd(count, lim);
35+
36+
bufferedWriter.write(String.valueOf(res));
37+
bufferedWriter.newLine();
38+
} catch (IOException ex) {
39+
throw new RuntimeException(ex);
40+
}
41+
});
42+
43+
bufferedReader.close();
44+
bufferedWriter.close();
45+
}
46+
}
47+
class Result {
48+
49+
/*
50+
* Complete the 'bitwiseAnd' function below.
51+
*
52+
* The function is expected to return an INTEGER.
53+
* The function accepts following parameters:
54+
* 1. INTEGER N
55+
* 2. INTEGER K
56+
*/
57+
58+
public static int bitwiseAnd(int N, int K) {
59+
int max = 0;
60+
61+
for (int i=1; i<N+1; i++) {
62+
63+
for (int j=i+1; j<N+1; j++){
64+
int andOp = i & j;
65+
66+
if ( andOp < K && andOp > max )
67+
max = andOp;
68+
}
69+
}
70+
71+
return max;
72+
}
73+
74+
}
75+
76+
77+
78+
class ResultTest {
79+
80+
@Test
81+
void checkBitwise() {
82+
int N = 5, K = 2, expected = 1;
83+
int N1 = 8, K1 = 5, expected1 = 4;
84+
int N2 = 2, K2 = 2, expected2 = 0;
85+
Assertions.assertEquals(expected, Result.bitwiseAnd(N,K));
86+
Assertions.assertEquals(expected1, Result.bitwiseAnd(N1,K1));
87+
Assertions.assertEquals(expected2, Result.bitwiseAnd(N2,K2));
88+
}
89+
90+
@Test
91+
public void given_binaryLiteral_thenReturnDecimalValue() {
92+
93+
byte five = 0b101;
94+
Assertions.assertEquals((byte) 5, five);
95+
96+
short three = 0b11;
97+
Assertions.assertEquals((short) 3, three);
98+
99+
int nine = 0B1001;
100+
Assertions.assertEquals(9, nine);
101+
102+
long twentyNine = 0B11101;
103+
Assertions.assertEquals(29, twentyNine);
104+
105+
int minusThirtySeven = -0b100101;
106+
Assertions.assertEquals(-37, minusThirtySeven);
107+
108+
}
109+
}

0 commit comments

Comments
 (0)