Check if the given decimal number has 0 and 1 digits only
Given an integer n, the task is to check whether n is in binary or not. Print true if n is the binary representation else print false.
Examples:
Input: n = 1000111
Output: true
Input: n = 123
Output: false
Method #1: Using Set First add all the digits of n into a set after that remove 0 and 1 from the set, if the size of the set becomes 0 then the number is in binary format.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
bool isBinary( int number)
{
set< int > set;
while (number > 0) {
int digit = number % 10;
set.insert(digit);
number /= 10;
}
set.erase(0);
set.erase(1);
if (set.size() == 0) {
return true ;
}
return false ;
}
int main()
{
int n = 1000111;
if (isBinary(n)==1)
cout<< "true" <<endl;
else
cout<< "No" <<endl;
}
|
Java
import java.util.HashSet;
import java.util.Set;
class GFG {
static boolean isBinary( int number)
{
Set<Integer> set = new HashSet<>();
while (number > 0 ) {
int digit = number % 10 ;
set.add(digit);
number /= 10 ;
}
set.remove( 0 );
set.remove( 1 );
if (set.size() == 0 ) {
return true ;
}
return false ;
}
public static void main(String a[])
{
int n = 1000111 ;
System.out.println(isBinary(n));
}
}
|
Python3
def isBinary(number):
set1 = set ()
while (number > 0 ):
digit = number % 10
set1.add(digit)
number = int (number / 10 )
set1.discard( 0 )
set1.discard( 1 )
if ( len (set1) = = 0 ):
return True
return False
if __name__ = = '__main__' :
n = 1000111
if (isBinary(n) = = 1 ):
print ( "true" )
else :
print ( "No" )
|
C#
using System;
using System.Collections.Generic;
public class GFG {
static bool isBinary( int number)
{
HashSet< int > set = new HashSet< int >();
while (number > 0) {
int digit = number % 10;
set .Add(digit);
number /= 10;
}
set .Remove(0);
set .Remove(1);
if ( set .Count == 0) {
return true ;
}
return false ;
}
public static void Main()
{
int n = 1000111;
Console.WriteLine(isBinary(n));
}
}
|
Javascript
<script>
function isBinary(number)
{
let set = new Set();
while (number > 0) {
let digit = number % 10;
set.add(digit);
number = Math.floor(number/10);
}
set. delete (0);
set. delete (1);
if (set.size == 0) {
return true ;
}
return false ;
}
let n = 1000111;
document.write(isBinary(n));
</script>
|
Time Complexity: O(logN), as we are using a loop to traverse logN times as in each traversal we are decrementing N by floor division of 10.
Auxiliary Space: O(logN), as we are using extra space for the set.
Method #2: Native Way
C++
#include<bits/stdc++.h>
using namespace std;
int isBinary( int number)
{
while (number > 0)
{
int digit = number % 10;
if (digit > 1)
return false ;
number /= 10;
}
return true ;
}
int main()
{
int n = 1000111;
if (isBinary(n) == 1)
cout << "true" ;
else
cout << "false" ;
}
|
Java
class GFG {
static boolean isBinary( int number)
{
while (number > 0 ) {
int digit = number % 10 ;
if (digit > 1 )
return false ;
number /= 10 ;
}
return true ;
}
public static void main(String a[])
{
int n = 1000111 ;
System.out.println(isBinary(n));
}
}
|
Python3
def isBinary(number):
while (number > 0 ):
digit = number % 10
if (digit > 1 ):
return False
number / / = 10
return True
if __name__ = = "__main__" :
n = 1000111
if (isBinary(n) = = 1 ):
print ( "true" )
else :
print ( "false" )
|
C#
using System;
class GFG {
static bool isBinary( int number)
{
while (number > 0) {
int digit = number % 10;
if (digit > 1)
return false ;
number /= 10;
}
return true ;
}
static void Main()
{
int n = 1000111;
Console.WriteLine(isBinary(n));
}
}
|
PHP
<?php
function isBinary( $number )
{
while ( $number > 0)
{
$digit = $number % 10;
if ( $digit > 1)
return false;
$number /= 10;
}
return true;
}
$n = 1000111;
if (isBinary( $n ) == 1)
echo "true" ;
else
echo "false" ;
|
Javascript
<script>
function isBinary(number)
{
while (number > 0)
{
let digit = number % 10;
if (digit > 1)
return false ;
number = Math.floor(number / 10);
}
return true ;
}
let n = 1000111;
document.write(isBinary(n));
</script>
|
Time Complexity: O(logN), as we are using a loop to traverse logN times as in each traversal we are decrementing N by floor division of 10.
Auxiliary Space: O(1), as we are not using any extra space.
Last Updated :
25 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...