Skip to content

Instantly share code, notes, and snippets.

@dscataglini
Created December 19, 2013 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dscataglini/8041664 to your computer and use it in GitHub Desktop.
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
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
@dscataglini
Copy link
Author

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

@fkchang
Copy link

fkchang commented Dec 5, 2023

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));
    }
}

@fkchang
Copy link

fkchang commented Jan 18, 2024

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