Check whether a number has consecutive 0’s in the given base or not
Last Updated :
03 Aug, 2023
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++
#include<bits/stdc++.h>
using namespace std;
int toK( int N, int K)
{
int w = 1;
int s = 0;
while (N != 0)
{
int r = N % K;
N = N/K;
s = r * w + s;
w *= 10;
}
return s;
}
bool check( int N)
{
bool fl = false ;
while (N != 0)
{
int r = N % 10;
N = N/10;
if (fl == true and r == 0)
return false ;
if (r > 0)
{
fl = false ;
continue ;
}
fl = true ;
}
return true ;
}
void hasConsecutiveZeroes( int N, int K)
{
int z = toK(N, K);
if (check(z))
cout<< "Yes" <<endl;
else
cout<< "No" <<endl;
}
int main()
{
int N = 15;
int K = 8;
hasConsecutiveZeroes(N, K);
}
|
Java
import java.util.*;
class GFG
{
static int toK( int N, int K)
{
int w = 1 ;
int s = 0 ;
while (N != 0 )
{
int r = N % K;
N = N / K;
s = r * w + s;
w *= 10 ;
}
return s;
}
static boolean check( int N)
{
boolean fl = false ;
while (N != 0 )
{
int r = N % 10 ;
N = N / 10 ;
if (fl == true && r == 0 )
return false ;
if (r > 0 )
{
fl = false ;
continue ;
}
fl = true ;
}
return true ;
}
static void hasConsecutiveZeroes( int N, int K)
{
int z = toK(N, K);
if (check(z))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
public static void main(String[] args)
{
int N = 15 ;
int K = 8 ;
hasConsecutiveZeroes(N, K);
}
}
|
Python3
def hasConsecutiveZeroes(N, K):
z = toK(N, K)
if (check(z)):
print ( "Yes" )
else :
print ( "No" )
def toK(N, K):
w = 1
s = 0
while (N ! = 0 ):
r = N % K
N = N / / K
s = r * w + s
w * = 10
return s
def check(N):
fl = False
while (N ! = 0 ):
r = N % 10
N = N / / 10
if (fl = = True and r = = 0 ):
return False
if (r > 0 ):
fl = False
continue
fl = True
return True
N, K = 15 , 8
hasConsecutiveZeroes(N, K)
|
C#
using System;
class GFG
{
static int toK( int N, int K)
{
int w = 1;
int s = 0;
while (N != 0)
{
int r = N % K;
N = N / K;
s = r * w + s;
w *= 10;
}
return s;
}
static Boolean check( int N)
{
Boolean fl = false ;
while (N != 0)
{
int r = N % 10;
N = N / 10;
if (fl == true && r == 0)
return false ;
if (r > 0)
{
fl = false ;
continue ;
}
fl = true ;
}
return true ;
}
static void hasConsecutiveZeroes( int N, int K)
{
int z = toK(N, K);
if (check(z))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
public static void Main(String[] args)
{
int N = 15;
int K = 8;
hasConsecutiveZeroes(N, K);
}
}
|
Javascript
<script>
function toK(N, K)
{
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 check(N)
{
let fl = false ;
while (N != 0)
{
let r = N % 10;
N = parseInt(N/10);
if (fl == true && r == 0)
return false ;
if (r > 0)
{
fl = false ;
continue ;
}
fl = true ;
}
return true ;
}
function hasConsecutiveZeroes(N, K)
{
let z = toK(N, K);
if (check(z))
document.write( "Yes" );
else
document.write( "No" );
}
let N = 15;
let K = 8;
hasConsecutiveZeroes(N, K);
</script>
|
PHP
<?php
function hasConsecutiveZeroes( $N , $K )
{
$z = toK( $N , $K );
if (check( $z ))
print ( "Yes" );
else
print ( "No" );
}
function toK( $N , $K )
{
$w = 1;
$s = 0;
while ( $N != 0)
{
$r = $N % $K ;
$N = (int)( $N / $K );
$s = $r * $w + $s ;
$w *= 10;
}
return $s ;
}
function check( $N )
{
$fl = false;
while ( $N != 0)
{
$r = $N % 10;
$N = (int)( $N / 10);
if ( $fl == true and $r == 0)
return false;
if ( $r > 0)
{
$fl = false;
continue ;
}
$fl = true;
}
return true;
}
$N = 15;
$K = 8;
hasConsecutiveZeroes( $N , $K );
?>
|
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) {
string s = "" ;
while (N > 0) {
s = to_string(N % K) + s;
N /= K;
}
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) {
String s = "" ;
while (N > 0 ) {
s = Integer.toString(N % K) + s;
N /= K;
}
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" );
}
}
}
|
Python3
def hasConsecutiveZeroes(N, K):
s = ""
while N > 0 :
s = str (N % K) + s
N / / = K
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)
{
string s = "" ;
while (N > 0)
{
s = (N % K).ToString() + s;
N /= K;
}
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
function hasConsecutiveZeroes(n, k) {
let s = "" ;
while (n > 0) {
s += String(n % k);
n = Math.floor(n / k);
}
let count = 0;
for (const c of s) {
if (c === "0" ) {
count++;
} else {
count = 0;
}
if (count >= 2) {
return false ;
}
}
return true ;
}
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.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...