-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate Black and White Balls Problem and sol.
- Loading branch information
1 parent
5b47395
commit 91ee3ce
Showing
2 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
Beginner Level π/Separate Black and White Balls/Solution.cpp
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#include <iostream> | ||
#include <string> | ||
using namespace std; | ||
|
||
class Solution { | ||
public: | ||
long long minimumSteps(string s) { | ||
int n = s.length(); | ||
int ones = 0, zeros = 0; | ||
long long count = 0; | ||
for (int i = n - 1; i >= 0; --i) { | ||
if (s[i] == '0') { | ||
// Count how many ones are after this zero | ||
zeros++; | ||
} else { | ||
// When encountering '1', we count it to match future '0's | ||
count += zeros; | ||
ones++; | ||
} | ||
} | ||
return count; | ||
} | ||
}; | ||
|
||
int main() { | ||
Solution solution; | ||
string s; | ||
|
||
// Taking input string from user | ||
cout << "Enter a binary string (consisting of 0's and 1's): "; | ||
cin >> s; | ||
|
||
// Calculating the minimum steps | ||
long long result = solution.minimumSteps(s); | ||
|
||
// Output the result | ||
cout << "Minimum steps to rearrange the string: " << result << endl; | ||
|
||
return 0; | ||
} |
37 changes: 37 additions & 0 deletions
37
Beginner Level π/Separate Black and White Balls/question.md
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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
### Separate Black and White Balls | ||
|
||
There are n balls on a table, each ball has a color black or white. | ||
You are given a 0-indexed binary string s of length n, where 1 and 0 represent black and white balls, respectively. | ||
In each step, you can choose two adjacent balls and swap them. | ||
Return the minimum number of steps to group all the black balls to the right and all the white balls to the left. | ||
|
||
## Example 1: | ||
|
||
Input: s = "101" | ||
Output: 1 | ||
Explanation: We can group all the black balls to the right in the following way: | ||
- Swap s[0] and s[1], s = "011". | ||
Initially, 1s are not grouped together, requiring at least 1 step to group them to the right. | ||
|
||
## Example 2: | ||
|
||
Input: s = "100" | ||
Output: 2 | ||
Explanation: We can group all the black balls to the right in the following way: | ||
- Swap s[0] and s[1], s = "010". | ||
- Swap s[1] and s[2], s = "001". | ||
It can be proven that the minimum number of steps needed is 2. | ||
|
||
## Example 3: | ||
|
||
Input: s = "0111" | ||
Output: 0 | ||
Explanation: All the black balls are already grouped to the right. | ||
|
||
|
||
|
||
## Constraints: | ||
|
||
- 1 <= n == s.length <= 105 | ||
- s[i] is either '0' or '1'. | ||
|