MCPcopy Create free account
hub / github.com/careercup/ctci / findOpenNumber

Method findOpenNumber

java/Chapter 10/Question10_3/QuestionB.java:12–49  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

10 public static int[] blocks = new int[blockNum];
11
12 public static void findOpenNumber() throws FileNotFoundException {
13 int starting = -1;
14 Scanner in = new Scanner (new FileReader ("Chapter 10/Question10_3/input_file_q10_3.txt"));
15 while (in.hasNextInt()) {
16 int n = in.nextInt();
17 blocks[n / (bitfield.length * 8)]++;
18 }
19
20 for (int i = 0; i < blocks.length; i++) {
21 if (blocks[i] < bitfield.length * 8){
22 /* if value < 2^20, then at least 1 number is missing in
23 * that section. */
24 starting = i * bitfield.length * 8;
25 break;
26 }
27 }
28
29 in = new Scanner(new FileReader("Chapter 10/Question10_3/input_file_q10_3.txt"));
30 while (in.hasNextInt()) {
31 int n = in.nextInt();
32 /* If the number is inside the block that�s missing
33 * numbers, we record it */
34 if (n >= starting && n < starting + bitfield.length * 8) {
35 bitfield [(n-starting) / 8] |= 1 << ((n - starting) % 8);
36 }
37 }
38
39 for (int i = 0 ; i < bitfield.length; i++) {
40 for (int j = 0; j < 8; j++) {
41 /* Retrieves the individual bits of each byte. When 0 bit
42 * is found, finds the corresponding value. */
43 if ((bitfield[i] & (1 << j)) == 0) {
44 System.out.println(i * 8 + j + starting);
45 return;
46 }
47 }
48 }
49 }
50
51 public static void main(String[] args) throws FileNotFoundException {
52 findOpenNumber();

Callers 1

mainMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected