Untitled

mail@pastecode.io avatar
unknown
plain_text
3 years ago
2.6 kB
3
Indexable
Never
const input = `3
2
a b
3
a b c
4
d e f g
`

function readInput(input) {
    var lines = input.split('\n');
    var test = parseInt(lines[0]);
    var arrs = [];
    for (let i=0; i<test; i++) {
        var nArr = lines[i*2+1];
        arrs.push(lines[i*2+2].split('\n'));
    }
    return arrs
}
// console.log(readInput(input));



function findPrimeFactorsAtTh(n, th) {
    if (n == 0) return 0;
    let count = 0;
    let dividable = false;
    while(n>1) {
        for (let i=2; i<=n; i++) {
            if (n % i == 0) {
                n = n/i;
                count++;
                if (count == th) return i;
                dividable = true;
                break;
            }
        }
        // if (!dividable) return null;
    }
}

function substractPrimeFactor (n, th) {
    if (n == 0) return null;
    while(n) {
        const result = findPrimeFactorsAtTh(n,th);
        // console.log('result', result)
        if (!result) return n;
        n = n-result;
        // console.log('n', n)
    }
    return n;
}

function findTheLargestSubString(a, b) {
    const obj = {}
    const str1 = a.length > b.length ? a : b;
    const str2 = a.length < b.length ? a : b;
    for (let i=0; i<str1.length-1; i++) {
        let str = str1[i];
        for (let j=i+1; j<str1.length; j++) {
            str += str1[j];
            obj[str] = 1;
        }
    }
    let max = "";
    for (let i=0; i<str2.length; i++) {
        let str = str2[i];
        for (let j=i+1; j<str2.length; j++) {
            str += str2[j];
            if (obj[str] && str.length > max.length) {
                max = str;
            }
        }
    }
    return max;
}

function findTheLargestSubString2(str1, str2) {
    let result = "";
    for (let i=0; i<str1.length; i++) {
        let match = "";
        for (let j=0; j<str2.length; j++) {
            if ((i + j < str1.length) && str1[i+j] == str2[j]) {
                match += str2[j];
                if (match.length > result.length) result = match;
            } else {
                match = ""
            }
        }
    }
    return result;
}


console.log(findTheLargestSubString2("apple pie available", "apple pies"));
// console.log(substractPrimeFactor(20, 2));

function gcd(a, b)
{
    if (b === 0) {
        return a;
    }
    return gcd(b, a % b);
}
 
function findMaxGCD(arr, n)
{
 
    let maxGCD = 0;
 
    for (let i = 0; i < n - 1; i++) {
        let tmp = gcd(arr[i], arr[i + 1]);
        if (tmp > maxGCD) {
            maxGCD = tmp;
        }
    }
 
    console.log("---------", maxGCD);
}
 
let arr = [2, 3, 4, 4, 4];

let n = arr.length;

findMaxGCD(arr, n);