Given a positive integer **N**, the task is to check whether the binary equivalent of that integer ends with a given string **str** or not.

Print “Yes” if it ends in “str”. Otherwise, Print “No”.**Examples**:

Input:N = 23, str = “111”Output:YesExplanation:

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 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; ` `}` |

*chevron_right*

*filter_none*

**Output:**

Yes

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:

- Check whether the binary equivalent of a number ends with "001" or not
- Sum of decimal equivalent of all possible pairs of Binary representation of a Number
- Check if a number ends with another number or not
- Check if binary representations of 0 to N are present as substrings in given binary string
- Convert the given BCD to its equivalent Binary form
- Check whether two strings are equivalent or not according to given condition
- Maximum decimal equivalent possible among all connected components of a Binary Valued Graph
- Python | Check if a given string is binary string or not
- Find if a string starts and ends with another given string
- Convert the ASCII value sentence to its equivalent string
- Build a DFA to accept Binary strings that starts or ends with "01"
- Check if binary string multiple of 3 using DFA
- Check divisibility of binary string by 2^k
- Check if all the 1's in a binary string are equidistant or not
- Check if a binary string contains consecutive same or not
- Check if given Binary string follows then given condition or not
- Check if a binary string has a 0 between 1s or not | Set 1 (General approach)
- Check if a binary string contains all permutations of length k
- Check if it is possible to rearrange a binary string with alternate 0s and 1s
- Program to build a DFA that checks if a string ends with "01" or "10"

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.