Sub arrays Flashcards
1
Q
Maximum sum in a subarray
A
- Can use dp or currSum
- dp[i] = currNo + dp[i - 1]
- maxSum = max(maxSum, dp[i])
2
Q
Maximum Size Subarray Sum Equals k
A
- HashMap
- map.put(currSum, j)
- currSum = k, maxLen = j + 1
- map.contains(currSum - k) i = currSum - k
maxLen = max(maxLen, j - i)
3
Q
Count of Subarray Sum Equals K
A
- HashMap
- map.contains(currSum - k)
count += map.get(currSum - k) - map.put(currSum, map.getOrDefault(currSum, 0) + 1)
4
Q
Maximum Product Subarray
A
- maintain min and max
- find the max = max(currNo, max(currSum * max, currSum * min))
- similarly for min
- maxProduct = max(maxProduct, max)