Divisibility by 64 with removal of bits allowed
Last Updated :
21 Jun, 2022
Given a binary string, we need to check whether that number is divisible by 64 or not after removing of some bits. If yes then print “possible” else “not possible”. We cannot make number 0 to make it divisible.
Example :
Input: 100010001
Output: Possible
Explanation: We can get string 1 000 000
after removing two ones which is a
representation of number 64 in the binary
numerical system.
Input: 100
Output: Not possible
Explanation : The number is 4 which is not
divisible by 64 or cannot be made possible
my removing some digits.
If we have 6 zeros after any one, then we can remove other bits represent it as a multiple of 64. So we just need to check if there is a 1 before six zeros.
C++
#include <iostream>
using namespace std;
bool checking(string s)
{
int c = 0;
int n = s.length();
for ( int i = n - 1; i >= 0; i--) {
if (s[i] == '0' )
c++;
if (c >= 6 and s[i] == '1' )
return true ;
}
return false ;
}
int main()
{
string s = "100010001" ;
if (checking(s))
cout << "Possible" ;
else
cout << "Not possible" ;
return 0;
}
|
Java
import java.io.*;
class GFG
{
static boolean checking(String s)
{
int c = 0 ;
int n = s.length();
for ( int i = n - 1 ; i >= 0 ; i--)
{
if (s.charAt(i) == '0' )
c++;
if (c >= 6 && s.charAt(i) == '1' )
return true ;
}
return false ;
}
public static void main (String[] args)
{
String s = "100010001" ;
if (checking(s))
System.out.println ( "Possible" );
else
System.out.println ( "Not possible" );
}
}
|
Python3
def checking(s):
c = 0
n = len (s)
i = n - 1
while (i > = 0 ):
if (s[i] = = '0' ):
c + = 1
if (c > = 6 and s[i] = = '1' ):
return True
i - = 1
return False
if __name__ = = '__main__' :
s = "100010001"
if (checking(s)):
print ( "Possible" )
else :
print ( "Not possible" )
|
C#
using System;
class GFG {
static bool checking( string s)
{
int c = 0;
int n = s.Length;
for ( int i = n - 1; i >= 0; i--)
{
if (s[i] == '0' )
c++;
if (c >= 6 && s[i] == '1' )
return true ;
}
return false ;
}
public static void Main ()
{
String s = "100010001" ;
if (checking(s))
Console.WriteLine (
"Possible" );
else
Console.WriteLine(
"Not possible" );
}
}
|
PHP
<?php
function checking( $s )
{
$c = 0;
$n = strlen ( $s );
for ( $i = $n - 1; $i >= 0; $i --)
{
if ( $s [ $i ] == '0' )
$c ++;
if ( $c >= 6 and $s [ $i ] == '1' )
return true;
}
return false;
}
$s = "100010001" ;
if (checking( $s ))
echo "Possible" ;
else
echo "Not possible" ;
?>
|
Javascript
<script>
function checking(s)
{
let c = 0;
let n = s.length;
for (let i = n - 1; i >= 0; i--)
{
if (s[i] == '0 ')
c++;
if (c >= 6 && s[i] == ' 1')
return true ;
}
return false ;
}
let s = "100010001" ;
if (checking(s))
document.write(
"Possible" );
else
document.write(
"Not possible" );
</script>
|
Output :
Possible
Time Complexity: O(length of string)
Space Complexity: O(1)
Share your thoughts in the comments
Please Login to comment...