• + 3 comments

    You can try to visualize the array as steps / stairs

    We are just noting down the bump ups and bump downs

    • + 3 comments

      I still havent understood this logic.Even though i implemented this logic in java with ease,i dont understand how this logic helps us arrive at the solution.

      • + 2 comments

        me netheir, I am looking for the maths here, I am pretty sure the solution has a math method. Somebody here wrote "Prefix sum".

        • + 0 comments

          I tried an answer in the spirit of digital signal processing here.

        • [deleted]
          + 11 comments

          After thinking like that i also understood the logic the solution.

          Let's think our summing part input like that {A B S} = {1 3 100} {2 5 150} {3 4 110} {2 4 160}

          Instead of writing all elements of array we can write maximum value at just starting and ending indexes to have less writing operation. So, after first input row, array can be something like that.

          0 100 0 100 0 0 0 0 0

          But the problem is here that even we didn't write anything, value of index 2 is also 100. When we wanted to continue with second step we have to check whether index 2 is between indexes of first row operation or not.

          Instead of doing like that we can write S value to index A and -S value to B+1, so it is still similar logic. Starting from A to B all indexes have S value and rest of them have less than these indexes as S as. Now the array is like that:

          0 100 0 0 -100 0 0 0 0

          While calculating second row, we are writing 150 to index 2 and -150 to index 6. It will be like that: 0 100 150 0 -100 0 -150 0 0

          If we write array with old method, which means that all numbers calculated one, it will be: 0 100 250 250 150 150 0 0 0

          It shows that value of index 2 is : 100+150 = 250. Value of index 5: 100 + 150 + (-100) = 150. So by calculating with the solution written above, instead of writing all numbers, we are writing changes at edge indexes.

          • + 2 comments

            check it out here, you will get all your doubts solved https://www.geeksforgeeks.org/difference-array-range-update-query-o1/

            • + 2 comments

              Below link will also help to understand theory behind it. https://www.geeksforgeeks.org/constant-time-range-add-operation-array/

              • + 10 comments

                Same solution in Javascript

                    var arr = [];
                    var max = 0;
                    // init each element of arr to 0
                    for (let l = 0; l < n; l++) {
                        arr[l] = 0;
                    }
                    // for each sum operation in queries
                    for (let i = 0; i < queries.length; i++) {
                       // update arr with number to add at index=queries[i][0]  and number to remove at index=queries[i][0]+1 => this will allow us to build each element of the final array by summing all elements before it. The aim of this trick is to lower time complexity
                        arr[queries[i][0]-1] += queries[i][2];
                        if (queries[i][1] < arr.length) {
                            arr[queries[i][1]] -= queries[i][2];
                        }
                    }
                    for (let j = 1; j < n; j++) {
                        arr[j] += arr[j-1];
                    }
                    for (let k = 0; k < arr.length; k++) {
                        max = Math.max(max, arr[k]);
                    }
                    //max = Math.max(...arr); // not working for big arrays
                    return max;
                
                • + 1 comment

                  Hey, I did the code in Java8 and my code is getting failed for input type - where only single value is present in a row of array. meaning only left index value is provided and right and k value is missing from array. So can you help me how to solve this issue?

                  • + 0 comments

                    you could post your code,and we can check it out

                • + 3 comments

                  simpler in es6:

                  function arrayManipulation(n, queries) { let arr = new Array(2*n).fill(0); let max = 0;

                  queries.forEach((item) => {
                      arr[item[0]] += item[2];
                      arr[item[1] + 1] -= item[2];
                  });
                  
                  arr.reduce((prev, curr, idx) => {
                      const sum = prev + curr;
                      if (sum > max) {
                          max = sum;
                      }
                  
                      return sum;
                  })
                  
                  return max;
                  

                  }

                  • + 1 comment

                    I did something pretty similar, just with a little bit more readable forEach:

                    const arr = new Array(n).fill(0);
                    let result = 0;
                    
                    queries.forEach(([a, b, k]) => {
                        arr[a - 1] += k;
                        if (b < arr.length) {
                            arr[b] -= k;
                        }
                    });
                    
                    arr.reduce((a, b) => {
                        const acc = a + b;
                        result = Math.max(result, acc);
                        return acc;
                    }, 0);
                    
                    return result;
                    
                    • + 3 comments

                      This produces wrong answer in some of the tests.

                      • + 2 comments

                        Hi,

                        try this. Here is the video tutorial for my solution O(n+m) complexity.

                        https://www.youtube.com/watch?v=hDhf04AJIRs&list=PLSIpQf0NbcCltzNFrOJkQ4J4AAjW3TSmA

                        Would really appreciate your feedback like and comment etc. on my video.

                        • + 1 comment

                          It is a good video. I understood the algorithm clearly.

                          • + 1 comment

                            thanks @chandraprabha90.

                            but it would be great if you can please provide your comments, like, dislike on my video how i did it..It motivates me to create better content for my audience.

                            • + 2 comments

                              Could you add subtitles? I tried watching it but couldn't quite understand your accent through the audio

                              • + 0 comments

                                Hi Grozny,

                                I appologize for my bad sound quality but i am trying to improve it. but it will be very difficult to add subtitle for this video because its around half an hour which explains all the concepts in deep.

                                Making this long video took lot of effort and time now adding a subtitle will be very tedious without any support.

                                Will suggest you to try automatic transcribe feature from youtube to translate it.

                                Anyway thanks for watching.

                              • + 0 comments

                                Hi Grozny,

                                I have added subtitle for this tutorial and I hope it will you to understand logic with more clarity.

                        • + 1 comment
                          [deleted]
                          • + 0 comments

                            Nice approach you got there!

                      • + 0 comments

                        I had the same issue. my mistake was decrementing lower and upper. you don't decrement upper, the difference array needs to show it went down AFTER then last index, not within.

                  • + 0 comments

                    Thought to myself ....no js solutions here? then I find 3 of them, Js, ES6, readable wow.

                • + 0 comments

                  Your last for loop isn't needed. You can move Math.max to the previous for loop.

                • + 0 comments

                  Added some ES6 syntax suger...

                  const arrayManipulation2 = (n, queries) => {
                    const arr = new Array(n).fill(0);
                    let max = 0;
                    for (let i = queries.length - 1; i >= 0; i--) {
                      const [a, b, k] = queries[i];
                      arr[a - 1] += k;
                      if (b < arr.length) {
                        arr[b] -= k;
                      }
                    }
                    for (let j = 1; j < n; j++) {
                      arr[j] += arr[j - 1];
                    }
                    for (let k = arr.length - 1; k >= 0; k--) {
                      max = Math.max(max, arr[k]);
                    }
                    return max;
                  };
                  
                • + 0 comments

                  Got stuck because of this

                  max = Math.max(...arr); // not working for big arrays

                  Thanks man!

                • + 0 comments

                  You could simplify your code a smidge, and save a little processing power, by removing your final for loop, and putting in an if check in the second to last loop, like this:

                  for (let j = 1; j < n; j++) {
                  	arr[j] += arr[j-1];
                  	
                  	if (arr[i] > biggest) {
                  		biggest = arr[i];
                  	}
                  }
                  
                • + 1 comment
                  [deleted]
                  • + 0 comments

                    Perfect! Could you please explain me the thought process behind the solution?

                • + 0 comments

                  My js solution ran out of time for a few test cases

                  function arrayManipulation(n, queries) { let a = new Array(n).fill(0); for(let j = 0; j < queries.length; j++) { let temp = queries[j] let k = temp[0]; while(k <= temp[1]) { a[k-1] += temp[2]; k++; }

                  } return Math.max.apply(Math, a); }

              • + 0 comments

                this one is the one that finally made it click for me...crazy how many different explanations are "whoosh" until the right person finds the "right" explanations.

                But let's keep doing standardized testing in schools /s

            • + 0 comments

              Thanks for the post, it really made me understand the logic clearly

          • + 0 comments

            Thank you @Kemal_caymaz for the explanation, I have found this very useful.

          • + 0 comments

            Thank you!

          • + 0 comments

            thnx

          • + 1 comment

            super awesome X 1000!!!

            can you expalin this: But the problem is here that even we didn't write anything, value of index 2 is also 100. When we wanted to continue with second step we have to check whether index 2 is between indexes of first row operation or not.

            • + 2 comments

              Hi,

              I have created a video tutorial for you and uploaded the same on youtube. Here is the video tutorial for my solution O(n+m) complexity.

              https://youtu.be/hDhf04AJIRs

              Would really appreciate your feedback like, dislike , comment etc. on my video.

              • + 1 comment

                fantastic video, thank you!

                • + 0 comments

                  most welcome.

                  but if you dont mind can you please leave the same comment along with like, dislike on my video. it motivates me and help others too find the solution over internet.

              • + 0 comments

                impressive ...

          • + 0 comments

            thanks bro..

          • + 0 comments

            Hi,

            Most of the peope solved this problem but time complexity of solution is O(n*m) (due to two nested for loops)which can not be used to solve this problem for given time constraint, so you need better approach which beats O(n*m)

            I have created a video tutorial for you and uploaded the same on youtube with complete explanation along with code complexity analysis.

            Here is the video tutorial for my solution O(n+m) complexity passed all test cases.

            https://youtu.be/hDhf04AJIRs

            Would really appreciate your feedback like, dislike , comment etc. on my video.

          • + 1 comment

            Thanks a lot budy for your fantastic explanation ! Your thinking is really amazing like you . Good job.

            • + 0 comments

              most welcome. It would be great, if you can provide your feedback like, dislike , comment etc. on my video. It motivate me to do more for you all

          • + 1 comment

            hey used this logic its failing for 10 test cases with large inputs

            • + 0 comments

              Hi,

              Most of the peope solved this problem but time complexity of solution is O(n*m) (due to two nested for loops)which can not be used to solve this problem for given time constraint, so you need better approach which beats O(n*m)

              I have created a video tutorial for you and uploaded the same on youtube with complete explanation along with code complexity analysis.

              Here is the video tutorial for my solution O(n+m) complexity passed all test cases.

              https://youtu.be/hDhf04AJIRs

              Would really appreciate your feedback like, dislike , comment etc. on my video.

          • + 0 comments

            Thanks, your explanation is very helpful

          • + 0 comments

            Thank you so much for clearly explaining this.

      • + 1 comment

        did you got it?

        • + 0 comments

          getting it correct for few cases but when both indexes are same then its giving a error message

      • + 0 comments

        How can you implement the logic with ease without understanding the logic? Copy and pasting is not the same as "implementing"

    • + 0 comments

      To explain further for people confused:

      We are creating a "difference array" Which shows how many steps up or down have occurred (the difference between 0 and result of each operation) and where in the array they have occurred. This way, you can see how high the max ends up and return that for the solution.

      I found this explanation helpful to have this fact dawn on me after much noodling: https://www.geeksforgeeks.org/difference-array-range-update-query-o1/

    • + 0 comments

      Like piling up blocks? Adding a number -> going up one step and subtracting -> down . Finally, we count how high we can go.