DEV Community 👩‍💻👨‍💻

ZeeshanAli-0704
ZeeshanAli-0704

Posted on

Array Manipulation - HackerRank

function arrayManipulation(n, queries) {
    console.log(n, q)
 const arr = Array(n + 1);
    let maxValue = 0,
    currentNumber = 0;
    queries.forEach(([startRange, endRange, value]) => {
         arr[startRange] = arr[startRange] || { start: 0, end: 0 };
         arr[endRange] = arr[endRange] || { start: 0, end: 0 };
         arr[startRange].start += value;
         arr[endRange].end += value;
     });
    arr.forEach((cell) => {
       if (cell) {
         currentNumber += cell.start;
         if (currentNumber > maxValue) {
            maxValue = currentNumber;
         }
         currentNumber -= cell.end;
     }
   });
 return maxValue;

}

Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Head to your account's Settings to...

🌚 Enable dark mode
🔠 Change your default font
📚 Adjust your experience level to see more relevant content