How to find whether an array include a pair of which product equals to N in Node.js

1 Answer

0 votes
function ProductExists(arr, N) {
    const size = arr.length;
    
    if (size < 2) {
        return false;
    }
    
    let set = new Set();
    
    for (let i = 0; i < size; i++) {
        if (arr[i] == 0 && N == 0) {
            return true;
        }
        if (N % arr[i] == 0) {
            if (set.has(Math.floor(N / arr[i]))) {
                return true;
            }
            set.add(arr[i]);
        }
    }
    return false;
}
        
const arr = [5, 7, 13, 25, 9, 3, 4];
const N = 21;
        
if (ProductExists(arr, N)) {
    console.log("Yes");
}
else {
    console.log("No");
}




/*
run:
  
Yes
  
*/

 



answered Jul 22, 2023 by avibootz
edited Jul 22, 2023 by avibootz
...