27.10.2014 Views

Cracking the Coding Interview, 4 Edition - 150 Programming Interview Questions and Solutions

Cracking the Coding Interview, 4 Edition - 150 Programming Interview Questions and Solutions

Cracking the Coding Interview, 4 Edition - 150 Programming Interview Questions and Solutions

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Solutions</strong> to Chapter 5 | Bit Manipulation<br />

1 int findMissing(ArrayList array) {<br />

2 return findMissing(array, BitInteger.INTEGER_SIZE - 1);<br />

3 }<br />

4<br />

5 int findMissing(ArrayList input, int column) {<br />

6 if (column < 0) { // Base case <strong>and</strong> error condition<br />

7 return 0;<br />

8 }<br />

9 ArrayList oddIndices = new ArrayList();<br />

10 ArrayList evenIndices = new ArrayList();<br />

11 for (BitInteger t : input) {<br />

12 if (t.fetch(column) == 0) {<br />

13 evenIndices.add(t);<br />

14 } else {<br />

15 oddIndices.add(t);<br />

16 }<br />

17 }<br />

18 if (oddIndices.size() >= evenIndices.size()) {<br />

19 return (findMissing(evenIndices, column - 1))

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!