• + 0 comments

    For Javascript, this is my solution. I don't think this is optimal solution. Coz it is having O(n^2).

    function getTotalX(a, b) {
        // Write your code here
        a.sort((a,b)=>(a-b));
        b.sort((a,b)=>(a-b));
        let count = 0;
        let startVal = a[a.length - 1];
        let endVal = b[0];
        while (startVal <= endVal) {
            let firstArr = 0;
            let secArr = 0;
            for (let num of a) {
                if (startVal % num === 0) {
                    firstArr ++;
                }
            }
            for (let num of b) {
                if (num % startVal === 0) {
                    secArr ++;
                }
            }
            if (firstArr === a.length && secArr === b.length) {
                count ++;
            }
            startVal ++;
        }
        return count;
    }