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 19 | Moderate<br />

19.11 Design an algorithm to find all pairs of integers within an array which sum to a specified<br />

value.<br />

SOLUTION<br />

pg 90<br />

One easy <strong>and</strong> (time) efficient solution involves a hash map from integers to integers. This algorithm<br />

works by iterating through <strong>the</strong> array. On each element x, look up sum - x in <strong>the</strong> hash<br />

table <strong>and</strong>, if it exists, print (x, sum - x). Add x to <strong>the</strong> hash table, <strong>and</strong> go to <strong>the</strong> next element.<br />

Alternate Solution<br />

Definition of Complement: If we’re trying to find a pair of numbers that sums to z, <strong>the</strong> complement<br />

of x will be z - x (that is, <strong>the</strong> number that can be added to x to make z). For example,<br />

if we’re trying to find a pair of numbers that sum to 12, <strong>the</strong> complement of –5 would be 17.<br />

The Algorithm: Imagine we have <strong>the</strong> following sorted array: {-2 -1 0 3 5 6 7 9 13 14 }. Let first<br />

point to <strong>the</strong> head of <strong>the</strong> array <strong>and</strong> last point to <strong>the</strong> end of <strong>the</strong> array. To find <strong>the</strong> complement<br />

of first, we just move last backwards until we find it. If first + last < sum, <strong>the</strong>n <strong>the</strong>re is no complement<br />

for first. We can <strong>the</strong>refore move first forward. We stop when first is greater than last.<br />

Why must this find all complements for first? Because <strong>the</strong> array is sorted <strong>and</strong> we’re trying<br />

progressively smaller numbers. When <strong>the</strong> sum of first <strong>and</strong> last is less than <strong>the</strong> sum, we know<br />

that trying even smaller numbers (as last) won’t help us find a complement.<br />

Why must this find all complements for last? Because all pairs must be made up of a first <strong>and</strong><br />

a last. We’ve found all complements for first, <strong>the</strong>refore we’ve found all complements of last.<br />

1 public static void printPairSums(int[] array, int sum) {<br />

2 Arrays.sort(array);<br />

3 int first = 0;<br />

4 int last = array.length - 1;<br />

5 while (first < last) {<br />

6 int s = array[first] + array[last];<br />

7 if (s == sum) {<br />

8 System.out.println(array[first] + “ “ + array[last]);<br />

9 ++first;<br />

10 --last;<br />

11 } else {<br />

12 if (s < sum) ++first;<br />

13 else --last;<br />

14 }<br />

15 }<br />

16 }<br />

CareerCup.com<br />

2 7 8

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

Saved successfully!

Ooh no, something went wrong!