Array Manipulation

  • + 0 comments

    JS:

    • credit to: chuntao_liu_0118

    function arrayManipulation(n, queries) {

    let arr = Array(n+1).fill(0);
    // console.log("arr: ", arr);
    
    for(let [a,b,k] of queries){
        arr[a-1] = arr[a-1] + k;
        arr[b] = arr[b] - k;
    }
    
    let acc = 0;
    let result = 0;
    
    arr.forEach(el=> {
        acc = acc+el;
        result = Math.max(result, acc)
    })
    
    // console.log(result);
    return result; 
    
    // ! end of function
    

    }

    let n1 = 5; // length of the array let queries1 = [ [ 1, 2, 100 ], [ 2, 5, 100 ], [ 3, 4, 100 ] ]; // number of queries

    arrayManipulation(n1, queries1);