From 4d6e48359c26431fa91a7ed75c09c0119b8790c4 Mon Sep 17 00:00:00 2001 From: VIRAJ <124988569+viraj200524@users.noreply.github.com> Date: Tue, 15 Oct 2024 21:06:12 +0530 Subject: [PATCH] Updated potd_15_10_2024.cpp --- october_2024/potd_15_10_2024.cpp | 87 ++++++++++++++++---------------- 1 file changed, 43 insertions(+), 44 deletions(-) diff --git a/october_2024/potd_15_10_2024.cpp b/october_2024/potd_15_10_2024.cpp index 554a21a..caf52d7 100644 --- a/october_2024/potd_15_10_2024.cpp +++ b/october_2024/potd_15_10_2024.cpp @@ -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 -#include -#include - -using namespace std; - -int subarraySum(vector& arr, int tar) { - // Hash map to store the cumulative sum frequencies - unordered_map 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 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