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 : 2
Input : 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;
int cnt = 0;
void countZero(string s)
{
for ( int i = 0; i < s.length(); i++) {
int div = s[i] - '0' ;
if ( div == 0)
cnt++;
}
cout << cnt << endl;
}
int main()
{
string s = "101101" ;
countZero(s);
}
|
Java
import java.io.*;
class GFG {
public static int cnt = 0 ;
public static void main(String[] args)
{
String s = "101101" ;
countZero(s);
}
static void countZero(String s)
{
for ( int i = 0 ; i < s.length(); i++) {
try {
int div = Character.getNumericValue(
s.charAt(i));
int val = 0 / div;
}
catch (Exception exception) {
cnt++;
}
}
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
{
static int cnt = 0;
static void CountZero( string s)
{
for ( int i = 0; i < s.Length; i++)
{
int div = s[i] - '0' ;
if (div == 0)
cnt++;
}
Console.WriteLine(cnt);
}
static void Main( string [] args)
{
string s = "101101" ;
CountZero(s);
}
}
|
Javascript
let cnt = 0;
function countZero(s) {
for (let i = 0; i < s.length; i++) {
if (s[i] === '0' ) {
cnt++;
}
}
console.log(cnt);
}
let s = "101101" ;
countZero(s);
|
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;
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" ;
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);
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Related Articles:
Last Updated :
28 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...