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
114 changes: 114 additions & 0 deletions algos/range_queries/PrefixSum/soln/Advait/Solution1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
------------------------------------------------------------
LeetCode Problem: 209 - Minimum Size Subarray Sum
Difficulty: Medium
Category: Prefix Sum / Sliding Window
------------------------------------------------------------

Problem Statement:
Given an array of positive integers nums and an integer target,
return the minimal length of a contiguous subarray of which the
sum is greater than or equal to target. If there is no such
subarray, return 0.

------------------------------------------------------------
Approach 1: Prefix Sum + Two Pointers (Explicit Prefix Array)
------------------------------------------------------------

Idea:
- Precompute prefix sums to allow O(1) subarray sum calculation.
- Use two pointers on the prefix array to maintain a sliding window.
- Since all elements are positive, expanding the right pointer
increases the sum and moving the left pointer decreases it.

Subarray Sum Formula:
sum(j..i-1) = prefix[i] - prefix[j]

Time Complexity: O(N)
Space Complexity: O(N)

------------------------------------------------------------
*/

#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
int minSubArrayLen(int target, vector<int>& nums) {
vector<int> a (nums.size()+1);
a[0]=0;
for(int i=0;i<nums.size();i++){
a[i+1]=a[i]+nums[i]; //prefix sum
}
int i=1,j=0;
int minsize=nums.size();
while(i<nums.size()+1){
int currsum=a[i]-a[j];
if(currsum<target){
i++; //update pointer if sum less than target
}
else{
if(minsize>(i-j)){
minsize=i-j;
}
j++; //increase lower pointer to find more possible subarrays
}
}
if(a[nums.size()]>=target){
return minsize; //if total cumulative sum is less than target then target achieving is not possible
}
else {
return 0;
}
}
};
/*
------------------------------------------------------------
Optimized Approach: Sliding Window (Implicit Prefix Sum)
------------------------------------------------------------

Idea:
- Instead of storing the entire prefix sum array, maintain
the current subarray sum directly.
- This is equivalent to prefix sum logic but reduces
extra space usage.

Key Insight:
- Because all elements are positive, the window can safely
move forward without missing optimal solutions.

Time Complexity: O(N)
Space Complexity: O(1)

------------------------------------------------------------
*/

class Solution {
public:
int minSubArrayLen(int target, vector<int>& nums) {
int sum=nums[0];
int minsize=nums.size();
bool flag=false;//set flag for if target is not achievable
int i=0,j=0;
while(i<nums.size()){
if(sum<target){
i++;
if(i<nums.size()){
sum+=nums[i];//increase sum
}
}
else{
flag=true;
if(minsize>(i-j+1)){
minsize=i-j+1;//update minsize if its lesser
}
sum-=nums[j];
j++;//decrease window
}
}
if(flag){
return minsize;
}
return 0;
}
};
133 changes: 133 additions & 0 deletions algos/range_queries/PrefixSum/soln/Advait/Solution2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
------------------------------------------------------------
LeetCode Problem: 560 - Subarray Sum Equals K
Difficulty: Medium
Category: Prefix Sum
------------------------------------------------------------

Problem Statement:
Given an integer array nums and an integer k, return the total
number of continuous subarrays whose sum equals k.

------------------------------------------------------------
Approach: Prefix Sum + Brute Force Enumeration
------------------------------------------------------------

Idea:
- Compute the prefix sum array where prefix[i] stores the sum of
elements from index 0 to i-1.
- Any subarray sum from index i to j-1 can be computed in O(1) as:

prefix[j] - prefix[i]

- Enumerate all possible (i, j) pairs and count how many satisfy
prefix[j] - prefix[i] == k.

Why this works:
- Prefix sums allow constant-time subarray sum calculation.
- Checking all subarrays guarantees correctness even with
negative numbers and zeros.

------------------------------------------------------------
Time Complexity:
- Prefix sum construction: O(N)
- Nested loops over all subarrays: O(N²)
- Total: O(N²)

Space Complexity:
- Prefix sum array: O(N)

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

class Solution {
public:
int subarraySum(vector<int>& nums, int k) {
vector<int> a (nums.size()+1);
a[0]=0;
for(int i=0;i<nums.size();i++){
a[i+1]=a[i]+nums[i];
}
int count=0;
for(int i=0;i<nums.size()+1;i++){
for(int j=i+1;j<nums.size()+1;j++){
if(a[j]-a[i]==k){
count++;
}
}
}
return count;
}
};

/*
------------------------------------------------------------
LeetCode Problem: 560 - Subarray Sum Equals K
Difficulty: Medium
Category: Prefix Sum + HashMap
------------------------------------------------------------

Problem Statement:
Given an integer array nums and an integer k, return the total
number of continuous subarrays whose sum equals k.

------------------------------------------------------------
Approach: Prefix Sum with Frequency HashMap
------------------------------------------------------------

Key Observation:
For a subarray ending at index j with sum equal to k:

prefix[j] - prefix[i] = k
=> prefix[i] = prefix[j] - k

Thus, for each prefix sum encountered, we need to know how many
times (prefixSum - k) has appeared previously.

Algorithm:
1. Traverse the array once while maintaining a running prefix sum.
2. Use a hashmap to store the frequency of prefix sums seen so far.
3. At each index:
- Check if (currentPrefixSum - k) exists in the hashmap.
- If it exists, its frequency contributes to the answer.
4. Update the frequency of the current prefix sum.

Important Detail:
- Initialize the hashmap with {0 : 1} to handle subarrays that
start from index 0.

------------------------------------------------------------
Correctness:
- This method counts all valid subarrays ending at each index.
- Works correctly even with negative numbers and zeros.
- Avoids the need for nested loops by reusing prefix sum history.

------------------------------------------------------------
Time Complexity:
- O(N), where N is the size of the array.

Space Complexity:
- O(N) for storing prefix sum frequencies in the hashmap.

------------------------------------------------------------
*/

class Solution {
public:
int subarraySum(vector<int>& nums, int k) {
unordered_map <int,int> fre;
fre[0]=1;
int sum=0;
int count=0;
for(int x=0;x<nums.size();x++){
sum+=nums[x];
if(fre.find(sum-k)!=fre.end()){
count+=fre[sum-k];
}
fre[sum]++;
}
return count;
}
};