VOOZH about

URL: https://www.geeksforgeeks.org/dsa/check-whether-a-number-has-consecutive-0s-in-the-given-base-or-not/

⇱ Check whether a number has consecutive 0's in the given base or not - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Check whether a number has consecutive 0's in the given base or not

Last Updated : 5 May, 2025

Given a decimal number N, the task is to check if a number has consecutive zeroes or not after converting the number to its K-based notation.

Examples:

Input: N = 4, K = 2 
Output: No 
4 in base 2 is 100, As there are consecutive 2 thus the answer is No.

Input: N = 15, K = 8 
Output: Yes 
15 in base 8 is 17, As there are no consecutive 0 so the answer is Yes. 

Approach: First convert the number N into base K and then simply check if the number has consecutive zeroes or not.

Below is the implementation of the above approach:  


Output
Yes

Time Complexity:O(logkn + log10n), where n and k represents the value of the given integers.
Auxiliary Space: O(1), no extra space is required, so it is a constant.

Here is another approach to solve the same problem:

  • Start with the given number N and initialize a variable called count to 0.
  • Convert N to base K by continuously dividing N by K and appending the remainder to the right of the result until N becomes 0. Let's call the result of this conversion s.
  • Traverse through the string s and check each character. If the character is '0', increment count by 1. If the character is not '0', reset count to 0.
  • If count becomes 2 or more during the traversal, then the number N has consecutive 0s in base K. Otherwise, it does not.

Here is the code of above approach:


Output
Yes

Time Complexity: O(logN + length of the string).
Auxiliary Space: O(logN) , because it also requires storing the converted number as a string.

Comment
Article Tags:
Article Tags: