Created
December 19, 2013 16:04
-
-
Save dscataglini/8041664 to your computer and use it in GitHub Desktop.
Finding Pairs
Given N integers, count the total pairs of integers that have a difference of K.
Sample Input #00:
k = 2 n = 1 5 3 4 2 Sample Output #00:
3 Sample Input #01:
k = 1
n = 363374326 364147530 61825163 1073065718 1281246024 1399469912 428047635 491595254 879792181 1069262793 Sample Output #01:
0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'test/unit' | |
# Head ends here | |
def pairs( a,k) | |
#a is an array containing numbers and k is the difference. | |
end | |
class PairTest < Test::Unit::TestCase | |
def test_small_occurances | |
assert_equal 3, pairs([1, 5, 3, 4, 2], 2) | |
end | |
def test_for_zero_occurances | |
assert_equal 0, pairs([363374326, 364147530, 61825163, 1073065718, 1281246024, 1399469912, 428047635, 491595254, 879792181, 1069262793], 1) | |
end | |
end |
Java equivalent
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import java.util.HashSet;
// Head ends here
public class Pairs {
public static int pairs(int[] a, int k) {
}
}
public class PairTest {
@Test
public void testSmallOccurances() {
assertEquals(3, Pairs.pairs(new int[]{1, 5, 3, 4, 2}, 2));
}
@Test
public void testForZeroOccurances() {
assertEquals(0, Pairs.pairs(new int[]{363374326, 364147530, 61825163, 1073065718, 1281246024, 1399469912, 428047635, 491595254, 879792181, 1069262793}, 1));
}
}
javascript
const assert = require('assert');
// Head ends here
function pairs(a, k) {
}
class PairTest {
testSmallOccurances() {
let result = pairs([1, 5, 3, 4, 2], 2);
let expected = 3;
try {
assert.strictEqual(result, expected);
console.log("success");
} catch (error) {
console.log("failure");
}
}
testForZeroOccurances() {
let result = pairs(
[
363374326,
364147530,
61825163,
1073065718,
1281246024,
1399469912,
428047635,
491595254,
879792181,
1069262793,
],
1
);
let expected = 0;
try {
assert.strictEqual(result, expected);
console.log("success");
} catch (error) {
console.log("failure");
}
}
}
// Create an instance of PairTest
let test = new PairTest();
// Call the test methods
test.testSmallOccurances();
test.testForZeroOccurances();
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Finding Pairs Given N integers, count the total pairs of integers that have a difference of K.
Sample Input #00:
Sample Output #00:
Sample Input #01:
Sample Output #01: