Print all Good numbers in given range
Given a digit ‘d’ and a range [L, R] where L < R, print all good numbers in given range that don’t contain digit ‘d’. A number is good if its every digit is larger than the sum of digits which are on the right side of that digit. For example 9620 is good number because 2 > 0, 6 > 2+0 and 9 > 6+2+0.
Example:
Input: L = 410, R = 520, d = 3
Output: 410 420 421 510 520
All the numbers in output are good (every digit is more
than sum of digits on right of it) and don't have digit 3.
Input: L = 410, R = 520, d = 1
Output: 420 430 520
All the numbers in output are good (every digit is more
than sum of digits on right of it) and don't have digit 1.
The idea is to traverse all numbers in the given range. For every number, traverse all digits. While traversing keep track of digit sum so far. At any point, if the previous sum becomes more than or equal to sum, return false. Also, if current digit becomes ‘d’, return false.
Below is the implementation of the idea.
C++
#include<bits/stdc++.h>
using namespace std;
bool isValid( int n, int d)
{
int digit = n%10;
int sum = digit;
if (digit == d)
return false ;
n /= 10;
while (n)
{
digit = n%10;
if (digit == d || digit <= sum)
return false ;
else
{
sum += digit;
n /= 10;
}
}
return 1;
}
void printGoodNumbers( int L, int R, int d)
{
for ( int i=L; i<=R; i++)
{
if (isValid(i, d))
cout << i << " " ;
}
}
int main()
{
int L = 410, R = 520, d = 3;
printGoodNumbers(L, R, d);
return 0;
}
|
Java
import java.io.*;
class Numbers
{
static boolean isValid( int n, int d)
{
int digit = n% 10 ;
int sum = digit;
if (digit == d)
return false ;
n /= 10 ;
while (n> 0 )
{
digit = n% 10 ;
if (digit == d || digit <= sum)
return false ;
else
{
sum += digit;
n /= 10 ;
}
}
return true ;
}
static void printGoodNumber( int L, int R, int d)
{
for ( int i=L;i<=R;i++)
{
if (isValid(i, d))
System.out.print(i+ " " );
}
}
public static void main (String[] args)
{
int L = 410 , R = 520 , d = 3 ;
printGoodNumber(L, R, d);
}
}
|
Python3
def isValid(n, d):
digit = n % 10 ;
sum = digit;
if (digit = = d):
return False ;
n = int (n / 10 );
while (n > 0 ):
digit = n % 10 ;
if (digit = = d or digit < = sum ):
return False ;
else :
sum + = digit;
n = int (n / 10 );
return True ;
def printGoodNumber(L, R, d):
for i in range (L, R + 1 ):
if (isValid(i, d)):
print (i, end = " " );
L = 410 ;
R = 520 ;
d = 3 ;
printGoodNumber(L, R, d);
|
C#
using System;
class GFG {
static bool isValid( int n, int d)
{
int digit = n % 10;
int sum = digit;
if (digit == d)
return false ;
n /= 10;
while (n > 0)
{
digit = n % 10;
if (digit == d || digit <= sum)
return false ;
else
{
sum += digit;
n /= 10;
}
}
return true ;
}
static void printGoodNumber( int L,
int R, int d)
{
for ( int i = L; i <= R; i++)
{
if (isValid(i, d))
Console.Write(i+ " " );
}
}
public static void Main ()
{
int L = 410, R = 520, d = 3;
printGoodNumber(L, R, d);
}
}
|
PHP
<?php
function isValid( $n , $d )
{
$digit = $n % 10;
$sum = $digit ;
if ( $digit == $d )
return false;
$n = (int)( $n / 10);
while ( $n )
{
$digit = $n % 10;
if ( $digit == $d || $digit <= $sum )
return false;
else
{
$sum += $digit ;
$n = (int)( $n / 10);
}
}
return 1;
}
function printGoodNumbers( $L , $R , $d )
{
for ( $i = $L ; $i <= $R ; $i ++)
{
if (isValid( $i , $d ))
echo $i . " " ;
}
}
$L = 410;
$R = 520;
$d = 3;
printGoodNumbers( $L , $R , $d );
?>
|
Javascript
<script>
function isValid(n, d)
{
var digit = n % 10;
var sum = digit;
if (digit == d)
return false ;
n = parseInt(n / 10);
while (n > 0)
{
digit = n%10;
if (digit == d || digit <= sum)
return false ;
else
{
sum += digit;
n = parseInt(n / 10);
}
}
return true ;
}
function printGoodNumber(L, R, d)
{
for (i = L; i <= R; i++)
{
if (isValid(i, d))
document.write(i + " " );
}
}
var L = 410, R = 520, d = 3;
printGoodNumber(L, R, d);
</script>
|
Output
410 420 421 510 520
Time Complexity: O((l-r) * log10 n) , takes O(log10 n) time to check if a number is a good number
Auxiliary Space: O(1)
Last Updated :
09 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...