![]() |
VOOZH | about |
Given a number N, the task is to check first whether the given number is binary or not and its value should be greater than 1. print true if N is the binary representation else print false.
Examples:
Input: N = 1010
Output: true
Explanation:
Given number is greater than 1 and none of its digits is greater than 1. Thus, it is a binary number greater than 1.Examples: N = 1234
Output: false
Explanation:
Given number is greater than 1 but some of its digits { 2, 3, 4} are greater than 1. Thus, it is not a binary number.
Iterative Approach:
Below is the implementation of the above approach:
true
Time Complexity: O(K), K is the number of digits in N
Auxiliary Space: O(1)
Regular Expression Approach:
1. Convert the number into string.
2. Create a Regular Expression as mentioned below:
regex = "[01][01]+";
where:
3. Match the given number with the regular expression. If it matches return true, else return false.
Below is the implementation of the above approach:
true
Time Complexity: O(K), K is the number of digits in N
Auxiliary Space: O(1)