Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Check whether a number has consecutive 0's in the given base or not
Next article icon

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

Last Updated : 05 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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:  

C++
// C++ implementation of the above approach #include<bits/stdc++.h> using namespace std;    // Function to convert N into base K int toK(int N, int K) {  // Weight of each digit     int w = 1;     int s = 0;     while (N != 0)      {         int r = N % K;         N = N/K;         s = r * w + s;         w *= 10;      }     return s;  }  // Function to check for consecutive 0 bool check(int N) {  // Flag to check if there are consecutive      // zero or not     bool fl = false;     while (N != 0)     {          int r = N % 10;         N = N/10;          // If there are two consecutive zero          // then returning False         if (fl == true and r == 0)             return false;         if (r > 0)             {             fl = false;             continue;             }         fl = true;      }      return true;          }  // We first convert to given base, then // check if the converted number has two // consecutive 0s or not void hasConsecutiveZeroes(int N, int K) {     int z = toK(N, K);     if (check(z))        cout<<"Yes"<<endl;     else       cout<<"No"<<endl; }        // Driver code int main() { int N = 15; int K = 8; hasConsecutiveZeroes(N, K);  } // This code is contributed by // Surendra_Gangwar 
Java
// Java implementation of the above approach import java.util.*;  class GFG  {   // Function to convert N into base K static int toK(int N, int K) {      // Weight of each digit     int w = 1;     int s = 0;     while (N != 0)     {         int r = N % K;         N = N / K;         s = r * w + s;         w *= 10;     }     return s;  }  // Function to check for consecutive 0 static boolean check(int N) {      // Flag to check if there are consecutive      // zero or not     boolean fl = false;     while (N != 0)     {          int r = N % 10;         N = N / 10;          // If there are two consecutive zero          // then returning False         if (fl == true && r == 0)             return false;         if (r > 0)         {             fl = false;             continue;         }         fl = true;     }     return true; }  // We first convert to given base, then // check if the converted number has two // consecutive 0s or not static void hasConsecutiveZeroes(int N, int K) {     int z = toK(N, K);     if (check(z))         System.out.println("Yes");     else         System.out.println("No"); }  // Driver code public static void main(String[] args) {     int N = 15;     int K = 8;     hasConsecutiveZeroes(N, K); } }  // This code is contributed by Princi Singh 
Python
# Python implementation of the above approach  # We first convert to given base, then # check if the converted number has two # consecutive 0s or not def hasConsecutiveZeroes(N, K):     z = toK(N, K)     if (check(z)):         print("Yes")     else:         print("No")  # Function to convert N into base K def toK(N, K):      # Weight of each digit     w = 1     s = 0     while (N != 0):         r = N % K         N = N//K         s = r * w + s         w* = 10     return s  # Function to check for consecutive 0 def check(N):      # Flag to check if there are consecutive      # zero or not     fl = False     while (N != 0):         r = N % 10         N = N//10          # If there are two consecutive zero          # then returning False         if (fl == True and r == 0):             return False         if (r > 0):             fl = False             continue         fl = True     return True  # Driver code N, K = 15, 8 hasConsecutiveZeroes(N, K) 
C#
// C# implementation of the above approach using System;  class GFG  {   // Function to convert N into base K static int toK(int N, int K) {      // Weight of each digit     int w = 1;     int s = 0;     while (N != 0)     {         int r = N % K;         N = N / K;         s = r * w + s;         w *= 10;     }     return s; }  // Function to check for consecutive 0 static Boolean check(int N) {      // Flag to check if there are consecutive      // zero or not     Boolean fl = false;     while (N != 0)     {          int r = N % 10;         N = N / 10;          // If there are two consecutive zero          // then returning False         if (fl == true && r == 0)             return false;         if (r > 0)         {             fl = false;             continue;         }         fl = true;     }     return true; }  // We first convert to given base, then // check if the converted number has two // consecutive 0s or not static void hasConsecutiveZeroes(int N, int K) {     int z = toK(N, K);     if (check(z))         Console.WriteLine("Yes");     else         Console.WriteLine("No"); }  // Driver code public static void Main(String[] args) {     int N = 15;     int K = 8;     hasConsecutiveZeroes(N, K); } }  // This code is contributed by 29AjayKumar 
JavaScript
<script>  // Javascript implementation of the above approach  // Function to convert N into base K function toK(N, K) {          // Weight of each digit     let w = 1;     let s = 0;          while (N != 0)     {         let r = N % K;         N = parseInt(N / K);         s = r * w + s;         w *= 10;     }     return s; }  // Function to check for consecutive 0 function check(N) {          // Flag to check if there are consecutive      // zero or not     let fl = false;          while (N != 0)     {         let r = N % 10;         N = parseInt(N/10);          // If there are two consecutive zero          // then returning False         if (fl == true && r == 0)             return false;         if (r > 0)         {             fl = false;             continue;         }         fl = true;     }      return true; }  // We first convert to given base, then // check if the converted number has two // consecutive 0s or not function hasConsecutiveZeroes(N, K) {     let z = toK(N, K);     if (check(z))        document.write("Yes");     else       document.write("No"); }  // Driver code let N = 15; let K = 8;  hasConsecutiveZeroes(N, K);  // This code is contributed by souravmahato348  </script> 
PHP
<?php // PHP implementation of the above approach  // We first convert to given base,  // then check if the converted number  // has two consecutive 0s or not function hasConsecutiveZeroes($N, $K) {     $z = toK($N, $K);     if (check($z))         print("Yes");     else         print("No"); }  // Function to convert N into base K function toK($N, $K) {     // Weight of each digit     $w = 1;     $s = 0;     while ($N != 0)     {         $r = $N % $K;         $N = (int)($N / $K);         $s = $r * $w + $s;         $w *= 10;     }     return $s; }  // Function to check for consecutive 0 function check($N) {     // Flag to check if there are      // consecutive zero or not     $fl = false;     while ($N != 0)     {         $r = $N % 10;         $N = (int)($N / 10);          // If there are two consecutive          // zero then returning false         if ($fl == true and $r == 0)             return false;         if ($r > 0)         {             $fl = false;             continue;         }         $fl = true;     }     return true; }  // Driver code $N = 15; $K = 8; hasConsecutiveZeroes($N, $K);  // This code is contributed by mits ?> 

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.

