## Monday, November 30, 2015

### Buttercola: Zenefits: Two Minus

Buttercola: Zenefits: Two Minus

Solution:
1. Sort the array
2. Maintain two pointers, lo and hi, points to index 0 and 1, respectively.
3. If nums[hi] - nums[lo] == k. Add into result. Then we need to check the duplicates for lo + 1, lo + 2. .... If no duplicates, hi++.
4. if nums[hi] - nums[lo] > k. lo++
5. If nums[hi] - nums[lo] < k, hi++.

`  ``public` `List<List<Integer>> findTwoMinus(``int``[] nums, ``int` `k) {`
`    ``List<List<Integer>> result = ``new` `ArrayList<>();`
`    ``if` `(nums == ``null` `|| nums.length < ``2``) {`
`      ``return` `result;`
`    ``}`
`    `
`    ``Arrays.sort(nums);`
`    `
`    ``int` `lo = ``0``;`
`    ``int` `hi = ``1``;`
`    ``int` `n = nums.length;`
`    `
`    ``while` `(lo < n && hi < n) {`
`      ``if` `(nums[hi] - nums[lo] == k) {`
`        ``List<Integer> curr = ``new` `ArrayList<>();`
`        ``curr.add(nums[lo]);`
`        ``curr.add(nums[hi]);`
`        ``result.add(curr);`
`        `
`        ``while` `(lo + ``1` `< n && nums[lo] == nums[lo + ``1``]) {`
`          ``result.add(curr);`
`          ``lo++;`
`        ``}`
`        ``hi++;`
`        `
`      ``} ``else` `if` `(nums[hi] - nums[lo] < k) {`
`        ``hi++;`
`      ``} ``else` `{`
`        ``lo++;`
`      ``}`
`    ``}`
`    `
`    ``return` `result;`
`  ``} `