Skip to content

Commit 57152a5

Browse files
authored
Merge pull request #39 from viraj200524/viraj
Updated potd_15_10_2024.cpp
2 parents 3c51a78 + 4d6e483 commit 57152a5

File tree

1 file changed

+43
-44
lines changed

1 file changed

+43
-44
lines changed

october_2024/potd_15_10_2024.cpp

Lines changed: 43 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,53 @@
1-
/*
2-
Example 1:
3-
Input: arr[] = [10, 2, -2, -20, 10] , tar = -10
4-
Output: 3
5-
Explanation: Subarrays with sum -10 are: [10, 2, -2, -20], [2, -2, -20, 10] and [-20, 10].
1+
// Traversal: We traverse the string from right to left. For every '1' encountered, we add the number of '0's seen so far (in count) to the total ans because each '1' has to move past those '0's.
62

7-
Example 2:
8-
Input: arr[] = [1, 4, 20, 3, 10, 5] , tar = 33
9-
Output: 1
10-
Explanation: Subarray with sum 33 is: [20,3,10].
11-
12-
*/
13-
14-
#include <iostream>
15-
#include <unordered_map>
16-
#include <vector>
17-
18-
using namespace std;
19-
20-
int subarraySum(vector<int>& arr, int tar) {
21-
// Hash map to store the cumulative sum frequencies
22-
unordered_map<int, int> sum_map;
23-
int current_sum = 0;
24-
int count = 0;
25-
26-
// Initialize sum_map with sum 0 seen once (base case)
27-
sum_map[0] = 1;
28-
29-
for (int num : arr) {
30-
// Update the cumulative sum
31-
current_sum += num;
3+
class Solution {
4+
public:
5+
// Function to calculate the minimum number of steps required
6+
// based on the string 's' containing '0's and '1's.
7+
long long minimumSteps(string s)
8+
{
9+
long long ans = 0; // Variable to store the final answer (minimum steps).
10+
11+
// If the string is empty or has only one character, no steps are needed.
12+
if(s.length() == 0 || s.length() == 1)
13+
return 0;
14+
15+
int r = s.length() - 1; // Set 'r' to the index of the last character in the string.
16+
long long count = 0LL; // 'count' keeps track of the number of '0's encountered.
3217

33-
// Check if there's a subarray (ending here) whose sum is target
34-
if (sum_map.find(current_sum - tar) != sum_map.end()) {
35-
count += sum_map[current_sum - tar];
18+
// Traverse the string from the last character to the first.
19+
while(r >= 0)
20+
{
21+
// If the current character is '1', we add the current count of '0's to 'ans'.
22+
// This is because each '1' should move past all the '0's seen so far.
23+
if(s[r] == '1')
24+
ans += count;
25+
else
26+
// If the current character is '0', increment the 'count' of '0's.
27+
count += 1LL;
28+
29+
r--; // Move to the previous character in the string.
3630
}
3731

38-
// Add the current cumulative sum to the map
39-
sum_map[current_sum]++;
32+
// Return the total number of steps (ans).
33+
return ans;
4034
}
41-
42-
return count;
43-
}
35+
};
4436

37+
// Main function to test the minimumSteps function
4538
int main() {
46-
vector<int> arr = {10, 2, -2, -20, 10};
47-
int tar = -10;
48-
49-
int result = subarraySum(arr, tar);
50-
cout << "Number of subarrays with sum " << tar << ": " << result << endl; // Output: 3
51-
39+
Solution solution;
40+
41+
// Example input string
42+
string s = "1010010001";
43+
44+
// Call the function and store the result
45+
long long result = solution.minimumSteps(s);
46+
47+
// Output the result
48+
cout << "Minimum number of steps required: " << result << endl; // Output should be 5
49+
5250
return 0;
5351
}
5452

53+
// Expected Output : Minimum number of steps required: 14

0 commit comments

Comments
 (0)