Skip to content

Commit

Permalink
add markdown
Browse files Browse the repository at this point in the history
  • Loading branch information
jaebradley committed Aug 15, 2017
1 parent b633f51 commit fc5b3f8
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions codereview/pairDifferenceCounter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
## Problem

Adapted from [this HackerRank Problem](https://www.hackerrank.com/challenges/pairs/problem).

> Given `N` integers, count the number of pairs of integers whose difference is `K`.
## Approach

1. Given the initial `int` array, sort it.
2. Now iterate through the indices in the array using a lower index (starting at `0`) and an upper index (starting at `1`)`.
3. Calculate the difference between the value at the upper index value and the value at the lower index
a. If the difference is equal to the target difference, increment the count and increment the upper index.
b. If the difference is less than the target difference, increment the upper index. A difference less than the target
difference implies that the value at the upper index is not large enough.
c. If the difference is greater than the target difference, increment the lower index. A difference greater than the
target implies that the value at the lower index is not large enough.
4. Return the count

## Implementation

<!--language: lang-java -->

public class PairDifferenceCounter {
public static int countPairsWithDifference(int[] values, int difference) {
Arrays.sort(values);

int count = 0;
int lowerIndex = 0;
int upperIndex = 1;

while (upperIndex < values.length) {
int pairDifference = values[upperIndex] - values[lowerIndex];

if (pairDifference == difference) {
count++;
upperIndex++;
} else if (pairDifference > difference) {
lowerIndex++;
} else {
upperIndex++;
}
}

return count;
}
}

0 comments on commit fc5b3f8

Please sign in to comment.