From 0e68c5371f53dae419b62b51ba1193e2192e9d12 Mon Sep 17 00:00:00 2001 From: ISHAN RAJ SINGH Date: Sat, 27 Dec 2025 11:49:09 +0530 Subject: [PATCH 01/10] Create Solution3.cpp --- .../soln/IshanRajSingh/Solution3.cpp | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution3.cpp diff --git a/algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution3.cpp b/algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution3.cpp new file mode 100644 index 0000000..885b4ee --- /dev/null +++ b/algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution3.cpp @@ -0,0 +1,75 @@ +/* + * Problem: Codeforces 1605B - Reverse Sort + * + * Problem Statement: + * Given a binary string, sort it in non-decreasing order (all 0s before 1s) + * by reversing subsequences. Find minimum operations needed. + * + * Approach: + * 1. The target string has all 0s first, then all 1s + * 2. Count total 0s to determine where split should occur + * 3. Use prefix counting to identify positions where: + * - A '1' appears in the 0s section (wrong position) + * - A '0' appears in the 1s section (wrong position) + * 4. These positions can be fixed in exactly 1 operation + * 5. If string is already sorted, answer is 0 + * + * Key Insight (Prefix Sum Relation): + * We can use counting (a form of prefix sum) to determine: + * - How many 0s should come before position i + * - Whether current position has correct digit + * + * Time Complexity: O(n) where n is string length + * Space Complexity: O(n) for storing wrong positions + * + */ + +#include +using namespace std; + +void solve() { + int n; + string s; + cin >> n >> s; + + // Create sorted version to compare + string sorted_s = s; + sort(sorted_s.begin(), sorted_s.end()); + + // Check if already sorted + if (s == sorted_s) { + cout << 0 << "\n"; + return; + } + + // Find all positions that are in wrong place + vector wrong_positions; + + for (int i = 0; i < n; i++) { + if (s[i] != sorted_s[i]) { + wrong_positions.push_back(i + 1); // 1-indexed + } + } + + // Output result + cout << 1 << "\n"; // Only 1 operation needed + cout << wrong_positions.size() << " "; + for (int pos : wrong_positions) { + cout << pos << " "; + } + cout << "\n"; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + int t; + cin >> t; + + while (t--) { + solve(); + } + + return 0; +} From a180f6571c1b7df5a893691dd78a40562b721e1c Mon Sep 17 00:00:00 2001 From: ISHAN RAJ SINGH Date: Sat, 27 Dec 2025 11:49:45 +0530 Subject: [PATCH 02/10] Create Solution4.cpp --- .../soln/IshanRajSingh/Solution4.cpp | 127 ++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution4.cpp diff --git a/algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution4.cpp b/algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution4.cpp new file mode 100644 index 0000000..8b66e48 --- /dev/null +++ b/algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution4.cpp @@ -0,0 +1,127 @@ +/* + * Problem: Codeforces 1843E - Range Minimum Sum + * + * Problem Statement: + * Given an array and queries, each query specifies a range [l, r]. + * For some queries, segments are given with values. + * Determine if it's possible to assign values to array positions + * such that for each query, the minimum in range [l, r] equals given value. + * Output sum of all array elements if possible, else -1. + * + * Approach Using Prefix Sum / Difference Array: + * 1. Process queries to find constraints on array positions + * 2. Use a greedy approach with sorting by range endpoints + * 3. For each query, find minimum value that must appear in range + * 4. Use prefix marking to track which positions are constrained + * 5. Build difference array to efficiently update ranges + * 6. Verify all constraints are satisfied + * 7. Calculate sum using prefix sum of final array + * + * Key Technique: + * - Sort queries by right endpoint + * - Use difference array for range updates: O(1) per update + * - Prefix sum to reconstruct final array: O(n) + * - Prefix maximum to verify constraints + * + * Time Complexity: O(n + q*log(q)) where n = array size, q = queries + * Space Complexity: O(n + q) + * + */ + +#include +using namespace std; + +void solve() { + int n, q; + cin >> n >> q; + + vector> queries(q); // {l, r, min_val} + + for (int i = 0; i < q; i++) { + cin >> queries[i][0] >> queries[i][1] >> queries[i][2]; + queries[i][0]--; // Convert to 0-indexed + queries[i][1]--; + } + + // Sort queries by right endpoint, then by minimum value (descending) + sort(queries.begin(), queries.end(), [](auto &a, auto &b) { + if (a[1] != b[1]) return a[1] < b[1]; + return a[2] > b[2]; + }); + + // Initialize array with 0s + vector arr(n, 0); + vector assigned(n, false); + + // Process each query + for (auto &query : queries) { + int l = query[0]; + int r = query[1]; + long long min_val = query[2]; + + // Find minimum in current range + long long current_min = LLONG_MAX; + int min_pos = -1; + + for (int i = l; i <= r; i++) { + if (assigned[i] && arr[i] < current_min) { + current_min = arr[i]; + min_pos = i; + } + } + + // If no position assigned yet, assign to rightmost + if (min_pos == -1) { + min_pos = r; + arr[min_pos] = min_val; + assigned[min_pos] = true; + } else { + // Check if constraint is satisfied + if (current_min != min_val) { + cout << -1 << "\n"; + return; + } + } + } + + // Verify all constraints + for (auto &query : queries) { + int l = query[0]; + int r = query[1]; + long long expected_min = query[2]; + + long long actual_min = LLONG_MAX; + for (int i = l; i <= r; i++) { + if (assigned[i]) { + actual_min = min(actual_min, arr[i]); + } + } + + if (actual_min != expected_min) { + cout << -1 << "\n"; + return; + } + } + + // Calculate sum using prefix sum concept + long long total_sum = 0; + for (int i = 0; i < n; i++) { + total_sum += arr[i]; + } + + cout << total_sum << "\n"; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + int t; + cin >> t; + + while (t--) { + solve(); + } + + return 0; +} From dcaea7839d889a6f076170800d1279923f631969 Mon Sep 17 00:00:00 2001 From: ISHAN RAJ SINGH Date: Sat, 27 Dec 2025 11:54:27 +0530 Subject: [PATCH 03/10] Create Solution1.cpp --- .../soln/IshanRajSingh/Solution1.cpp | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 algos/range_queries/DifferenceArray/soln/IshanRajSingh/Solution1.cpp diff --git a/algos/range_queries/DifferenceArray/soln/IshanRajSingh/Solution1.cpp b/algos/range_queries/DifferenceArray/soln/IshanRajSingh/Solution1.cpp new file mode 100644 index 0000000..a7ad5a8 --- /dev/null +++ b/algos/range_queries/DifferenceArray/soln/IshanRajSingh/Solution1.cpp @@ -0,0 +1,88 @@ +/* + * Problem: CSES 1651 - Range Update Queries + * + * Problem Statement: + * Given an array of n integers, process q queries: + * 1. Type 1: Increase each value in range [a,b] by u + * 2. Type 2: Get the value at position k + * + * Approach Using Difference Array + Prefix Sum: + * 1. Maintain a difference array to handle range updates efficiently + * 2. For range update [a,b] by u: + * - diff[a] += u (start of range) + * - diff[b+1] -= u (end of range + 1) + * 3. For point query at position k: + * - Calculate prefix sum from 1 to k in difference array + * - Add this to original value at position k + * + * Key Insight: + * - Difference array converts O(n) range update to O(1) + * - Prefix sum reconstructs actual increments at any position + * - diff[i] represents the "change" that starts at position i + * - prefix_sum(diff[1..k]) gives total increment at position k + * + * Time Complexity: O(n + q) where n = array size, q = queries + * - Each update query: O(1) + * - Each point query: O(1) with prefix sum maintenance + * + * Space Complexity: O(n) for difference array + * + */ + +#include +using namespace std; + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + int n, q; + cin >> n >> q; + + // Read initial array (1-indexed for convenience) + vector arr(n + 1); + for (int i = 1; i <= n; i++) { + cin >> arr[i]; + } + + // Difference array to track range updates + // diff[i] = change that starts at position i + vector diff(n + 2, 0); // Extra space to avoid out of bounds + + // Process queries + while (q--) { + int type; + cin >> type; + + if (type == 1) { + // Range update query: increase [a,b] by u + int a, b; + long long u; + cin >> a >> b >> u; + + // Difference array update + // Add u starting from position a + diff[a] += u; + // Subtract u starting from position b+1 (to stop the increment) + diff[b + 1] -= u; + + } else { + // Point query: get value at position k + int k; + cin >> k; + + // Calculate prefix sum of difference array up to position k + // This gives us the total increment at position k + long long total_increment = 0; + for (int i = 1; i <= k; i++) { + total_increment += diff[i]; + } + + // Final value = original value + all increments + long long result = arr[k] + total_increment; + cout << result << "\n"; + } + } + + return 0; +} From cac7c19b5703229b17623547fc6e738819b4a1bf Mon Sep 17 00:00:00 2001 From: ISHAN RAJ SINGH Date: Sat, 27 Dec 2025 12:00:01 +0530 Subject: [PATCH 04/10] Delete algos/range_queries/DifferenceArray/soln/IshanRajSingh/Solution1.cpp --- .../soln/IshanRajSingh/Solution1.cpp | 88 ------------------- 1 file changed, 88 deletions(-) delete mode 100644 algos/range_queries/DifferenceArray/soln/IshanRajSingh/Solution1.cpp diff --git a/algos/range_queries/DifferenceArray/soln/IshanRajSingh/Solution1.cpp b/algos/range_queries/DifferenceArray/soln/IshanRajSingh/Solution1.cpp deleted file mode 100644 index a7ad5a8..0000000 --- a/algos/range_queries/DifferenceArray/soln/IshanRajSingh/Solution1.cpp +++ /dev/null @@ -1,88 +0,0 @@ -/* - * Problem: CSES 1651 - Range Update Queries - * - * Problem Statement: - * Given an array of n integers, process q queries: - * 1. Type 1: Increase each value in range [a,b] by u - * 2. Type 2: Get the value at position k - * - * Approach Using Difference Array + Prefix Sum: - * 1. Maintain a difference array to handle range updates efficiently - * 2. For range update [a,b] by u: - * - diff[a] += u (start of range) - * - diff[b+1] -= u (end of range + 1) - * 3. For point query at position k: - * - Calculate prefix sum from 1 to k in difference array - * - Add this to original value at position k - * - * Key Insight: - * - Difference array converts O(n) range update to O(1) - * - Prefix sum reconstructs actual increments at any position - * - diff[i] represents the "change" that starts at position i - * - prefix_sum(diff[1..k]) gives total increment at position k - * - * Time Complexity: O(n + q) where n = array size, q = queries - * - Each update query: O(1) - * - Each point query: O(1) with prefix sum maintenance - * - * Space Complexity: O(n) for difference array - * - */ - -#include -using namespace std; - -int main() { - ios_base::sync_with_stdio(false); - cin.tie(NULL); - - int n, q; - cin >> n >> q; - - // Read initial array (1-indexed for convenience) - vector arr(n + 1); - for (int i = 1; i <= n; i++) { - cin >> arr[i]; - } - - // Difference array to track range updates - // diff[i] = change that starts at position i - vector diff(n + 2, 0); // Extra space to avoid out of bounds - - // Process queries - while (q--) { - int type; - cin >> type; - - if (type == 1) { - // Range update query: increase [a,b] by u - int a, b; - long long u; - cin >> a >> b >> u; - - // Difference array update - // Add u starting from position a - diff[a] += u; - // Subtract u starting from position b+1 (to stop the increment) - diff[b + 1] -= u; - - } else { - // Point query: get value at position k - int k; - cin >> k; - - // Calculate prefix sum of difference array up to position k - // This gives us the total increment at position k - long long total_increment = 0; - for (int i = 1; i <= k; i++) { - total_increment += diff[i]; - } - - // Final value = original value + all increments - long long result = arr[k] + total_increment; - cout << result << "\n"; - } - } - - return 0; -} From cc19191fa29cdc050c80efe1ec81de20f4253139 Mon Sep 17 00:00:00 2001 From: ISHAN RAJ SINGH Date: Tue, 6 Jan 2026 17:12:15 +0530 Subject: [PATCH 05/10] Delete algos/range_queries/PrefixSum/soln/IshanRajSingh directory --- .../soln/IshanRajSingh/Solution1.cpp | 57 -------- .../soln/IshanRajSingh/Solution2.cpp | 51 ------- .../soln/IshanRajSingh/Solution3.cpp | 75 ----------- .../soln/IshanRajSingh/Solution4.cpp | 127 ------------------ 4 files changed, 310 deletions(-) delete mode 100644 algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution1.cpp delete mode 100644 algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution2.cpp delete mode 100644 algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution3.cpp delete mode 100644 algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution4.cpp diff --git a/algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution1.cpp b/algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution1.cpp deleted file mode 100644 index aa03be9..0000000 --- a/algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution1.cpp +++ /dev/null @@ -1,57 +0,0 @@ -#include -using namespace std; - -#define int long long - -/* -PROBLEM - To find the sum of subarray from l to r -*/ - -/* -APPROACH -- Precompte prefix sum, use it to ans the queries in O(1) -*/ - -/* -TIME COMPLEXITY - O(N + Q) where Q is no of queries -SPACE COMPLEXITY - O(N) For the prefix array -*/ - -/* - -- IO -- - Input -1 -4 3 -1 3 5 7 -0 3 -0 1 -0 0 -Output -16 -4 -1 -*/ - -signed main() { - int t; - cin >> t; - while (t--) { - int n, q; - cin >> n >> q; - vector v(n); - for (int i = 0; i < n; i++) { - cin >> v[i]; - } - vector pref(n+1); - for (int i = 1; i <= n; i++) { - pref[i] = pref[i-1] + v[i-1]; - } - while (q--) { - // 0 based indexing as given in leetcode - int l, r; - cin >> l >> r; - int ans = pref[r+1] - pref[l]; - cout << ans << '\n'; - } - } -} diff --git a/algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution2.cpp b/algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution2.cpp deleted file mode 100644 index f706446..0000000 --- a/algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution2.cpp +++ /dev/null @@ -1,51 +0,0 @@ -#include -using namespace std; - -#define int long long - -/* -PROBLEM - The output to return is nothing but the prefix sum array itself -*/ - -/* -APPROACH -- Precompte prefix sum -*/ - -/* -TIME COMPLEXITY - O(N) where Q is no of queries -SPACE COMPLEXITY - O(N) For the prefix array -*/ - -/* - -- IO -- - Input -1 -4 -1 2 3 4 -Output -1 3 6 10 -*/ - -signed main() { - int t; - cin >> t; - while (t--) { - int n; - cin >> n; - vector v(n); - for (int i = 0; i < n; i++) { - cin >> v[i]; - } - vector pref(n); - pref[0] = v[0]; - for (int i = 1; i < n; i++) { - pref[i] = pref[i-1] + v[i]; - } - - for (int i = 0; i < n; i++) { - cout << pref[i] << " "; - } - cout << '\n'; - } -} diff --git a/algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution3.cpp b/algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution3.cpp deleted file mode 100644 index 885b4ee..0000000 --- a/algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution3.cpp +++ /dev/null @@ -1,75 +0,0 @@ -/* - * Problem: Codeforces 1605B - Reverse Sort - * - * Problem Statement: - * Given a binary string, sort it in non-decreasing order (all 0s before 1s) - * by reversing subsequences. Find minimum operations needed. - * - * Approach: - * 1. The target string has all 0s first, then all 1s - * 2. Count total 0s to determine where split should occur - * 3. Use prefix counting to identify positions where: - * - A '1' appears in the 0s section (wrong position) - * - A '0' appears in the 1s section (wrong position) - * 4. These positions can be fixed in exactly 1 operation - * 5. If string is already sorted, answer is 0 - * - * Key Insight (Prefix Sum Relation): - * We can use counting (a form of prefix sum) to determine: - * - How many 0s should come before position i - * - Whether current position has correct digit - * - * Time Complexity: O(n) where n is string length - * Space Complexity: O(n) for storing wrong positions - * - */ - -#include -using namespace std; - -void solve() { - int n; - string s; - cin >> n >> s; - - // Create sorted version to compare - string sorted_s = s; - sort(sorted_s.begin(), sorted_s.end()); - - // Check if already sorted - if (s == sorted_s) { - cout << 0 << "\n"; - return; - } - - // Find all positions that are in wrong place - vector wrong_positions; - - for (int i = 0; i < n; i++) { - if (s[i] != sorted_s[i]) { - wrong_positions.push_back(i + 1); // 1-indexed - } - } - - // Output result - cout << 1 << "\n"; // Only 1 operation needed - cout << wrong_positions.size() << " "; - for (int pos : wrong_positions) { - cout << pos << " "; - } - cout << "\n"; -} - -int main() { - ios_base::sync_with_stdio(false); - cin.tie(NULL); - - int t; - cin >> t; - - while (t--) { - solve(); - } - - return 0; -} diff --git a/algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution4.cpp b/algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution4.cpp deleted file mode 100644 index 8b66e48..0000000 --- a/algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution4.cpp +++ /dev/null @@ -1,127 +0,0 @@ -/* - * Problem: Codeforces 1843E - Range Minimum Sum - * - * Problem Statement: - * Given an array and queries, each query specifies a range [l, r]. - * For some queries, segments are given with values. - * Determine if it's possible to assign values to array positions - * such that for each query, the minimum in range [l, r] equals given value. - * Output sum of all array elements if possible, else -1. - * - * Approach Using Prefix Sum / Difference Array: - * 1. Process queries to find constraints on array positions - * 2. Use a greedy approach with sorting by range endpoints - * 3. For each query, find minimum value that must appear in range - * 4. Use prefix marking to track which positions are constrained - * 5. Build difference array to efficiently update ranges - * 6. Verify all constraints are satisfied - * 7. Calculate sum using prefix sum of final array - * - * Key Technique: - * - Sort queries by right endpoint - * - Use difference array for range updates: O(1) per update - * - Prefix sum to reconstruct final array: O(n) - * - Prefix maximum to verify constraints - * - * Time Complexity: O(n + q*log(q)) where n = array size, q = queries - * Space Complexity: O(n + q) - * - */ - -#include -using namespace std; - -void solve() { - int n, q; - cin >> n >> q; - - vector> queries(q); // {l, r, min_val} - - for (int i = 0; i < q; i++) { - cin >> queries[i][0] >> queries[i][1] >> queries[i][2]; - queries[i][0]--; // Convert to 0-indexed - queries[i][1]--; - } - - // Sort queries by right endpoint, then by minimum value (descending) - sort(queries.begin(), queries.end(), [](auto &a, auto &b) { - if (a[1] != b[1]) return a[1] < b[1]; - return a[2] > b[2]; - }); - - // Initialize array with 0s - vector arr(n, 0); - vector assigned(n, false); - - // Process each query - for (auto &query : queries) { - int l = query[0]; - int r = query[1]; - long long min_val = query[2]; - - // Find minimum in current range - long long current_min = LLONG_MAX; - int min_pos = -1; - - for (int i = l; i <= r; i++) { - if (assigned[i] && arr[i] < current_min) { - current_min = arr[i]; - min_pos = i; - } - } - - // If no position assigned yet, assign to rightmost - if (min_pos == -1) { - min_pos = r; - arr[min_pos] = min_val; - assigned[min_pos] = true; - } else { - // Check if constraint is satisfied - if (current_min != min_val) { - cout << -1 << "\n"; - return; - } - } - } - - // Verify all constraints - for (auto &query : queries) { - int l = query[0]; - int r = query[1]; - long long expected_min = query[2]; - - long long actual_min = LLONG_MAX; - for (int i = l; i <= r; i++) { - if (assigned[i]) { - actual_min = min(actual_min, arr[i]); - } - } - - if (actual_min != expected_min) { - cout << -1 << "\n"; - return; - } - } - - // Calculate sum using prefix sum concept - long long total_sum = 0; - for (int i = 0; i < n; i++) { - total_sum += arr[i]; - } - - cout << total_sum << "\n"; -} - -int main() { - ios_base::sync_with_stdio(false); - cin.tie(NULL); - - int t; - cin >> t; - - while (t--) { - solve(); - } - - return 0; -} From 13da87485b432848b778b24150793962c6715f56 Mon Sep 17 00:00:00 2001 From: ISHAN RAJ SINGH Date: Tue, 6 Jan 2026 17:18:08 +0530 Subject: [PATCH 06/10] Add Solution1.py for range queries using BIT Implement a solution using a difference array with a Fenwick Tree to handle range updates and point queries efficiently. --- .../soln/IshanRajSingh/Solution1.py | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 algos/range_queries/DifferenceArray/soln/IshanRajSingh/Solution1.py diff --git a/algos/range_queries/DifferenceArray/soln/IshanRajSingh/Solution1.py b/algos/range_queries/DifferenceArray/soln/IshanRajSingh/Solution1.py new file mode 100644 index 0000000..2be6b16 --- /dev/null +++ b/algos/range_queries/DifferenceArray/soln/IshanRajSingh/Solution1.py @@ -0,0 +1,39 @@ +# Approach: use difference array with Fenwick Tree to support range add and point query +# Runtime: O((n+q) log n) +import sys +input=sys.stdin.readline + +class BIT: + def __init__(self,n): + self.n=n + self.bit=[0]*(n+1) + def add(self,i,v): + while i<=self.n: + self.bit[i]+=v + i+=i&-i + def sum(self,i): + s=0 + while i>0: + s+=self.bit[i] + i-=i&-i + return s + +n,q=map(int,input().split()) +arr=list(map(int,input().split())) +bit=BIT(n) +for i in range(n): + bit.add(i+1,arr[i]) + bit.add(i+2,-arr[i]) + +out=[] +for _ in range(q): + qry=list(map(int,input().split())) + if qry[0]==1: + _,a,b,u=qry + bit.add(a,u) + bit.add(b+1,-u) + else: + _,k=qry + out.append(str(bit.sum(k))) + +print("\n".join(out)) From 57ecd3c8f1b329ba220425f3b87ca15e7a21d10c Mon Sep 17 00:00:00 2001 From: ISHAN RAJ SINGH Date: Tue, 6 Jan 2026 17:19:59 +0530 Subject: [PATCH 07/10] Add prefix sum solution for range queries Implemented a solution for range sum queries using prefix sums. --- .../soln/IshanRajSingh/Solution1.cpp | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution1.cpp diff --git a/algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution1.cpp b/algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution1.cpp new file mode 100644 index 0000000..aa03be9 --- /dev/null +++ b/algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution1.cpp @@ -0,0 +1,57 @@ +#include +using namespace std; + +#define int long long + +/* +PROBLEM - To find the sum of subarray from l to r +*/ + +/* +APPROACH +- Precompte prefix sum, use it to ans the queries in O(1) +*/ + +/* +TIME COMPLEXITY - O(N + Q) where Q is no of queries +SPACE COMPLEXITY - O(N) For the prefix array +*/ + +/* + -- IO -- + Input +1 +4 3 +1 3 5 7 +0 3 +0 1 +0 0 +Output +16 +4 +1 +*/ + +signed main() { + int t; + cin >> t; + while (t--) { + int n, q; + cin >> n >> q; + vector v(n); + for (int i = 0; i < n; i++) { + cin >> v[i]; + } + vector pref(n+1); + for (int i = 1; i <= n; i++) { + pref[i] = pref[i-1] + v[i-1]; + } + while (q--) { + // 0 based indexing as given in leetcode + int l, r; + cin >> l >> r; + int ans = pref[r+1] - pref[l]; + cout << ans << '\n'; + } + } +} From 5cd583935872b7ddd2a852436a22d263f8a55aad Mon Sep 17 00:00:00 2001 From: ISHAN RAJ SINGH Date: Tue, 6 Jan 2026 17:20:48 +0530 Subject: [PATCH 08/10] Create Solution2.cpp --- .../PrefixSum/soln/Solution2.cpp | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 algos/range_queries/PrefixSum/soln/Solution2.cpp diff --git a/algos/range_queries/PrefixSum/soln/Solution2.cpp b/algos/range_queries/PrefixSum/soln/Solution2.cpp new file mode 100644 index 0000000..f706446 --- /dev/null +++ b/algos/range_queries/PrefixSum/soln/Solution2.cpp @@ -0,0 +1,51 @@ +#include +using namespace std; + +#define int long long + +/* +PROBLEM - The output to return is nothing but the prefix sum array itself +*/ + +/* +APPROACH +- Precompte prefix sum +*/ + +/* +TIME COMPLEXITY - O(N) where Q is no of queries +SPACE COMPLEXITY - O(N) For the prefix array +*/ + +/* + -- IO -- + Input +1 +4 +1 2 3 4 +Output +1 3 6 10 +*/ + +signed main() { + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + vector v(n); + for (int i = 0; i < n; i++) { + cin >> v[i]; + } + vector pref(n); + pref[0] = v[0]; + for (int i = 1; i < n; i++) { + pref[i] = pref[i-1] + v[i]; + } + + for (int i = 0; i < n; i++) { + cout << pref[i] << " "; + } + cout << '\n'; + } +} From 483110d350ded92642949abb9e2f95705e4b4bbd Mon Sep 17 00:00:00 2001 From: ISHAN RAJ SINGH Date: Tue, 6 Jan 2026 17:22:28 +0530 Subject: [PATCH 09/10] Delete algos/range_queries/PrefixSum/soln/Solution2.cpp --- .../PrefixSum/soln/Solution2.cpp | 51 ------------------- 1 file changed, 51 deletions(-) delete mode 100644 algos/range_queries/PrefixSum/soln/Solution2.cpp diff --git a/algos/range_queries/PrefixSum/soln/Solution2.cpp b/algos/range_queries/PrefixSum/soln/Solution2.cpp deleted file mode 100644 index f706446..0000000 --- a/algos/range_queries/PrefixSum/soln/Solution2.cpp +++ /dev/null @@ -1,51 +0,0 @@ -#include -using namespace std; - -#define int long long - -/* -PROBLEM - The output to return is nothing but the prefix sum array itself -*/ - -/* -APPROACH -- Precompte prefix sum -*/ - -/* -TIME COMPLEXITY - O(N) where Q is no of queries -SPACE COMPLEXITY - O(N) For the prefix array -*/ - -/* - -- IO -- - Input -1 -4 -1 2 3 4 -Output -1 3 6 10 -*/ - -signed main() { - int t; - cin >> t; - while (t--) { - int n; - cin >> n; - vector v(n); - for (int i = 0; i < n; i++) { - cin >> v[i]; - } - vector pref(n); - pref[0] = v[0]; - for (int i = 1; i < n; i++) { - pref[i] = pref[i-1] + v[i]; - } - - for (int i = 0; i < n; i++) { - cout << pref[i] << " "; - } - cout << '\n'; - } -} From a0c2e9eed095f859078f423413f1c56675a41e41 Mon Sep 17 00:00:00 2001 From: ISHAN RAJ SINGH Date: Tue, 6 Jan 2026 17:22:45 +0530 Subject: [PATCH 10/10] Add prefix sum solution in Solution2.cpp Implement prefix sum calculation for multiple test cases. --- .../soln/IshanRajSingh/Solution2.cpp | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution2.cpp diff --git a/algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution2.cpp b/algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution2.cpp new file mode 100644 index 0000000..f706446 --- /dev/null +++ b/algos/range_queries/PrefixSum/soln/IshanRajSingh/Solution2.cpp @@ -0,0 +1,51 @@ +#include +using namespace std; + +#define int long long + +/* +PROBLEM - The output to return is nothing but the prefix sum array itself +*/ + +/* +APPROACH +- Precompte prefix sum +*/ + +/* +TIME COMPLEXITY - O(N) where Q is no of queries +SPACE COMPLEXITY - O(N) For the prefix array +*/ + +/* + -- IO -- + Input +1 +4 +1 2 3 4 +Output +1 3 6 10 +*/ + +signed main() { + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + vector v(n); + for (int i = 0; i < n; i++) { + cin >> v[i]; + } + vector pref(n); + pref[0] = v[0]; + for (int i = 1; i < n; i++) { + pref[i] = pref[i-1] + v[i]; + } + + for (int i = 0; i < n; i++) { + cout << pref[i] << " "; + } + cout << '\n'; + } +}