Counting pairs that have a given difference in Java You will be given an integer k and a list of integers. Count the number of distinct valid pair of integers (a,b) in the list for which a+k=b.
  
  For example, the array [1,1,1,2] has two different valid pairs:(1,1) and (1,2).
  Note that the three possible instances od pair (1,1) count as a single valid pair, as do the three possible instances of the pair (1,2). if k=1, then this means we have total of one 1 valid pair which satisfies a+k=b=>1+1=2, the pair (1,2).


My code:

public class PairArrayStream {

    public static void main(String[] args) {
        int k =1;
        List&lt;Integer&gt; input = Arrays.asList(1,1,1,2);
        HashSet&lt;HashSet&gt; hs = new HashSet&lt;HashSet&gt;();
        IntStream.range(0,  input.size())
        .forEach(i -&gt; IntStream.range(0,  input.size())
            .filter(j -&gt; i != j &amp;&amp; input.get(i) - input.get(j) == k)
            .forEach(j -&gt; {                 
                 HashSet inner = new HashSet&lt;&gt;();
                 inner.add(input.get(j));
                 inner.add(input.get(i));
                 hs.add(inner);                 
            })
    );
        System.out.println("OutPut "+hs.size());
}

}


Without java 8 features::

int k =1;
  List&lt;Integer&gt; input = Arrays.asList(1,1,1,2);
  HashSet&lt;HashSet&gt; hs = new HashSet&lt;HashSet&gt;();    
   for(int i =0 ; i&lt;numbers.size();i++){
       for(int j = i; j&lt;numbers.size();j++){
           if(Math.abs(numbers.get(j)-numbers.get(i)) == k){
                HashSet inner = new HashSet&lt;&gt;();
                 inner.add(numbers.get(j));
                 inner.add(numbers.get(i));
                 hs.add(inner);
           }
       }
   }


Well, I am getting the correct output but 40% of the test cases gives me a timeout. Opinions and tactics are welcomed to make the code better and fast.
From a review of your code, I see that you loop over your input in a nested for loop. This is actually not required to solve the problem. Instead, create one hashset for every number in your numbers array, and another hashset for every number in numbers + k. Then we only have to check for intersections. 

This should take your \$O(n^2)\$ algorithm and turn it into a nicer \$O(n)\$. Here's my suggested code (note that I included an inner class to store the pairs, but you could use builtins to solve it too: 

private static class Pair {
    public int a;
    public int b;
    public Pair(int a, int b) {
        this.a = a;
        this.b = b;
    }
    public String toString() {
        return "("+a+","+b+")";
    }
}

public static List&lt;Pair&gt; getPairsFast(int k, List&lt;Integer&gt; numbers) {
    HashSet&lt;Integer&gt; hLow = new HashSet&lt;&gt;();
    HashSet&lt;Integer&gt; hHigh = new HashSet&lt;&gt;();
    List&lt;Pair&gt; ret = new ArrayList&lt;&gt;();
    for (int i : numbers) {
        hLow.add(i);
        hHigh.add(i+k);
    }
    for (int i : hHigh) {
        if (hLow.contains(i)) {
            ret.add(new Pair(i-k, i));
        }
    }
    return ret;
}


From some testing, it indeed seems to perform better the larger the input is, and it is >400 times faster for an input of size \$10^5\$.