Count number of 0’s with given conditions
Given a binary string (containing 1’s and 0’s), write a program to count the number of 0’s in a given string such that the following conditions hold:
- 1’s and 0’s are in any order in a string
- Use of conditional statements like if, if..else, and switch are not allowed
- Use of addition/subtraction is not allowed
Examples:
Input : S = “101101”
Output : 2Input : S = “00101111000”
Output : 6
Recommended: Please try your approach on {IDE} first, before moving on to the solution.
Approach: The above problem can be solved with the below idea:
If counter and conditional statements are not allowed. We are having option of Error handling.
Steps involved in the implementation of the approach:
- Take a string and a static variable counter which maintain the count of zero.
- Traverse through each character and convert it into an integer.
- Take this number and use it as a denominator of the number “0”.
- Use the exception handling method try..catch in a loop such that whenever we got the Arithmetic exception, the counter will increment.
- Print the counter value as a result.
Below is the implementation of the above approach:
C++
#include <iostream> #include <string> using namespace std; // Counter for count the zero's int cnt = 0; // Function to calculate the number // of 0's void countZero(string s) { for ( int i = 0; i < s.length(); i++) { int div = s[i] - '0' ; // If we do 0/0 it gives // exception where we cnt // the number of 0's // and other are 1's if ( div == 0) cnt++; } // Return the count of 0's cout << cnt << endl; } int main() { string s = "101101" ; // Function call countZero(s); } |
Java
// Java implementation of the code import java.io.*; class GFG { // Counter for count the zero's public static int cnt = 0 ; public static void main(String[] args) { String s = "101101" ; // Function call countZero(s); } // Function to calculate the number // of 0's static void countZero(String s) { for ( int i = 0 ; i < s.length(); i++) { try { int div = Character.getNumericValue( s.charAt(i)); // If we do 0/0 it gives // exception where we cnt // the number of 0's // and other are 1's int val = 0 / div; } // O is found catch (Exception exception) { cnt++; } } // Return the count of 0's System.out.println(cnt); } } |
Python3
cnt = 0 def count_zero(s: str ): global cnt for i in range ( len (s)): div = int (s[i]) if div = = 0 : cnt + = 1 print (cnt) s = "101101" count_zero(s) |
C#
using System; class Program { // Counter for count the zero's static int cnt = 0; // Function to calculate the number // of 0's static void CountZero( string s) { for ( int i = 0; i < s.Length; i++) { int div = s[i] - '0' ; // If we do 0/0 it gives // exception where we cnt // the number of 0's // and other are 1's if (div == 0) cnt++; } // Return the count of 0's Console.WriteLine(cnt); } static void Main( string [] args) { string s = "101101" ; // Function call CountZero(s); } } |
Javascript
// Counter for count the zero's let cnt = 0; // Function to calculate the number // of 0's function countZero(s) { for (let i = 0; i < s.length; i++) { if (s[i] === '0' ) { cnt++; } } // Return the count of 0's console.log(cnt); } let s = "101101" ; // Function call countZero(s); |
Output
2
Time Complexity: O(N)
Auxiliary Space: O(1)
An approach using XOR operation:
Follow the steps below to implement:
- Iterate over the string.
- The character 0 is represented as 48 in ASCII, so we can use XOR operation with 48 to check if the current character is 0.
- If the character is 0, then XOR with 48 will result in 0, which will be added to the count variable.
- If the character is 1, then XOR with 48 will result in 1, which will not affect the count variable.
- Finally, the number of zeros in the binary string is n – count, which is printed as the output.
C++
#include <bits/stdc++.h> using namespace std; // Function to calculate the number // of 0's void countZero(string binary_string) { int n = binary_string.length(); int count = 0; for ( int i = 0; i < n; i++) { count += binary_string[i] ^ 48; } cout << n - count << endl; } int main() { string s = "101101" ; // Function call countZero(s); } |
Java
import java.util.*; public class CountZero { public static void countZero(String binaryString) { int n = binaryString.length(); int count = 0 ; for ( int i = 0 ; i < n; i++) { count += binaryString.charAt(i) ^ 48 ; } System.out.println(n - count); } public static void main(String[] args) { String s = "101101" ; countZero(s); } } |
C#
using System; public class Program { static void countZero( string binaryString) { int n = binaryString.Length; int count = 0; for ( int i = 0; i < n; i++) { count += binaryString[i] ^ 48; } Console.WriteLine(n - count); } static void Main( string [] args) { string s = "101101" ; countZero(s); } } |
Python3
def countZero(binary_string): n = len (binary_string) count = 0 for i in range (n): count + = ord (binary_string[i]) ^ 48 print (n - count) s = "101101" countZero(s) |
Javascript
function countZero(binaryString) { const n = binaryString.length; let count = 0; for (let i = 0; i < n; i++) { count += binaryString.charCodeAt(i) ^ 48; } console.log(n - count); } const s = "101101" ; countZero(s); |
Output
2
Time Complexity: O(N)
Auxiliary Space: O(1)
Related Articles:
Please Login to comment...