Skip to content

Added code for B/W balls #32:#33

Open
anushka-220 wants to merge 1 commit intogdgpce:mainfrom
anushka-220:add-strings-code
Open

Added code for B/W balls #32:#33
anushka-220 wants to merge 1 commit intogdgpce:mainfrom
anushka-220:add-strings-code

Conversation

@anushka-220
Copy link
Copy Markdown

Fixes #32

Input Taken:
This code reads a binary string s where each character is either '0' (white ball) or '1' (black ball).

Logic used:
It iterates through the length of the string once using a for loop.
The variable 'swaps' stores the minimum required swaps to get black balls on the right and white balls on the left.
Using a variable 'white_balls' , we have kept a track of the number of balls encountered so far.
For each white ball ('0'), the program adds the current count of 'white_balls' to the 'swaps' variable.
For each black ball ('1'), it increments the white_balls counter.

Output:
The program calculates and outputs the minimum number of swaps needed to rearrange the balls.

Copy link
Copy Markdown
Collaborator

@Saket6198 Saket6198 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@anushka-220
in your cpp file itself, write the working of your code/ algorithm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

B/W Balls

2 participants