From f2fdf2af71892342d1f8fc418d3d504dbb31ecf8 Mon Sep 17 00:00:00 2001 From: Kdev <120096635+KushagraSahu-01@users.noreply.github.com> Date: Sun, 28 Dec 2025 01:17:53 +0530 Subject: [PATCH] Add solution for range queries using difference array Implemented a solution for processing range update and point query using a difference array. --- .../soln/KushagraSahu/Soution1.cpp | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 algos/range_queries/DifferenceArray/soln/KushagraSahu/Soution1.cpp diff --git a/algos/range_queries/DifferenceArray/soln/KushagraSahu/Soution1.cpp b/algos/range_queries/DifferenceArray/soln/KushagraSahu/Soution1.cpp new file mode 100644 index 0000000..c756205 --- /dev/null +++ b/algos/range_queries/DifferenceArray/soln/KushagraSahu/Soution1.cpp @@ -0,0 +1,56 @@ +/* +Given an array of n integers, your task is to process q queries of the following types: + +increase each value in range [a,b] by u +what is the value at position k? + +Input +The first input line has two integers n and q: the number of values and queries. +The second line has n integers x_1,x_2,\dots,x_n: the array values. +Finally, there are q lines describing the queries. Each line has three integers: either "1 a b u" or "2 k". +Output +Print the result of each query of type 2. + +Approach : we have to update the array multiple times so instead of running +a loop everytime we create a difference array and then take the prefixSum of it when we require the final array +Time Complexity : O(n) for calculating the prefix sum in the type 2 query +Space Complexity : O(n) because of arr and diff arr of size n +*/ +#include +using namespace std; +int main() +{ + int n,q; + cin >>n>>q; + long long int arr[n],diff[n]; + for (int i = 0; i < n; i++) + diff[i] = 0; + for(int i=0;i>arr[i]; + while(q--) + { + int type; + cin >>type; + if(type==1) + { + int a,b; + long long int u; + cin >>a>>b>>u; + a--; + b--; + diff[a]+=u; + if(b+1>k; + k--; + long long sum=0; + for(int i=0;i<=k;i++) + sum+=diff[i]; + cout <