Check if given number contains only “01” and “10” as substring in its binary representation


In this article, we delve into an interesting problem from the world of binary string manipulation: "Check if a given number contains only '01' and '10' as substrings in its binary representation". This problem challenges us to verify whether a number's binary representation contains only the substrings '01' and '10'. We'll discuss the problem in detail, offer a C++ code implementation, and illustrate the concept with an example.

Understanding the Problem Statement

Given a number, the task is to check if its binary representation contains only '01' and '10' as substrings. In other words, we need to verify if the binary representation of a given number consists of alternating 0s and 1s.

Approach

A simple way to solve this problem is to convert the number into its binary representation and then check if the binary string contains any '00' or '11' substring. If it does, then the binary representation does not consist of only '01' and '10' substrings. If it doesn't, then it does.

Example

Following are the programs to the above approach −

#include <stdio.h>

int checkBinary(int n) {
   char binary[33];
   sprintf(binary, "%d", n);  // Convert integer to string
   for (int i = 0; binary[i + 1] != '\0'; i++) {
      if (binary[i] == binary[i + 1]) {
         return 0;  // Binary representation contains consecutive same digits
      }
   }
   return 1;  // Binary representation contains only alternating '0's and '1's
}
int main() {
   int n = 5;
   if (checkBinary(n)) {
      printf("The binary representation of %d contains only '01' and '10' substrings.\n", n);
   } else {
      printf("The binary representation of %d does not contain only '01' and '10' substrings.\n", n);
   }
   return 0;
}

Output

The binary representation of 5 contains only '01' and '10' substrings.
#include<bits/stdc++.h>
using namespace std;

bool checkBinary(int n) {
   string binary = bitset<32>(n).to_string();
   return binary.find("00") == string::npos && binary.find("11") == string::npos;
}
int main() {
   int n = 5;
   if (checkBinary(n)) {
      cout << "The binary representation of " << n << " contains only '01' and '10' substrings.";
   } else {
      cout << "The binary representation of " << n << " does not contain only '01' and '10' substrings.";
   }
   return 0;
}

Output

The binary representation of 5 contains only '01' and '10' substrings.
public class BinarySubstringCheck {
   public static boolean checkBinary(int n) {
      String binary = Integer.toBinaryString(n);
      for (int i = 0; i < binary.length() - 1; i++) {
         if (binary.charAt(i) == binary.charAt(i + 1)) {
            return false;  // Binary representation contains consecutive same digits
         }
      }
      return true;  // Binary representation contains only alternating '0's and '1's
   }

   public static void main(String[] args) {
      int n = 5;
      if (checkBinary(n)) {
         System.out.println("The binary representation of " + n + " contains only '01' and '10' substrings.");
      } else {
         System.out.println("The binary representation of " + n + " does not contain only '01' and '10' substrings.");
      }
   }
}

Output

The binary representation of 5 contains only '01' and '10' substrings.
def check_binary(n):
   binary = bin(n)[2:]  # Convert integer to binary string
   for i in range(len(binary) - 1):
      if binary[i] == binary[i + 1]:
         return False  # Binary representation contains consecutive same digits
   return True  # Binary representation contains only alternating '0's and '1's

def main():
   n = 5
   if check_binary(n):
      print(f"The binary representation of {n} contains only '01' and '10' substrings.")
   else:
      print(f"The binary representation of {n} does not contain only '01' and '10' substrings.")

if __name__ == "__main__":
   main()

Output

The binary representation of 5 contains only '01' and '10' substrings.

This code first converts the number into its binary representation using the bitset function. It then checks if this binary string contains any '00' or '11' substring using the find function.

Test Case

Let's consider the number 5. Its binary representation is '101'. As you can see, it contains only '01' and '10' as substrings. Therefore, the output of the code will be: "The binary representation of 5 contains only '01' and '10' substrings."

Conclusion

This article provided an insight into the problem of checking if a given number's binary representation contains only '01' and '10' substrings. The problem, although seemingly complex, simplifies when approached methodically. We discussed a strategy to solve the problem, implemented the approach in C++, and explained the concept with a practical example.

Updated on: 16-Oct-2023

146 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements