`{"id":"eb054131-c5a4-402a-9e68-224ec8424d93","name":"Range Addition","description":"1. You are given a number N.\r\n 2. Assume you have an array of length N initialised with all 0's.\r\n 3. You are also given K update operations.\r\n 4. Each operation contain 3 numbers: startIndex, endIndex and inc and updates each element of the subarray arr[startIndex, endIndex](both inclusive) with inc.\r\n 5. You have to find the resultant array when all K operations are executed.\r\n 6. display is a utility function, feel free to use it for debugging purposes.\r\n 7. main takes input from the users.\r\n 8. This is a functional problem. \r\n 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.\r\n 10. Don't change the code of main and display.\r\n\r\nExpected Complexity : O(n+k)","inputFormat":"First line takes input N, the length of the array.\r\n Second line takes input K, the number of update operations.\r\n Next K lines take input 3 space separated integers representing the startIndex, endIndex and inc.\r\n Input is handled for you.","outputFormat":"The resultant array.\r\n Output is handled for you.","constraints":"1 &lt;= N &lt;= 30000","sampleCode":{"cpp":{"code":""},"java":{"code":"import java.util.*;\r\n \r\n public class Main {\r\n \r\n // This is a functional problem. You have to complete this function.\r\n // It takes as input a length and a 2D array of update operations.\r\n // It should return the modified array.\r\n public static int[] Range(int length, int[][] updates) {\r\n // write your code here. \r\n \r\n }\r\n \r\n public static void main(String[] args) {\r\n Scanner sc = new Scanner(System.in);\r\n \r\n // Input for length of first array.\r\n int length = sc.nextInt();\r\n \r\n int K = sc.nextInt();\r\n \r\n int[][] updates = new int[K];\r\n \r\n for (int i = 0; i < updates.length; i++) {\r\n for (int j = 0; j < updates.length; j++) {\r\n updates[i][j] = sc.nextInt();\r\n }\r\n }\r\n \r\n int[] result = Range(length, updates);\r\n display(result);\r\n }\r\n \r\n // function to display an array.\r\n public static void display(int[] arr) {\r\n \r\n for (int i = 0; i < arr.length; i++) {\r\n System.out.print(arr[i] + \" \");\r\n }\r\n \r\n System.out.println();\r\n }\r\n \r\n }"},"ruby":{"code":""},"python":{"code":""},"javascript":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"4 \r\n3\r\n0 2 -1\r\n2 2 2\r\n1 3 1","sampleOutput":"-1 0 2 1","questionVideo":"https://www.youtube.com/embed/-SDHYqxI-Hc","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"ed294401-c1be-4c10-a3f7-3e2e4f8d9755","name":"Range Queries For Experts","slug":"range-queries-for-experts-903","type":0},{"id":"2684424c-b1dd-4289-b0a2-3ade526b3874","name":"Range Addition","slug":"range-addition","type":1}],"next":{"id":"708eaf40-ad8a-4b4f-b9f6-8c4276e08d11","name":"Moksh And His Girlfriend","type":1,"slug":"moksh-and-his-girlfriend"},"prev":null}}`

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)

`{"id":"eb054131-c5a4-402a-9e68-224ec8424d93","name":"Range Addition","description":"1. You are given a number N.\r\n 2. Assume you have an array of length N initialised with all 0's.\r\n 3. You are also given K update operations.\r\n 4. Each operation contain 3 numbers: startIndex, endIndex and inc and updates each element of the subarray arr[startIndex, endIndex](both inclusive) with inc.\r\n 5. You have to find the resultant array when all K operations are executed.\r\n 6. display is a utility function, feel free to use it for debugging purposes.\r\n 7. main takes input from the users.\r\n 8. This is a functional problem. \r\n 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.\r\n 10. Don't change the code of main and display.\r\n\r\nExpected Complexity : O(n+k)","inputFormat":"First line takes input N, the length of the array.\r\n Second line takes input K, the number of update operations.\r\n Next K lines take input 3 space separated integers representing the startIndex, endIndex and inc.\r\n Input is handled for you.","outputFormat":"The resultant array.\r\n Output is handled for you.","constraints":"1 &lt;= N &lt;= 30000","sampleCode":{"cpp":{"code":""},"java":{"code":"import java.util.*;\r\n \r\n public class Main {\r\n \r\n // This is a functional problem. You have to complete this function.\r\n // It takes as input a length and a 2D array of update operations.\r\n // It should return the modified array.\r\n public static int[] Range(int length, int[][] updates) {\r\n // write your code here. \r\n \r\n }\r\n \r\n public static void main(String[] args) {\r\n Scanner sc = new Scanner(System.in);\r\n \r\n // Input for length of first array.\r\n int length = sc.nextInt();\r\n \r\n int K = sc.nextInt();\r\n \r\n int[][] updates = new int[K];\r\n \r\n for (int i = 0; i < updates.length; i++) {\r\n for (int j = 0; j < updates.length; j++) {\r\n updates[i][j] = sc.nextInt();\r\n }\r\n }\r\n \r\n int[] result = Range(length, updates);\r\n display(result);\r\n }\r\n \r\n // function to display an array.\r\n public static void display(int[] arr) {\r\n \r\n for (int i = 0; i < arr.length; i++) {\r\n System.out.print(arr[i] + \" \");\r\n }\r\n \r\n System.out.println();\r\n }\r\n \r\n }"},"ruby":{"code":""},"python":{"code":""},"javascript":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"4 \r\n3\r\n0 2 -1\r\n2 2 2\r\n1 3 1","sampleOutput":"-1 0 2 1","questionVideo":"https://www.youtube.com/embed/-SDHYqxI-Hc","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"ed294401-c1be-4c10-a3f7-3e2e4f8d9755","name":"Range Queries For Experts","slug":"range-queries-for-experts-903","type":0},{"id":"2684424c-b1dd-4289-b0a2-3ade526b3874","name":"Range Addition","slug":"range-addition","type":1}],"next":{"id":"708eaf40-ad8a-4b4f-b9f6-8c4276e08d11","name":"Moksh And His Girlfriend","type":1,"slug":"moksh-and-his-girlfriend"},"prev":null}}` Editor

medium

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)

1 <= N <= 30000

## Format

### Input

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

The resultant array. Output is handled for you.

## Example

Sample Input

```.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}4 3 0 2 -1 2 2 2 1 3 1```

### Sample Output

`.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}-1 0 2 1`

Question Video

Discussions

Show Discussion

Related Resources 