Given a positive integer N and a digit D, the task is to check if N can be represented as a sum of positive integers containing the digit D at least once. If it is possible to represent N in such a format, then print “Yes”. Otherwise, print “No”.
Examples:
Input: N = 24, D = 7
Output: Yes
Explanation: The value 24 can be represented as 17 + 7, both containing the digit 7.
Input: N = 27 D = 2
Output: Yes
Approach 1:
Follow the steps to solve the problem:
- Check if the given N contains digit D or not. If found to be true, then print “Yes”.
- Otherwise, iterate until N is greater than 0 and perform the following steps:
- After completing the above steps, if none of the above conditions satisfies, then print “No”.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
bool findDigit( int N, int D)
{
while (N > 0) {
int a = N % 10;
if (a == D) {
return true ;
}
N /= 10;
}
return false ;
}
bool check( int N, int D)
{
while (N > 0) {
if (findDigit(N, D) == true ) {
return true ;
}
N -= D;
}
return false ;
}
int main()
{
int N = 24;
int D = 7;
if (check(N, D)) {
cout << "Yes" ;
}
else {
cout << "No" ;
}
return 0;
}
|
Java
import java.util.*;
class GFG{
static boolean findDigit( int N, int D)
{
while (N > 0 )
{
int a = N % 10 ;
if (a == D)
{
return true ;
}
N /= 10 ;
}
return false ;
}
static boolean check( int N, int D)
{
while (N > 0 )
{
if (findDigit(N, D) == true )
{
return true ;
}
N -= D;
}
return false ;
}
public static void main(String[] args)
{
int N = 24 ;
int D = 7 ;
if (check(N, D))
{
System.out.print( "Yes" );
}
else
{
System.out.print( "No" );
}
}
}
|
Python3
def findDigit(N, D):
while (N > 0 ):
a = N % 10
if (a = = D):
return True
N / = 10
return False
def check(N, D):
while (N > 0 ):
if (findDigit(N, D) = = True ):
return True
N - = D
return False
if __name__ = = '__main__' :
N = 24
D = 7
if (check(N, D)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG{
static bool findDigit( int N, int D)
{
while (N > 0)
{
int a = N % 10;
if (a == D)
{
return true ;
}
N /= 10;
}
return false ;
}
static bool check( int N, int D)
{
while (N > 0)
{
if (findDigit(N, D) == true )
{
return true ;
}
N -= D;
}
return false ;
}
public static void Main()
{
int N = 24;
int D = 7;
if (check(N, D))
{
Console.WriteLine( "Yes" );
}
else
{
Console.WriteLine( "No" );
}
}
}
|
Javascript
<script>
function findDigit(N, D)
{
while (N > 0)
{
let a = N % 10;
if (a == D)
{
return true ;
}
N = Math.floor(N / 10);
}
return false ;
}
function check(N, D)
{
while (N > 0)
{
if (findDigit(N, D) == true )
{
return true ;
}
N -= D;
}
return false ;
}
let N = 24;
let D = 7;
if (check(N, D))
{
document.write( "Yes" );
}
else
{
document.write( "No" );
}
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Approach 2: Dynamic Programming
We can solve this problem using Dynamic Programming (DP). We can create a DP array of size N+1, where DP[i] stores whether it is possible to represent i as the sum of numbers having the digit D in them or not. We can initialize DP[0] as true since it is always possible to represent 0 as the sum of no numbers.
Then, for each i from 1 to N, we can check if i contains the digit D. If it does, then we can mark DP[i] as true. Otherwise, we can iterate over all possible j < i such that DP[j] is true, and check if i-j contains the digit D. If it does, then we can mark DP[i] as true.
At the end, we can check if DP[N] is true or not. If it is, then we can say that N can be represented as the sum of numbers having the digit D in them. Otherwise, we can say that it is not possible.
C++
#include <cstring>
#include <iostream>
using namespace std;
bool check( int N, int D)
{
bool DP[N + 1];
memset (DP, false , sizeof (DP));
DP[0] = true ;
for ( int i = 1; i <= N; i++) {
if (i % 10 == D || i / 10 == D) {
DP[i] = true ;
continue ;
}
for ( int j = 1; j < i; j++) {
if (DP[j]
&& ((i - j) % 10 == D
|| (i - j) / 10 == D)) {
DP[i] = true ;
break ;
}
}
}
return DP[N];
}
int main()
{
int N = 24;
int D = 7;
if (check(N, D)) {
cout << "Yes" ;
}
else {
cout << "No" ;
}
return 0;
}
|
Java
import java.util.Arrays;
public class Main {
static boolean check( int N, int D) {
boolean [] DP = new boolean [N + 1 ];
Arrays.fill(DP, false );
DP[ 0 ] = true ;
for ( int i = 1 ; i <= N; i++) {
if (i % 10 == D || i / 10 == D) {
DP[i] = true ;
continue ;
}
for ( int j = 1 ; j < i; j++) {
if (DP[j]
&& ((i - j) % 10 == D
|| (i - j) / 10 == D)) {
DP[i] = true ;
break ;
}
}
}
return DP[N];
}
public static void main(String[] args) {
int N = 24 ;
int D = 7 ;
if (check(N, D)) {
System.out.println( "Yes" );
}
else {
System.out.println( "No" );
}
}
}
|
Python3
def check(N, D):
DP = [ False ] * (N + 1 )
DP[ 0 ] = True
for i in range ( 1 , N + 1 ):
if i % 10 = = D or i / / 10 = = D:
DP[i] = True
continue
for j in range ( 1 , i):
if DP[j] and ((i - j) % 10 = = D or (i - j) / / 10 = = D):
DP[i] = True
break
return DP[N]
N = 24
D = 7
if check(N, D):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
namespace ConsoleApp {
class Program {
static bool Check( int N, int D)
{
bool [] DP = new bool [N + 1];
for ( int i = 0; i < DP.Length; i++) {
DP[i] = false ;
}
DP[0] = true ;
for ( int i = 1; i <= N; i++) {
if (i % 10 == D || i / 10 == D) {
DP[i] = true ;
continue ;
}
for ( int j = 1; j < i; j++) {
if (DP[j]
&& ((i - j) % 10 == D
|| (i - j) / 10 == D)) {
DP[i] = true ;
break ;
}
}
}
return DP[N];
}
static void Main( string [] args)
{
int N = 24;
int D = 7;
if (Check(N, D)) {
Console.WriteLine( "Yes" );
}
else {
Console.WriteLine( "No" );
}
Console.ReadKey();
}
}
}
|
Javascript
function check(N, D) {
let DP = new Array(N + 1).fill( false );
DP[0] = true ;
for (let i = 1; i <= N; i++) {
if (i % 10 === D || Math.floor(i / 10) === D) {
DP[i] = true ;
continue ;
}
for (let j = 1; j < i; j++) {
if (DP[j] && ((i - j) % 10 === D || Math.floor((i - j) / 10) === D)) {
DP[i] = true ;
break ;
}
}
}
return DP[N];
}
let N = 24;
let D = 7;
if (check(N, D)) {
console.log( "Yes" );
} else {
console.log( "No" );
}
|
Time Complexity: O(N^(1/3)log(N)), where N is the input number.
Auxiliary Space: O(N^(1/3)), since we are storing all perfect cubes up to N^(1/3) in the map.