Skip to content

Commit

Permalink
Updated potd_15_10_2024.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
viraj200524 authored Oct 15, 2024
1 parent 3c51a78 commit 4d6e483
Showing 1 changed file with 43 additions and 44 deletions.
87 changes: 43 additions & 44 deletions october_2024/potd_15_10_2024.cpp
Original file line number Diff line number Diff line change
@@ -1,54 +1,53 @@
/*
Example 1:
Input: arr[] = [10, 2, -2, -20, 10] , tar = -10
Output: 3
Explanation: Subarrays with sum -10 are: [10, 2, -2, -20], [2, -2, -20, 10] and [-20, 10].
// 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.

Example 2:
Input: arr[] = [1, 4, 20, 3, 10, 5] , tar = 33
Output: 1
Explanation: Subarray with sum 33 is: [20,3,10].
*/

#include <iostream>
#include <unordered_map>
#include <vector>

using namespace std;

int subarraySum(vector<int>& arr, int tar) {
// Hash map to store the cumulative sum frequencies
unordered_map<int, int> sum_map;
int current_sum = 0;
int count = 0;

// Initialize sum_map with sum 0 seen once (base case)
sum_map[0] = 1;

for (int num : arr) {
// Update the cumulative sum
current_sum += num;
class Solution {
public:
// Function to calculate the minimum number of steps required
// based on the string 's' containing '0's and '1's.
long long minimumSteps(string s)
{
long long ans = 0; // Variable to store the final answer (minimum steps).

// If the string is empty or has only one character, no steps are needed.
if(s.length() == 0 || s.length() == 1)
return 0;

int r = s.length() - 1; // Set 'r' to the index of the last character in the string.
long long count = 0LL; // 'count' keeps track of the number of '0's encountered.

// Check if there's a subarray (ending here) whose sum is target
if (sum_map.find(current_sum - tar) != sum_map.end()) {
count += sum_map[current_sum - tar];
// Traverse the string from the last character to the first.
while(r >= 0)
{
// If the current character is '1', we add the current count of '0's to 'ans'.
// This is because each '1' should move past all the '0's seen so far.
if(s[r] == '1')
ans += count;
else
// If the current character is '0', increment the 'count' of '0's.
count += 1LL;

r--; // Move to the previous character in the string.
}

// Add the current cumulative sum to the map
sum_map[current_sum]++;
// Return the total number of steps (ans).
return ans;
}

return count;
}
};

// Main function to test the minimumSteps function
int main() {
vector<int> arr = {10, 2, -2, -20, 10};
int tar = -10;

int result = subarraySum(arr, tar);
cout << "Number of subarrays with sum " << tar << ": " << result << endl; // Output: 3

Solution solution;

// Example input string
string s = "1010010001";

// Call the function and store the result
long long result = solution.minimumSteps(s);

// Output the result
cout << "Minimum number of steps required: " << result << endl; // Output should be 5

return 0;
}

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

0 comments on commit 4d6e483

Please sign in to comment.