diff --git a/algos/range_queries/PrefixSum/soln/neeru24/solution1.cpp b/algos/range_queries/PrefixSum/soln/neeru24/solution1.cpp new file mode 100644 index 0000000..42f1ea8 --- /dev/null +++ b/algos/range_queries/PrefixSum/soln/neeru24/solution1.cpp @@ -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 +using namespace std; + +int main() { + int n; + cin >> n; + + vector 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; +} diff --git a/algos/range_queries/PrefixSum/soln/neeru24/solution2.cpp b/algos/range_queries/PrefixSum/soln/neeru24/solution2.cpp new file mode 100644 index 0000000..2aefd4d --- /dev/null +++ b/algos/range_queries/PrefixSum/soln/neeru24/solution2.cpp @@ -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 +using namespace std; + +int main() { + int n, k; + cin >> n >> k; + + vector arr(n); + for (int i = 0; i < n; i++) { + cin >> arr[i]; + } + + unordered_map 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; +} diff --git a/algos/range_queries/PrefixSum/soln/neeru24/solution3.cpp b/algos/range_queries/PrefixSum/soln/neeru24/solution3.cpp new file mode 100644 index 0000000..6f0bac9 --- /dev/null +++ b/algos/range_queries/PrefixSum/soln/neeru24/solution3.cpp @@ -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 +using namespace std; + +int main() { + int n, k; + cin >> n >> k; + + vector 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; +}