Q:

Write a JavaScript program to find two elements of the array such that their absolute difference is not greater than a given integer but is as close to the said integer

0

Write a JavaScript program to find two elements of the array such that their absolute difference is not greater than a given integer but is as close to the said integer. 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

function different_values(ara, n) {
    var max_val = -1;
    for (var i = 0; i < ara.length; i++) {
        for (var j = i + 1; j < ara.length; j++) {
            var x = Math.abs(ara[i] - ara[j]);
            if (x <= n) {
                max_val = Math.max(max_val, x)
            }
        }
    }
    return max_val
}
console.log(different_values([12, 10, 33, 34], 10));
console.log(different_values([12, 10, 33, 34], 24));
console.log(different_values([12, 10, 33, 44], 40));

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now