Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions algos/range_queries/PrefixSum/soln/neeru24/solution1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
Problem Statement:
Given an array of integers and multiple queries, each query asks for the
sum of elements in the range [L, R] (0-based indexing).

Approach (Prefix Sum):
We build a prefix sum array where prefix[i] stores the sum of elements
from index 0 to i.
The sum of range [L, R] is:
prefix[R] - prefix[L-1] (if L > 0), else prefix[R].

Time Complexity:
O(n) preprocessing + O(1) per query

Space Complexity:
O(n)

Example:
Input:
5
1 2 3 4 5
3
0 2
1 3
2 4

Output:
6
9
12
*/

#include <bits/stdc++.h>
using namespace std;

int main() {
int n;
cin >> n;

vector<int> arr(n), prefix(n);
for (int i = 0; i < n; i++) {
cin >> arr[i];
}

prefix[0] = arr[0];
for (int i = 1; i < n; i++) {
prefix[i] = prefix[i - 1] + arr[i];
}

int q;
cin >> q;
while (q--) {
int l, r;
cin >> l >> r;
int sum = prefix[r] - (l > 0 ? prefix[l - 1] : 0);
cout << sum << "\n";
}

return 0;
}
54 changes: 54 additions & 0 deletions algos/range_queries/PrefixSum/soln/neeru24/solution2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
Problem Statement:
Given an array of integers and an integer K, find the number of subarrays
whose sum is equal to K.

Approach (Prefix Sum + Hash Map):
We store prefix sums and count how many times (currentPrefix - K)
has appeared before.
This helps us count valid subarrays efficiently.

Time Complexity:
O(n)

Space Complexity:
O(n)

Example:
Input:
5 5
1 2 3 2 5

Output:
2
*/

#include <bits/stdc++.h>
using namespace std;

int main() {
int n, k;
cin >> n >> k;

vector<int> arr(n);
for (int i = 0; i < n; i++) {
cin >> arr[i];
}

unordered_map<int, int> mp;
mp[0] = 1;

int prefixSum = 0, count = 0;
for (int i = 0; i < n; i++) {
prefixSum += arr[i];

if (mp.find(prefixSum - k) != mp.end()) {
count += mp[prefixSum - k];
}

mp[prefixSum]++;
}

cout << count << "\n";
return 0;
}
50 changes: 50 additions & 0 deletions algos/range_queries/PrefixSum/soln/neeru24/solution3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
Problem Statement:
Given an array and an integer K, find the maximum sum of any
subarray of size K.

Approach (Prefix Sum):
Using prefix sums, sum of subarray [i, i+K-1] can be calculated
in O(1). We try all possible windows and keep track of the maximum.

Time Complexity:
O(n)

Space Complexity:
O(n)

Example:
Input:
7 3
2 1 5 1 3 2 1

Output:
9
*/

#include <bits/stdc++.h>
using namespace std;

int main() {
int n, k;
cin >> n >> k;

vector<int> arr(n), prefix(n);
for (int i = 0; i < n; i++) {
cin >> arr[i];
}

prefix[0] = arr[0];
for (int i = 1; i < n; i++) {
prefix[i] = prefix[i - 1] + arr[i];
}

int maxSum = INT_MIN;
for (int i = k - 1; i < n; i++) {
int currSum = prefix[i] - (i >= k ? prefix[i - k] : 0);
maxSum = max(maxSum, currSum);
}

cout << maxSum << "\n";
return 0;
}