Check whether the binary equivalent of a number ends with “001” or not
Given a positive integer N, the task is to check whether the binary equivalent of that integer ends with “001” or not.
Print “Yes” if it ends in “001”. Otherwise, Print “No“.
Examples :
Input: N = 9
Output: Yes
Explanation
Binary of 9 = 1001, which ends with 001
Input: N = 5
Output: No
Binary of 5 = 101, which does not end in 001
Naive Approach
Find the Binary Equivalent of N and check if 001 is a Suffix of its Binary Equivalent.
Below is the implementation of the above approach:
C++
// C++ implementation of the// above approach#include <bits/stdc++.h>using namespace std;// Function returns true if// s1 is suffix of s2bool isSuffix(string s1, string s2){ int n1 = s1.length(); int n2 = s2.length(); if (n1 > n2) return false; for (int i = 0; i < n1; i++) if (s1[n1 - i - 1] != s2[n2 - i - 1]) return false; return true;}// Function to check if binary equivalent// of a number ends in "001" or notbool CheckBinaryEquivalent(int N){ // To store the binary // number int B_Number = 0; int cnt = 0; while (N != 0) { int rem = N % 2; int c = pow(10, cnt); B_Number += rem * c; N /= 2; // Count used to store // exponent value cnt++; } string bin = to_string(B_Number); return isSuffix("001", bin);}// Driver codeint main(){ int N = 9; if (CheckBinaryEquivalent(N)) cout << "Yes"; else cout << "No"; return 0;} |
Java
// Java implementation of the above approachclass GFG{ // Function returns true if// s1 is suffix of s2static boolean isSuffix(String s1, String s2){ int n1 = s1.length(); int n2 = s2.length(); if (n1 > n2) return false; for(int i = 0; i < n1; i++) if (s1.charAt(n1 - i - 1) != s2.charAt(n2 - i - 1)) return false; return true;} // Function to check if binary equivalent// of a number ends in "001" or notstatic boolean CheckBinaryEquivalent(int N){ // To store the binary // number int B_Number = 0; int cnt = 0; while (N != 0) { int rem = N % 2; int c = (int)Math.pow(10, cnt); B_Number += rem * c; N /= 2; // Count used to store // exponent value cnt++; } String bin = Integer.toString(B_Number); return isSuffix("001", bin);} // Driver codepublic static void main (String[] args){ int N = 9; if (CheckBinaryEquivalent(N)) System.out.println("Yes"); else System.out.println("No");}}// This code is contributed by AnkitRai01 |
Python3
# Python3 implementation of the# above approach# Function returns true if# s1 is suffix of s2def isSuffix(s1, s2) : n1 = len(s1); n2 = len(s2); if (n1 > n2) : return False; for i in range(n1) : if (s1[n1 - i - 1] != s2[n2 - i - 1]) : return False; return True;# Function to check if binary equivalent# of a number ends in "001" or notdef CheckBinaryEquivalent(N) : # To store the binary # number B_Number = 0; cnt = 0; while (N != 0) : rem = N % 2; c = 10 ** cnt; B_Number += rem * c; N //= 2; # Count used to store # exponent value cnt += 1; bin = str(B_Number); return isSuffix("001", bin);# Driver codeif __name__ == "__main__" : N = 9; if (CheckBinaryEquivalent(N)) : print("Yes"); else : print("No"); # This code is contributed by AnkitRai01 |
C#
// C# implementation of the above approachusing System;class GFG{ // Function returns true if// s1 is suffix of s2static bool isSuffix(string s1, string s2){ int n1 = s1.Length; int n2 = s2.Length; if (n1 > n2) return false; for(int i = 0; i < n1; i++) if (s1[n1 - i - 1] != s2[n2 - i - 1]) return false; return true;} // Function to check if binary equivalent// of a number ends in "001" or notstatic bool CheckBinaryEquivalent(int N){ // To store the binary // number int B_Number = 0; int cnt = 0; while (N != 0) { int rem = N % 2; int c = (int)Math.Pow(10, cnt); B_Number += rem * c; N /= 2; // Count used to store // exponent value cnt++; } string bin = B_Number.ToString(); return isSuffix("001", bin);} // Driver codepublic static void Main (string[] args){ int N = 9; if (CheckBinaryEquivalent(N)) Console.WriteLine("Yes"); else Console.WriteLine("No");}}// This code is contributed by AnkitRai01 |
Javascript
<script>// javascript implementation of the above approach // Function returns true if// s1 is suffix of s2function isSuffix( s1, s2){ var n1 = s1.length; var n2 = s2.length; if (n1 > n2) return false; for(var i = 0; i < n1; i++) if (s1[n1 - i - 1] != s2[n2 - i - 1]) return false; return true;} // Function to check if binary equivalent// of a number ends in "001" or notfunction CheckBinaryEquivalent( N){ // To store the binary // number var B_Number = 0; var cnt = 0; while (N != 0) { var rem = N % 2; var c = Math.pow(10, cnt); B_Number += rem * c; N = Math.floor(N/ 2); // Count used to store // exponent value cnt++; } console.log(B_Number); var bin = B_Number.toString(); return isSuffix("001", bin);} // Driver code var N = 9; if (CheckBinaryEquivalent(N)) document.write("Yes"); else document.write("No"); </script> |
Yes
Time complexity: O(N)
Auxiliary space: O(1)
Efficient Approach
We can observe that the binary equivalent of a number ends in “001” only when (N – 1) is divisible by 8.
Illustration:
The sequence 1, 9, 17, 25, 33……. has 001 as the suffix in their binary representation.
Nth term of the above sequence is denoted by 8 * N + 1
So the binary equivalent of a number ends in “001” only when (N – 1) % 8 == 0
Below is the implementation of the above approach:
C++
// C++ implementation of the above// approach#include <bits/stdc++.h>using namespace std;// Function to check if binary// equivalent of a number ends// in "001" or notbool CheckBinaryEquivalent(int N){ // To check if binary equivalent // of a number ends in // "001" or not return (N - 1) % 8 == 0;}// Driver codeint main(){ int N = 9; if (CheckBinaryEquivalent(N)) cout << "Yes"; else cout << "No"; return 0;} |
Java
// Java implementation of the above approachclass GFG{ // Function to check if binary// equivalent of a number ends// in "001" or notstatic boolean CheckBinaryEquivalent(int N){ // To check if binary equivalent // of a number ends in // "001" or not return (N - 1) % 8 == 0;} // Driver codepublic static void main (String[] args){ int N = 9; if (CheckBinaryEquivalent(N)) System.out.println("Yes"); else System.out.println("No");}}// This code is contributed by AnkitRai01 |
Python3
# Python3 implementation of the above approach# Function to check if binary# equivalent of a number ends# in "001" or notdef CheckBinaryEquivalent(N): # To check if binary equivalent # of a number ends in # "001" or not return (N - 1) % 8 == 0;# Driver codeif __name__ == "__main__": N = 9; if (CheckBinaryEquivalent(N)): print("Yes"); else : print("No"); # This code is contributed by AnkitRai01 |
C#
// C# implementation of the above approachusing System;class GFG{ // Function to check if binary// equivalent of a number ends// in "001" or notstatic bool CheckBinaryEquivalent(int N){ // To check if binary equivalent // of a number ends in // "001" or not return (N - 1) % 8 == 0;} // Driver codepublic static void Main (string[] args){ int N = 9; if (CheckBinaryEquivalent(N)) Console.WriteLine("Yes"); else Console.WriteLine("No");}}// This code is contributed by AnkitRai01 |
Javascript
<script>// Javascript implementation of the above// approach// Function to check if binary// equivalent of a number ends// in "001" or notfunction CheckBinaryEquivalent(N){ // To check if binary equivalent // of a number ends in // "001" or not return (N - 1) % 8 == 0;}// Driver codevar N = 9;if (CheckBinaryEquivalent(N)) document.write( "Yes");else document.write( "No");</script> |
Yes
Time complexity: O(1)
Auxiliary space: O(1)
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. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.


