I wrote the following code to answer this, but I am wondering if there is any better approach.
For example
If the following array is given:
{10,21,34,45,56}
we should find the differentiate of each number with other values of the array and in case it is equals to 11 should increment the counter.
10-21=-11 10-34=-24 10-45=-35 10-56=-46 21-10=11 < 21-34=-13 21-45=-24 21-56=-35 34-10=24 34-21=13 34-45=-11 34-56=-22 45-10=35 45-21=24 45-34=11 << 45-56=-11 56-10=46 56-21=35 56-34=22 56-45=11 <<<
Code
List<Integer> a = new ArrayList<Integer>();
a.add(10);
a.add(21);
a.add(34);
a.add(45);
a.add(56);
int number = 11;
int counter = 0;
for(int i=0;i<a.size();i++){
for(int j=0;j<a.size();j++){
if(j!=i){
int t = a.get(i) - a.get(j);
if(t == number){
System.err.println(a.get(i) +"-"+ a.get(j) + "=" + t);
counter++;
}
}
}
}
System.err.println(counter);
}
Output
The application is correctly showing the results but I am wondering if there is any other approach to the above solution.
21-10=11 45-34=11 56-45=11 3