Divisibility by 64 with removal of bits allowed - GeeksforGeeks

27 Apr, 2021
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++
Java
Python3
C#
PHP
Javascript
// CPP program to find if given binary string can// become divisible by 64 after removing some bits.#include <iostream>using namespace std; // function to check if it is possible// to make it a multiple of 64.bool checking(string s){    int c = 0; // counter to count 0's    int n = s.length(); // length of the string     // loop which traverses right to left and    // calculates the number of zeros before 1.    for (int i = n - 1; i >= 0; i--) {        if (s[i] == '0')            c++;         if (c >= 6 and s[i] == '1')            return true;    }     return false;} // driver codeint main(){    string s = "100010001";    if (checking(s))        cout << "Possible";    else        cout << "Not possible";    return 0;}
// Java program to find if// given binary string can// become divisible by// 64 after removing some bitsimport java.io.*; class GFG{    // function to check if it is possible    // to make it a multiple of 64.    static boolean checking(String s)    {        // counter to count 0's        int c = 0;                 // length of the string        int n = s.length();             // loop which traverses right to left and        // calculates the number of zeros before 1.        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;    }     // Driver code    public static void main (String[] args)    {        String s = "100010001";        if (checking(s))            System.out.println ( "Possible");        else            System.out.println ( "Not possible");         }} // This code  is contributed by vt_m
# Python 3 program to find if given binary# string can become divisible by 64 after# removing some bits. # function to check if it is possible# to make it a multiple of 64.def checking(s):    c = 0         # counter to count 0's    n = len(s)         # length of the string     # loop which traverses right to left and    # calculates the number of zeros before 1.    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 # Driver codeif __name__ == '__main__':    s = "100010001"    if (checking(s)):        print("Possible")    else:        print("Not possible")     # This code is contributed by# Surendra_Gangwar
// C# program to find if given binary// string can become divisible by 64// after removing some bitsusing System; class GFG {         // function to check if it is possible    // to make it a multiple of 64.    static bool checking(string s)    {                 // counter to count 0's        int c = 0;                 // length of the string        int n = s.Length;             // loop which traverses right to        // left and calculates the number        // of zeros before 1.        for (int i = n - 1; i >= 0; i--)        {            if (s[i] == '0')                c++;                 if (c >= 6 && s[i] == '1')                return true;        }             return false;    }     // Driver code    public static void Main ()    {        String s = "100010001";                 if (checking(s))            Console.WriteLine (                          "Possible");        else            Console.WriteLine(                     "Not possible");    }} // This code is contributed by vt_m.
<?php// PHP program to find if// given binary string can// become divisible by 64// after removing some bits. // function to check if// it is possible// to make it a multiple of 64. function checking($s){    // counter to count 0's    $c = 0;         // length of the string    $n = strlen($s);     // loop which traverses right    // to left and calculates the    // number of zeros before 1.    for ($i = $n - 1; $i >= 0; $i--)    {        if ($s[$i] == '0')            $c++;         if ($c >= 6 and $s[$i] == '1')            return true;    }     return false;} // Driver Code$s = "100010001";if (checking($s))    echo "Possible";else    echo "Not possible"; // This code is contributed by ajit?>
<script> // Javascript program to find if given binary// string can become divisible by 64// after removing some bits     // function to check if it is possible    // to make it a multiple of 64.    function checking(s)    {                   // counter to count 0's        let c = 0;                   // length of the string        let n = s.length;               // loop which traverses right to        // left and calculates the number        // of zeros before 1.        for (let i = n - 1; i >= 0; i--)        {            if (s[i] == '0')                c++;                   if (c >= 6 && s[i] == '1')                return true;        }               return false;    }     // Driver code        let s = "100010001";                   if (checking(s))            document.write(                          "Possible");        else            document.write(                     "Not possible");       // This code is contributed by code_hunt.</script>
Output :  
Possible
Time Complexity: O(length of string) 
YouTubeGeeksforGeeks507K subscribersDivisibility by 64 with removal of bits allowed | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.You're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmMore videosMore videosSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 3:11•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=9Gq2xPjmXw4" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
This article is contributed by Twinkle Bajaj. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
vt_m
jit_t
SURENDRA_GANGWAR
code_hunt
binary-string
divisibility
Bit Magic
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Set, Clear and Toggle a given bit of a number in C
Find the size of Largest Subset with positive Bitwise AND
Check whether bitwise AND of a number with any subset of an array is zero or not
Write an Efficient Method to Check if a Number is Multiple of 3
Highest power of 2 less than or equal to given number
Swap two nibbles in a byte
Swap bits in a given number
Check for Integer Overflow
Reverse actual bits of the given number
Find one extra character in a string