I want to iterate an array and find all pairs where their difference is 2
This is what i have so far:
var numberOfCases = 5;
var diff = 2;
var input = [1,5,3,4,2];
getPossiblepairs(input);
function getPossiblepairs(input){
for(cmp in input){
for(number in input){
if((input[cmp] - input[number]) == diff){
console.log("("+input[cmp]+","+input[number]+")");
}
}
}
}
This works but i still feel guilty using two for loops as the bigO is O(n^2) Is this the only way to do this?