Open In App

Decimal representation of given binary string is divisible by 20 or not

Last Updated : 19 Apr, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

The problem is to check whether the decimal representation of the given binary number is divisible by 20 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 the minimum numbers of multiplication and division operations. No leadings 0’s are there in the input.

Examples:

Input : 101000
Output : Yes
(10100)2 = (40)10
and 40 is divisible by 20.
Input : 110001110011100
Output : Yes

Approach:

  1. Initialize a variable 'dec' to 0, which will store the decimal value of the binary number.
  2. Traverse the binary number from left to right and for each digit, multiply the current value of 'dec' by 2 and add the current binary digit (0 or 1) to it.
  3. After the traversal is complete, the final value of 'dec' will be the decimal representation of the binary number.

Once we have the decimal representation of the binary number, we can check whether it is divisible by 20 by using the modulo operator (%). If the decimal representation is divisible by 20, then the binary number is also divisible by 20.

Implementation:

C++
Java Python3 C# JavaScript

Output
Yes

Output: Yes

Time Complexity: O(N)
Space Complexity: O(1)

Approach: Following are the steps:

  1. Let the binary string be bin[].
  2. Let the length of bin[] be n.
  3. If bin[n-1] == '1', then it is an odd number and thus not divisible by 20.
  4. Else check if bin[0..n-2] is divisible by 10. Refer to this post.
C++
Java Python3 C# PHP JavaScript

Output
Yes

Time Complexity: O(n), where n is the number of digits in the binary number.
Auxiliary Space: O(1)


Next Article
Practice Tags :

Similar Reads