Arithmetic Progression Queries Problem Statement
Given an integer array ARR
of size N
, perform the following operations:
- update(l, r, val):
Add (val + i)
to arr[l + i]
for all 0 ≤ i ≤ r - l
.
- rangeSum(l, r):
Return the sum of all elements in the array from index l
to r
, i.e., the sum of arr[l...r]
.
Two types of queries denote these operations:
Type 1: For update(l, r, val)
operation.
Type 2: For rangeSum(l, r)
operation.
Note: Queries use 1-based indexing.
Input:
Two integers N
and Q
representing the size of the array and number of operations.
The next Q
lines contain operations, each starting with an integer representing the type of operation.
If the integer is 1
, it describes a update(l, r, val)
operation followed by integers l
, r
, and val
.
If the integer is 2
, it describes a rangeSum(l, r)
operation followed by integers l
and r
.
Output:
For each operation of type 2, output the sum of arr[l..r]
on a new line.
Example:
Input:
N = 5, Q = 3
Operations:
1 1 3 2
2 1 3
2 2 5
Output:
6
15
Constraints:
1 ≤ N ≤ 10^5
1 ≤ Q ≤ 10^5
1 ≤ l ≤ r ≤ N
0 ≤ val ≤ 10^6
0 ≤ arr[i] ≤ 10^6
Note: The solution handles all input/output; just focus on implementing the function logic.
Be the first one to answer
Add answer anonymously...
Top JPMorgan Chase & Co. SDE-2 interview questions & answers
Popular interview questions of SDE-2
>
JPMorgan Chase & Co. SDE-2 Interview Questions
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app