Approach: 2 

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:

C++
#include <iostream> #include <string>  using namespace std;  bool hasConsecutiveZeroes(int N, int K) {     // Convert N to base K     string s = "";     while (N > 0) {         s = to_string(N % K) + s;         N /= K;     }      // Traverse through the converted string and check for consecutive 0s     int count = 0;     for (char c : s) {         if (c == '0') {             count++;             if (count >= 2) {                 return false;             }         } else {             count = 0;         }     }      return true; }  int main() {     int N = 15;     int K = 8;     if (hasConsecutiveZeroes(N, K)) {         cout << "Yes" << endl;     } else {         cout << "No" << endl;     }     return 0; } 
Java
import java.util.*;  public class Main {   static boolean hasConsecutiveZeroes(int N, int K) {     // Convert N to base K     String s = "";     while (N > 0) {         s = Integer.toString(N % K) + s;         N /= K;     }      // Traverse through the converted string and check for consecutive 0s     int count = 0;     for (char c : s.toCharArray()) {         if (c == '0') {             count++;             if (count >= 2) {                 return false;             }         } else {             count = 0;         }     }      return true; }  public static void main(String[] args) {     int N = 15;     int K = 8;     if (hasConsecutiveZeroes(N, K)) {         System.out.println("Yes");     } else {         System.out.println("No");     } } } 
Python
def hasConsecutiveZeroes(N, K):     # Convert N to base K     s = ""     while N > 0:         s = str(N % K) + s         N //= K      # Traverse through the converted string and check for consecutive 0s     count = 0     for c in s:         if c == '0':             count += 1             if count >= 2:                 return False         else:             count = 0      return True  N = 15 K = 8 if hasConsecutiveZeroes(N, K):     print("Yes") else:     print("No") 
C#
using System;  public class ConsecutiveZeroesCheck {     public static bool HasConsecutiveZeroes(int N, int K)     {         // Convert N to base K         string s = "";         while (N > 0)         {             s = (N % K).ToString() + s;             N /= K;         }          // Traverse through the converted string and check for consecutive 0s         int count = 0;         foreach (char c in s)         {             if (c == '0')             {                 count++;                 if (count >= 2)                 {                     return false;                 }             }             else             {                 count = 0;             }         }          return true;     }      public static void Main(string[] args)     {         int N = 15;         int K = 8;          if (HasConsecutiveZeroes(N, K))         {             Console.WriteLine("Yes");         }         else         {             Console.WriteLine("No");         }     } } 
JavaScript
/**  * This function checks if the number N in base K has consecutive 0s.  *  * @param n The number to check.  * @param k The base to convert N to.  *  * @returns True if N in base K has consecutive 0s, False otherwise.  */ function hasConsecutiveZeroes(n, k) {      // Convert N to base K.     // The variable `s` will store the converted string.     let s = "";     while (n > 0) {         // Append the remainder of N divided by K to the string `s`.         s += String(n % k);         // Get the quotient of N divided by K.         n = Math.floor(n / k);     }      // Traverse through the converted string and check for consecutive 0s.     // The variable `count` will keep track of the number of consecutive 0s.     let count = 0;     for (const c of s) {         // If the current character is a 0, increment `count`.         if (c === "0") {             count++;         } else {             // If the current character is not a 0, reset `count` to 0.             count = 0;         }          // If `count` is greater than or equal to 2, return False.         if (count >= 2) {             return false;         }     }      // If `count` is less than 2, return True.     return true; }  /**  * This is the main entry point of the program.  *  * It takes two integers N and K as input and prints "Yes" if N in base K has consecutive 0s,  * and "No" otherwise.  */     const n = 15;     const k = 8;     if (hasConsecutiveZeroes(n, k)) {         console.log("Yes");     } else {         console.log("No");     } 

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.


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

I

indrajit1
Improve
Article Tags :
  • Mathematical
  • Python
  • Python Programs
  • DSA
  • base-conversion
Practice Tags :
  • Mathematical
  • python

Similar Reads

    Techniques to Find Consecutive 1s or 0s in a Python String
    We are given a binary string and a number m, and we have to check if the string has m consecutive 1’s or 0’s. Below are a few examples to understand the problem statement clearly. Examples:Input: str = “001001”, m = 2 Output: TrueExplanation: the string have 2 consecutive 0s Input: str = “1000000001
    4 min read
    Python Program for Remove leading zeros from a Number given as a string
    Given numeric string str, the task is to remove all the leading zeros from a given string. If the string contains only zeros, then print a single "0".Examples:Input: str = "0001234" Output: 1234 Explanation: Removal of leading substring "000" modifies the string to "1234". Hence, the final answer is
    3 min read
    Python program to convert Base 4 system to binary number
    Given a base 4 number N, the task is to write a python program to print its binary equivalent. Conversion Table: Examples: Input : N=11002233 Output : 101000010101111 Explanation : From that conversion table we changed 1 to 01, 2 to 10 ,3 to 11 ,0 to 00.Input : N=321321 Output: 111001111001Method 1:
    3 min read
    Python program to convert any base to decimal by using int() method
    Given a number and its base, the task is to convert the given number into its corresponding decimal number. The base of number can be anything like digits between 0 to 9 and A to Z. Where the value of A is 10, value of B is 11, value of C is 12 and so on. Examples: Input : '1011' base = 2 Output : 1
    2 min read
    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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences