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!
Select
Add answer anonymously...

JPMorgan Chase & Co. SDE-2 interview questions & answers

A SDE-2 was asked 10mo agoQ. Design a parking system for a parking lot. The parking lot has three kinds of pa...read more
A SDE-2 was asked 10mo agoQ. Given a string s, find the number of substrings without repeating characters.
A SDE-2 was asked 10mo agoQ. Debugging a Java Code

Popular interview questions of SDE-2

A SDE-2 was asked 10mo agoQ1. Design a parking system for a parking lot. The parking lot has three kinds of pa...read more
A SDE-2 was asked 10mo agoQ2. Given a string s, find the number of substrings without repeating characters.
A SDE-2 was asked 10mo agoQ3. Debugging a Java Code
JPMorgan Chase & Co. SDE-2 Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
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

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits