Redirecting to

Range Addition

Try First, Check Solution later

1. You should first read the question and watch the question video.
2. Think of a solution approach, then try and submit the question on editor tab.
3. We strongly advise you to watch the solution video for prescribed approach.

1. You are given a number N.
2. Assume you have an array of length N initialised with all 0's.
3. You are also given K update operations.
4. Each operation contain 3 numbers: startIndex, endIndex and inc and updates each element of the subarray arr[startIndex, endIndex](both inclusive) with inc.
5. You have to find the resultant array when all K operations are executed.
6. display is a utility function, feel free to use it for debugging purposes.
7. main takes input from the users.
8. This is a functional problem.
9. You have to complete the getModifiedArray function. It takes as input a length and a 2D array of update operations. It should return the modified array.
10. Don't change the code of main and display.

Expected Complexity : O(n+k)
Input Format
First line takes input N, the length of the array.
Second line takes input K, the number of update operations.
Next K lines take input 3 space separated integers representing the startIndex, endIndex and inc.
Input is handled for you.
Output Format
The resultant array.
Output is handled for you.
Question Video
1 <= N <= 30000
Sample Input
0 2 -1
2 2 2
1 3 1
Sample Output
-1 0 2 1

  • Asked in Companies
  • Related Topics

Video Solution

Code Solution

Id Name