HomeSoftware DevelopmentDepend variety of 0's with given situations

# Depend variety of 0’s with given situations

Given a binary string (containing 1’s and 0’s), write a program to depend the variety of 0’s in a given string such that the next situations maintain:

• 1’s and 0’s are in any order in a string
• Use of conditional statements like if, if..else, and swap are usually not allowed
• Use of  addition/subtraction isn’t allowed

Examples:

Enter : S = “101101”
Output : 2

Enter : S = “00101111000”
Output : 6

Strategy: The above drawback will be solved with the beneath concept:

If counter and conditional statements are usually not allowed. We’re having choice of Error dealing with.

Steps concerned within the implementation of the method:

• Take a string and a static variable counter which keep the depend of zero.
• Traverse by means of every character and convert it into an integer.
• Take this quantity and use it as a denominator of the quantity “0”.
• Use the exception dealing with technique attempt..catch in a loop such that at any time when we obtained the Arithmetic exception, the counter will increment.
• Print the counter worth consequently.

Beneath is the implementation of the above method:

## Java

 `import` `java.io.*;` ` `  `class` `GFG {` ` `  `    ` `    ``public` `static` `int` `cnt = ``0``;` ` `  `    ``public` `static` `void` `essential(String[] args)` `    ``{` ` `  `        ``String s = ``"01001001110"``;` ` `  `        ` `        ``countZero(s);` `    ``}` ` `  `    ` `    ` `    ``static` `void` `countZero(String s)` `    ``{` ` `  `        ``for` `(``int` `i = ``0``; i < s.size(); i++) {` `            ``attempt` `{` `                ``int` `div = Character.getNumericValue(` `                    ``s.charAt(i));` `                ` `                ` `                ` `                ` `                ``int` `val = ``0` `/ div;` `            ``}` ` `  `            ` `            ``catch` `(Exception exception) {` `                ``cnt++;` `            ``}` `        ``}` ` `  `        ` `        ``System.out.println(cnt);` `    ``}` `}`

Time Complexity: O(N)
Auxiliary House: O(1)

Associated Articles:

RELATED ARTICLES

### Unstable Type that means in DSA

1. Ashurita

[url=http://diflucanv.com/]where to buy diflucan in uk[/url]

2. Ashurita

[url=http://doxycyclinedsp.online/]doxycycline online cheap[/url]

3. Ashurita

[url=https://alisinopril.online/]buy lisinopril 20 mg online uk[/url]