
Asked in JPMorgan Chase & Co.
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.

AnswerBot
4mo
Implement update and rangeSum operations on an integer array based on given queries.
Implement update(l, r, val) by adding (val + i) to arr[l + i] for all i in range (0, r - l).
Implement rangeSum(l, r)...read more
Help your peers!
Add answer anonymously...
Top SDE-2 Interview Questions Asked at JPMorgan Chase & Co.
Q. Design a parking system for a parking lot. The parking lot has three kinds of pa...read more
Q. Given a string s, find the number of substrings without repeating characters.
Q. Debugging a Java Code
Interview Questions Asked to SDE-2 at Other Companies
Top Skill-Based Questions for JPMorgan Chase & Co. SDE-2
Algorithms Interview Questions and Answers
250 Questions
Data Structures Interview Questions and Answers
250 Questions
System Design Interview Questions and Answers
250 Questions
Web Development Interview Questions and Answers
250 Questions
Operating Systems Interview Questions and Answers
250 Questions
Java Interview Questions and Answers
250 Questions
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

