Enhance Article

Save Article

Like Article

Enhance Article

Save Article

Given a binary string (containing 1’s and 0’s), write a program to rely 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 change should not allowed
  • Use of  addition/subtraction isn’t allowed


Enter : S = “101101”
Output : 2

Enter : S = “00101111000”
Output : 6

Really useful: Please attempt your strategy on {IDE}  first, earlier than transferring on to the answer.

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

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

Steps concerned within the implementation of the strategy:

  • Take a string and a static variable counter which keep the rely of zero.
  • Traverse by way 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 every time we bought the Arithmetic exception, the counter will increment.
  • Print the counter worth because of this.

Beneath is the implementation of the above strategy:


import java.io.*;


class GFG {



    public static int cnt = 0;


    public static void essential(String[] args)



        String s = "01001001110";








    static void countZero(String s)



        for (int i = 0; i < s.size(); i++) {

            attempt {

                int div = Character.getNumericValue(






                int val = 0 / div;




            catch (Exception exception) {









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

Associated Articles:

By admin

Leave a Reply

Your email address will not be published. Required fields are marked *