• + 0 comments

    /* Any ideas of why my code is leading up to a large execution time. Thank you in advance */

    function arrayManipulation(n, queries) {

    if (n > 10 ** 7) {
        n = 10 ** 7
    };
    
    if (queries.length > 2 * (10 ** 5)){
        queries.length = 2 * (10 ** 5)
    };
    
    
    let array = [];
    array.length = n
    // value/ start / end
    array.fill(0, 0, n)
    
    let result = 0;
    
    
        queries.forEach((e)=> {
             let a = e[0] - 1; // queries[y][0]
             let b = e[1] - 1;  // queries[y][1]
             let k = e[2];      // queries[y][2]
    
    
              for (let i = a; i <= b; i++) {
                // add k to the values between array[i] && array[i]
                array[i] = array[i] + k;
    
    
    
            }    
    
    
        })
    
    
    result = Math.max(...array)
    
    return result
    

    }