-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3c51a78
commit 4d6e483
Showing
1 changed file
with
43 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |