Check whether the binary equivalent of a number ends with given string or not
Last Updated :
08 Jun, 2022
Given a positive integer N, the task is to check whether the binary equivalent of that integer ends with the given string str or not.
Print "Yes" if it ends in "str". Otherwise, Print "No".
Examples:
Input: N = 23, str = "111"
Output: Yes
Explanation:
Binary of 23 = 10111, which ends with "111"
Input: N = 5, str = "111"
Output: No
Approach: The idea is to find the Binary Equivalent of N and check if str 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 s2
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 "111" or not
bool CheckBinaryEquivalent(int N, string str)
{
// 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(str, bin);
}
// Driver code
int main()
{
int N = 23;
string str = "111";
if (CheckBinaryEquivalent(N, str))
cout << "Yes";
else
cout << "No";
return 0;
}
Java
// Java implementation of the
// above approach
class GFG{
// Function returns true if
// s1 is suffix of s2
static boolean isSuffix(String s1, String s2)
{
int n1 = s1.length(),
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 "111" or not
static boolean CheckBinaryEquivalent(int N,
String str)
{
// 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(str, bin);
}
// Driver code
public static void main(String[] args)
{
int N = 23;
String str = "111";
if (CheckBinaryEquivalent(N, str))
System.out.print("Yes\n");
else
System.out.print("No\n");
}
}
// This code is contributed by shubham
Python3
# Python3 implementation of the
# above approach
# Function returns true if
# s1 is suffix of s2
def 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 "111" or not
def CheckBinaryEquivalent(N, s):
# To store the binary
# number
B_Number = 0;
cnt = 0;
while (N != 0):
rem = N % 2;
c = pow(10, cnt);
B_Number += rem * c;
N //= 2;
# Count used to store
# exponent value
cnt += 1;
bin = str(B_Number);
return isSuffix(s, bin);
# Driver code
if __name__ == "__main__":
N = 23;
s = "111";
if (CheckBinaryEquivalent(N, s)):
print("Yes")
else:
print("No")
# This code is contributed by rutvik_56
C#
// C# implementation of the
// above approach
using System;
using System.Collections;
class GFG{
// Function returns true if
// s1 is suffix of s2
static bool isSuffix(String s1, String s2)
{
int n1 = s1.Length,
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 "111" or not
static bool CheckBinaryEquivalent(int N,
String str)
{
// 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(str, bin);
}
// Driver Code
public static void Main (String[] args)
{
int N = 23;
String str = "111";
if (CheckBinaryEquivalent(N, str))
Console.WriteLine("Yes\n");
else
Console.WriteLine("No\n");
}
}
// This code is contributed by jana_sayantan
JavaScript
<script>
// Javascript implementation of the
// above approach
// Function returns true if
// s1 is suffix of s2
function 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 "111" or not
function CheckBinaryEquivalent(N, str)
{
// 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 = parseInt(N/2);
// Count used to store
// exponent value
cnt++;
}
var bin = B_Number.toString();
return isSuffix(str, bin);
}
// Driver code
var N = 23;
var str = "111";
if (CheckBinaryEquivalent(N, str))
document.write( "Yes");
else
document.write( "No");
</script>
Time Complexity: O(|str|)
Auxiliary Space: O(log2(N)) as we are making an extra string bin for converting N into its binary equivalent string
Similar Reads
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 =
12 min read
Check if given Binary string follows then given condition or not Given binary string str, the task is to check whether the given string follows the below condition or not: String starts with a '1'.Each '1' is followed by empty string(""), '1', or "00".Each "00" is followed by empty string(""), '1'. If the given string follows the above criteria then print "Valid
10 min read
Program to convert given Binary to its equivalent ASCII character string Given a binary string str, the task is to find its equivalent ASCII (American Standard Code for Information Interchange) character string. Examples: Input: str = "0110000101100010"Output: abExplanation: Dividing str into set of 8 bits as follows: 01100001 = 97, ASCII value of 97 is 'a'.01100010 = 98
9 min read
Append a digit in the end to make the number equal to the length of the remaining string Given a string str in which an integer is appended in the end (with or without leading zeroes). The task is to find a single digit from the range [0, 9] that must be appended in the end of the integer so that the number becomes equal to the length of remaining string. Print -1 if its not possible.Ex
8 min read
Check length of a string is equal to the number appended at its last Given a string that (may) be appended with a number at last. You need to find whether the length of string excluding that number is equal to that number. For example for "helloworld10", answer is True as helloworld consist of 10 letters. Length of String is less than 10, 000. Examples : Input: str =
11 min read