diff --git a/algos/range_queries/PrefixSum/soln/Advait/Solution1.cpp b/algos/range_queries/PrefixSum/soln/Advait/Solution1.cpp new file mode 100644 index 0000000..6aa297d --- /dev/null +++ b/algos/range_queries/PrefixSum/soln/Advait/Solution1.cpp @@ -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 +using namespace std; +class Solution { + public: + int minSubArrayLen(int target, vector& nums) { + vector a (nums.size()+1); + a[0]=0; + for(int i=0;i(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& 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(i-j+1)){ + minsize=i-j+1;//update minsize if its lesser + } + sum-=nums[j]; + j++;//decrease window + } + } + if(flag){ + return minsize; + } + return 0; + } +}; diff --git a/algos/range_queries/PrefixSum/soln/Advait/Solution2.cpp b/algos/range_queries/PrefixSum/soln/Advait/Solution2.cpp new file mode 100644 index 0000000..e6e64ca --- /dev/null +++ b/algos/range_queries/PrefixSum/soln/Advait/Solution2.cpp @@ -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 +using namespace std; + +class Solution { +public: + int subarraySum(vector& nums, int k) { + vector a (nums.size()+1); + a[0]=0; + for(int i=0;i 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& nums, int k) { + unordered_map fre; + fre[0]=1; + int sum=0; + int count=0; + for(int x=0;x