The problem is to check whether the decimal representation of the given binary number is divisible by 5 or not. Take care, the number could be very large and may not fit even in long long int. The approach should be such that there are zero or minimum number of multiplication and division operations. No leading 0’s are there in the input.
Examples:
Input : 1010
Output : YES
(1010)2 = (10)10,
and 10 is divisible by 5.
Input : 10000101001
Output : YES
Approach:
The following steps are:
- Convert the binary number to base 4.
- Numbers in base 4 contains only 0, 1, 2, 3 as their digits.
- 5 in base 4 is equivalent to 11.
- Now apply the rule of divisibility by 11 where you add all the digits at odd places and add all the digits at even places and then subtract one from the other. If the result is divisible by 11(which remember is 5), then the binary number is divisible by 5.
How to convert binary number to base 4 representation?
- Check whether the length of binary string is even or odd.
- If odd, the add ‘0’ in the beginning of the string.
- Now, traverse the string from left to right.
- One by extract substrings of size 2 and add their equivalent decimal to the resultant string.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int equivalentBase4(string bin)
{
if (bin.compare( "00" ) == 0)
return 0;
if (bin.compare( "01" ) == 0)
return 1;
if (bin.compare( "10" ) == 0)
return 2;
return 3;
}
string isDivisibleBy5(string bin)
{
int l = bin.size();
if (l % 2 != 0)
bin = '0' + bin;
int odd_sum, even_sum = 0;
int isOddDigit = 1;
for ( int i = 0; i<bin.size(); i+= 2)
{
if (isOddDigit)
odd_sum += equivalentBase4(bin.substr(i, 2));
else
even_sum += equivalentBase4(bin.substr(i, 2));
isOddDigit ^= 1;
}
if ( abs (odd_sum - even_sum) % 5 == 0)
return "Yes" ;
return "No" ;
}
int main()
{
string bin = "10000101001" ;
cout << isDivisibleBy5(bin);
return 0;
}
|
Java
class GFG
{
static int equivalentBase4(String bin)
{
if (bin.compareTo( "00" ) == 0 )
return 0 ;
if (bin.compareTo( "01" ) == 0 )
return 1 ;
if (bin.compareTo( "10" ) == 0 )
return 2 ;
return 3 ;
}
static String isDivisibleBy5(String bin)
{
int l = bin.length();
if (l % 2 != 0 )
bin = '0' + bin;
int odd_sum= 0 , even_sum = 0 ;
int isOddDigit = 1 ;
for ( int i = 0 ; i<bin.length(); i+= 2 )
{
if (isOddDigit != 0 )
odd_sum += equivalentBase4(bin.substring(i, i+ 2 ));
else
even_sum += equivalentBase4(bin.substring(i, i+ 2 ));
isOddDigit ^= 1 ;
}
if (Math.abs(odd_sum - even_sum) % 5 == 0 )
return "Yes" ;
return "No" ;
}
public static void main (String[] args)
{
String bin = "10000101001" ;
System.out.println(isDivisibleBy5(bin));
}
}
|
Python 3
def equivalentBase4( bin ):
if ( bin = = "00" ):
return 0
if ( bin = = "01" ):
return 1
if ( bin = = "10" ):
return 2
if ( bin = = "11" ):
return 3
def isDivisibleBy5( bin ):
l = len ( bin )
if ((l % 2 ) = = 1 ):
bin = '0' + bin
odd_sum = 0
even_sum = 0
isOddDigit = 1
for i in range ( 0 , len ( bin ), 2 ):
if (isOddDigit):
odd_sum + = equivalentBase4( bin [i:i + 2 ])
else :
even_sum + = equivalentBase4( bin [i:i + 2 ])
isOddDigit = isOddDigit ^ 1
if ( abs (odd_sum - even_sum) % 5 = = 0 ):
return "Yes"
else :
return "No"
if __name__ = = "__main__" :
bin = "10000101001"
print (isDivisibleBy5( bin ))
|
C#
using System;
class GFG
{
public static int equivalentBase4( string bin)
{
if (bin.CompareTo( "00" ) == 0)
{
return 0;
}
if (bin.CompareTo( "01" ) == 0)
{
return 1;
}
if (bin.CompareTo( "10" ) == 0)
{
return 2;
}
return 3;
}
public static string isDivisibleBy5( string bin)
{
int l = bin.Length;
if (l % 2 != 0)
{
bin = '0' + bin;
}
int odd_sum = 0, even_sum = 0;
int isOddDigit = 1;
for ( int i = 0; i < bin.Length; i += 2)
{
if (isOddDigit != 0)
{
odd_sum += equivalentBase4(
bin.Substring(i, 2));
}
else
{
even_sum += equivalentBase4(
bin.Substring(i, 2));
}
isOddDigit ^= 1;
}
if (Math.Abs(odd_sum - even_sum) % 5 == 0)
{
return "YES" ;
}
return "NO" ;
}
public static void Main( string [] args)
{
string bin = "10000101001" ;
Console.WriteLine(isDivisibleBy5(bin));
}
}
|
Javascript
<script>
function equivalentBase4(bin){
if (bin == "00" )
return 0
if (bin == "01" )
return 1
if (bin == "10" )
return 2
if (bin == "11" )
return 3
}
function isDivisibleBy5(bin){
let l = bin.length
if ((l % 2) == 1){
bin = '0' + bin
}
let odd_sum = 0
let even_sum = 0
let isOddDigit = 1
for (let i=0;i<bin.length;i+=2){
if (isOddDigit)
odd_sum += equivalentBase4(bin.substring(i,i + 2))
else
even_sum += equivalentBase4(bin.substring(i,i + 2))
isOddDigit = isOddDigit ^ 1
}
if (Math.abs(odd_sum - even_sum) % 5 == 0)
return "Yes"
else
return "No"
}
let bin = "10000101001"
document.writeln(isDivisibleBy5(bin))
</script>
|
Time Complexity: O(n), where n is the number of digits in the binary number.
Auxiliary Space: O(1)
This article is contributed by Aarti_Rathi and Ayush Jauhari. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.