Given two integers N and K, the task is to print all the numbers from the range [1, N] whose product of digits is equal to K. If no such number is found, then print “-1”.
Examples:
Input: N =100, K = 25
Output: 55
Explanation: There is only a single number 55 whose product of digits is equal to K.
Input: N = 500, K = 10
Output: 25 52 125 152 215 251
Approach: Follow the steps below to solve the problem:
- Initialize a variable, say flag, to store whether any number satisfying the given conditions exists or not.
- Declare a function, productOfDigits(), to find the product of digits of the number.
- Iterate over the range [1, N]:
- If the product of digits of arr[i] is equal to K, print that number and set flag = 1.
- If flag is equal to 0, which means that no such number is found in the range [1, N]. Therefore, print “-1”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int productOfDigits( int N)
{
int product = 1;
while (N != 0) {
product = product * (N % 10);
N = N / 10;
}
return product;
}
void productOfDigitsK( int N, int K)
{
int flag = 0;
for ( int i = 1; i <= N; ++i) {
if (K == productOfDigits(i)) {
cout << i << " " ;
flag = 1;
}
}
if (flag == 0)
cout << "-1" ;
}
int main()
{
int N = 500, K = 10;
productOfDigitsK(N, K);
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static int productOfDigits( int N)
{
int product = 1 ;
while (N != 0 ) {
product = product * (N % 10 );
N = N / 10 ;
}
return product;
}
static void productOfDigitsK( int N, int K)
{
int flag = 0 ;
for ( int i = 1 ; i <= N; ++i) {
if (K == productOfDigits(i)) {
System.out.print(i + " " );
flag = 1 ;
}
}
if (flag == 0 )
System.out.println(- 1 );
}
public static void main(String[] args)
{
int N = 500 , K = 10 ;
productOfDigitsK(N, K);
}
}
|
Python3
def productOfDigits(N) :
product = 1 ;
while (N ! = 0 ) :
product = product * (N % 10 );
N = N / / 10 ;
return product;
def productOfDigitsK(N, K) :
flag = 0 ;
for i in range ( 1 , N + 1 ) :
if (K = = productOfDigits(i)) :
print (i, end = " " );
flag = 1 ;
if (flag = = 0 ) :
print ( "-1" );
if __name__ = = "__main__" :
N = 500 ; K = 10 ;
productOfDigitsK(N, K);
|
C#
using System;
class GFG
{
static int productOfDigits( int N)
{
int product = 1;
while (N != 0) {
product = product * (N % 10);
N = N / 10;
}
return product;
}
static void productOfDigitsK( int N, int K)
{
int flag = 0;
for ( int i = 1; i <= N; ++i) {
if (K == productOfDigits(i)) {
Console.Write(i + " " );
flag = 1;
}
}
if (flag == 0)
Console.WriteLine(-1);
}
static public void Main()
{
int N = 500, K = 10;
productOfDigitsK(N, K);
}
}
|
Javascript
<script>
function productOfDigits(N)
{
let product = 1;
while (N != 0) {
product = product * (N % 10);
N = Math.floor(N / 10);
}
return product;
}
function productOfDigitsK(N, K)
{
let flag = 0;
for (let i = 1; i <= N; ++i) {
if (K == productOfDigits(i)) {
document.write(i + " " );
flag = 1;
}
}
if (flag == 0)
document.write(-1);
}
let N = 500, K = 10;
productOfDigitsK(N, K);
</script>
|
Output
25 52 125 152 215 251
Time Complexity: O(N * logN)
Auxiliary Space: O(1)