Fenwick Tree Problem Statement
You are provided with an array/list ARR
consisting of 'N' integers, along with 'Q' queries. Each query can be of one of the following two types:
- Type 1 (Range Sum): Given two integers 'L' and 'R' (0 ≤ L ≤ R < N), compute the sum of all elements in the array with indices in the range ['L', 'R'] inclusive.
- Type 2 (Point Update): Given an index 'i' and a value 'X', update the value of
ARR[i]
to 'X'.
Your task is to execute these queries on the given array and return the required outputs for each query.
Input:
The first line contains an integer 'T', representing the number of test cases.
The first line of each test case contains a single integer 'N', indicating the number of elements in the array.
The second line of each test case contains 'N' space-separated integers representing the array 'ARR'.
The third line of each test case contains an integer 'Q', denoting the number of queries.
The next 'Q' lines of each test case contain three space-separated integers, which are:
1. The first integer indicating the type of query, either '1' for a range sum query or '2' for an update query.
2. For query type '1', the second and third integers are 'L' and 'R', representing the range for the sum calculation.
3. For query type '2', the second integer is 'i', the index to update, and the third integer is 'X', the new value at index 'i'.
Output:
For each query of type 1, output the sum of the array values with indices in the range 'L' and 'R'.
Do not output anything for query type 2; simply perform the update on the array.
Example:
Input:
1
5
3 2 1 4 5
3
1 1 3
2 2 6
1 1 3
Output:
7
9
Constraints:
- 1 ≤ T ≤ 10
- 1 ≤ N ≤ 105
- 1 ≤ Q ≤ 105
- -109 ≤ ARR[i] ≤ 109
Where 'N' denotes the length of the array ARR
, 'Q' denotes the number of queries, and ARR[i]
is the value of the element at index 'i'.
Time Limit: 1 sec

AnswerBot
4mo
Implement Fenwick Tree to handle range sum and point update queries on an array.
Implement Fenwick Tree data structure to efficiently handle range sum and point update queries.
For range sum queries, us...read more
UREVENUKA SHIVAJI
10mo
-1
Vignesh N
2y
I want this answer immediately
Add answer anonymously...
Amazon Software Developer interview questions & answers
A Software Developer was asked 1mo agoQ. What is HTML?
A Software Developer was asked 1mo agoQ. What is MySQL?
A Software Developer was asked 2mo agoQ. Given two strings s and t, return true if they are equal when both are typed int...read more
Popular interview questions of Software Developer
A Software Developer was asked 1mo agoQ1. What is HTML?
A Software Developer was asked 1mo agoQ2. What is MySQL?
A Software Developer was asked 2mo agoQ3. What is the system design for the cart feature in an e-commerce website?
Top HR questions asked in Amazon Software Developer
A Software Developer was asked 1mo agoQ1. Tell me about a time you had to get to the root cause of a problem
A Software Developer was asked 5mo agoQ2. What are the short-term and long-term goals for the team or organization?
A Software Developer was asked 5mo agoQ3. Why do you want to work at Amazon?
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

