Given a binary string S of length N, the task is to check if the decimal representation of the binary string is divisible by 9 or not.
Examples:
Input: S = 1010001
Output:True
Explanation: The decimal representation of the binary string S is 81, which is divisible by 9. Therefore, the required output is True.Input: S = 1010011
Output: False
Explanation: The decimal representation of the binary string S is 81, which is divisible by 9. Therefore, the required output is False.
Naive Approach: The simplest approach to solve this problem is to convert the binary number into a decimal number and check if the decimal number is divisible by 9 or not. If found to be true then print True. Otherwise, print False.
Time Complexity: O(N)
Auxiliary Space: O(1)
Efficient Approach: If the length of a binary string is greater than 64 then the decimal representation of the binary string will cause an overflow. Therefore, to reduce the overflow issue the idea is to convert the binary string into the octal representation and check if the octal representation of the binary string is divisible by 9 or not. Follow the steps below to solve the problem:
- Convert the binary string into octal representation.
- Initialize a variable, say Oct_9 to store the octal representation of 9.
- Find the sum of digits, say evenSum present at even positions in the octal representation of the binary string.
- Find the sum of digits, say oddSum present at odd positions in the octal representation of the binary string.
- Check if abs(oddSum – EvenSum) % Oct_9 == 0 or not. If found to be true, then print Yes.
- Otherwise, print No.
Below is the implementation of the above approach:
C++
// C++ program to implement // the above approach #include <bits/stdc++.h> using namespace std; // Function to convert the binary string // into octal representation string ConvertequivalentBase8(string S) { // Stores binary representation of // the decimal value [0 - 7] map<string, char> mp; // Stores the decimal values // of binary strings [0 - 7] mp["000"] = '0'; mp["001"] = '1'; mp["010"] = '2'; mp["011"] = '3'; mp["100"] = '4'; mp["101"] = '5'; mp["110"] = '6'; mp["111"] = '7'; // Stores length of S int N = S.length(); if (N % 3 == 2) { // Update S S = "0" + S; } else if (N % 3 == 1) { // Update S S = "00" + S; } // Update N N = S.length(); // Stores octal representation // of the binary string string oct; // Traverse the binary string for (int i = 0; i < N; i += 3) { // Stores 3 consecutive characters // of the binary string string temp = S.substr(i, 3); // Append octal representation // of temp oct.push_back(mp[temp]); } return oct; } // Function to check if binary string // is divisible by 9 or not string binString_div_9(string S, int N) { // Stores octal representation // of S string oct; oct = ConvertequivalentBase8(S); // Stores sum of elements present // at odd positions of oct int oddSum = 0; // Stores sum of elements present // at odd positions of oct int evenSum = 0; // Stores length of oct int M = oct.length(); // Traverse the string oct for (int i = 0; i < M; i += 2) { // Update oddSum oddSum += int(oct[i] - '0'); } // Traverse the string oct for (int i = 1; i < M; i += 2) { // Update evenSum evenSum += int(oct[i] - '0'); } // Stores cotal representation // of 9 int Oct_9 = 11; // If absolute value of (oddSum // - evenSum) is divisible by Oct_9 if (abs(oddSum - evenSum) % Oct_9 == 0) { return "Yes"; } return "No"; } // Driver Code int main() { string S = "1010001"; int N = S.length(); cout << binString_div_9(S, N); } |
Yes
Time Complexity: O(N)
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Decimal representation of given binary string is divisible by 5 or not
- Decimal representation of given binary string is divisible by 10 or not
- Decimal representation of given binary string is divisible by 20 or not
- Check if Decimal representation of an Octal number is divisible by 7
- Convert a given Decimal number to its BCD representation
- Convert Decimal To Hexa-Decimal including negative numbers
- Find permutation of n which is divisible by 3 but not divisible by 6
- Find the occurrence of the given binary pattern in the binary representation of the array elements
- Check if binary representation of a number is palindrome
- Check if binary representation of a given number and its complement are anagram
- Check if actual binary representation of a number is palindrome
- Check if the binary representation of a number has equal number of 0s and 1s in blocks
- Check if all the set bits of the binary representation of N are at least K places away
- Construct Binary Tree from String with bracket representation
- Python | Check if a given string is binary string or not
- Number of substrings with odd decimal value in a binary string
- Counting even decimal value substrings in a binary string
- Largest sub-string of a binary string divisible by 2
- Check if a large number is divisible by 11 or not
- Check if a large number is divisible by 3 or not
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

