Skip to content

Commit 795c6b1

Browse files
authored
Merge pull request ByteByteGoHq#34 from ongshunping/cpp-solutions-prefix-sums
Add C++ solutions for Chapter 10 (Prefix Sums)
2 parents b6ad7b3 + 944421c commit 795c6b1

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed

cpp/Prefix Sums/k_sum_subarrays.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <vector>
2+
3+
int kSumSubarrays(std::vector<int>& nums, int k) {
4+
int n = nums.size();
5+
int count = 0;
6+
// Populate the prefix sum array, setting its first element to 0.
7+
std::vector<int> prefixSum;
8+
prefixSum.push_back(0);
9+
for (int i = 0; i < n; i++) {
10+
prefixSum.push_back(prefixSum.back() + nums[i]);
11+
}
12+
// Loop through all valid pairs of prefix sum values to find all
13+
// subarrays that sum to 'k'.
14+
for (int j = 1; j <= n; j++) {
15+
for (int i = 1; i <= j; i++) {
16+
if (prefixSum[j] - prefixSum[i - 1] == k) {
17+
count += 1;
18+
}
19+
}
20+
}
21+
return count;
22+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <vector>
2+
#include <unordered_map>
3+
4+
int kSumSubarraysOptimized(std::vector<int>& nums, int k) {
5+
int count = 0;
6+
// Initialize the map with 0 to handle subarrays that sum to 'k'
7+
// from the start of the array.
8+
std::unordered_map<int, int> prefixSumMap;
9+
prefixSumMap[0] = 1;
10+
int currPrefixSum = 0;
11+
for (int num : nums) {
12+
// Update the running prefix sum by adding the current number.
13+
currPrefixSum += num;
14+
// If a subarray with sum 'k' exists, increment 'count' by the
15+
// number of times it has been found.
16+
if (prefixSumMap.find(currPrefixSum - k) != prefixSumMap.end()) {
17+
count += prefixSumMap[currPrefixSum - k];
18+
}
19+
// Update the frequency of 'currPrefixSum' in the hash map.
20+
int freq = prefixSumMap[currPrefixSum];
21+
prefixSumMap[currPrefixSum] = freq + 1;
22+
}
23+
return count;
24+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <vector>
2+
3+
std::vector<int> productArrayWithoutCurrentElement(std::vector<int>& nums) {
4+
int n = nums.size();
5+
std::vector<int> res(n, 1);
6+
// Populate the output with the running left product.
7+
for (int i = 1; i < n; i++) {
8+
res[i] = res[i - 1] * nums[i - 1];
9+
}
10+
// Multiply the output with the running right product, from right to
11+
// left.
12+
int rightProduct = 1;
13+
for (int i = n - 1; i >= 0; i--) {
14+
res[i] *= rightProduct;
15+
rightProduct *= nums[i];
16+
}
17+
return res;
18+
}

cpp/Prefix Sums/sum_between_range.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <vector>
2+
3+
class SumBetweenRange {
4+
public:
5+
SumBetweenRange(std::vector<int>& nums) {
6+
if (!nums.empty()) {
7+
prefixSum.push_back(nums[0]);
8+
for (int i = 1; i < nums.size(); i++) {
9+
prefixSum.push_back(prefixSum.back() + nums[i]);
10+
}
11+
}
12+
}
13+
14+
int sumRange(int i, int j) {
15+
if (i == 0) {
16+
return prefixSum[j];
17+
}
18+
return prefixSum[j] - prefixSum[i - 1];
19+
}
20+
21+
private:
22+
std::vector<int> prefixSum;
23+
};

0 commit comments

Comments
 (0)