Check whether a number has consecutive 0's in the given base or not
Last Updated :
05 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:
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
?>
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");
}
Time Complexity: O(logN + length of the string).
Auxiliary Space: O(logN) , because it also requires storing the converted number as a string.
Similar Reads
Check Whether String Contains Only Numbers or Not - Python
We are given a string s="12345" we need to check whether the string contains only number or not if the string contains only number we will return True or if the string does contains some other value then we will return False. This article will explore various techniques to check if a string contains
3 min read
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 - Check if String contains any Number
We are given a string and our task is to check whether it contains any numeric digits (0-9). For example, consider the following string: s = "Hello123" since it contains digits (1, 2, 3), the output should be True while on the other hand, for s = "HelloWorld" since it has no digits the output should
2 min read
Python Map | Length of the Longest Consecutive 1's in Binary Representation of a given integer
Given a number n, find length of the longest consecutive 1s in its binary representation. Examples: Input : n = 14 Output : 3 The binary representation of 14 is 1110. Input : n = 222 Output : 4 The binary representation of 222 is 11011110. We have existing solution for this problem please refer Leng
3 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 for Check if all digits of a number divide it
Given a number n, find whether all digits of n divide it or not. Examples: Input : 128Output : Yes128 % 1 == 0, 128 % 2 == 0, and 128 % 8 == 0. Input : 130Output : No We want to test whether each digit is non-zero and divides the number. For example, with 128, we want to test d != 0 && 128 %
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
Python program to check if number is palindrome (one-liner)
In this article, we are given a number and we have to check whether the number is palindrome or not in one-liner code. The output will be True if it's a Palindrome number otherwise it would be False. Let's discuss how to find whether a number is palindrome or not in this article. Input1: test_number
3 min read
Python Program to Check Number is a Power of Two
we will discuss how to write a Python program to determine whether a given number is a power of two. A number is said to be a power of two if it can be expressed in the form of 2n, where n is a non-negative integer.Examples: Pythondef is_power_of_two(n): if n <= 0: return False return (n & (n
4 min read