Given three positive integers N, M, and X, the task is to generate a number by appending X digits on the right side of N such that the number is divisible by M. If multiple solutions exist, then print any of them. Otherwise, print -1.
Examples:
Input: N = 10, M = 5, X = 4
Output: 105555
Explanation: One of possible values of N (= 10) by appending X(= 4) digits on the right side of N is 105555, which is divisible by M (= 5).Input: N = 4, M = 50, X = 2
Output: 400
Approach: The idea is to append X digits on the right side of N by trying out all possible digits from the range [0, 9] and after appending X digits on the right side of N, check if the number is divisible by M or not. If found to be true, then print the number. Otherwise, print -1. Following are the recurrence relation:
Follow the steps below to solve the problem:
- Use the above recurrence relation, check if the number N is divisible by M or not by appending X digits on the right side of N. If found to be true, then print the value of N by appending X digits.
- Otherwise, print -1.
Below is the implementation of the above approach :
// C++ program to implement // the above approach #include <bits/stdc++.h> using namespace std;
// Function to check if the value of N by // appending X digits on right side of N // is divisible by M or not bool isDiv( int N, int X, int M, int & res)
{ // Base Case
if (X == 0) {
// If N is divisible
// by M
if (N % M == 0) {
// Update res
res = N;
return true ;
}
return false ;
}
// Iterate over the range [0, 9]
for ( int i = 0; i <= 9; i++) {
// If N is divisible by M by
// appending X digits
if (isDiv(N * 10 + i, X - 1, M, res)) {
return true ;
}
}
} // Driver Code int main()
{ int N = 4, M = 50, X = 2;
// Stores the number by appending
// X digits on the right side of N
int res = -1;
isDiv(N, X, M, res);
cout << res;
} |
// Java program for the above approach import java.io.*;
import java.util.*;
class GFG
{ // Function to check if the value of N by
// appending X digits on right side of N
// is divisible by M or not
static int isDiv( int N, int X, int M, int res)
{
// Base Case
if (X == 0 )
{
// If N is divisible
// by M
if (N % M == 0 )
{
// Update res
res = N;
return res;
}
return res;
}
// Iterate over the range [0, 9]
for ( int i = 0 ; i < 9 ; i++)
{
// If N is divisible by M by
// appending X digits
int temp = isDiv(N * 10 + i, X - 1 , M, res);
if (temp != - 1 )
{
return temp;
}
}
return res;
}
// Driver code
public static void main(String[] args)
throws java.lang.Exception
{
int N = 4 , M = 50 , X = 2 ;
// Stores the number by appending
// X digits on the right side of N
int res = - 1 ;
res = isDiv(N, X, M, res);
System.out.println(res);
}
} // This code is contributed by 18bhupenderyadav18. |
# Python3 program to implement # the above approach # Function to check if the value of N by # appending X digits on right side of N # is divisible by M or not # global variable to store result res = - 1
def isDiv(N, X, M):
# Base case
if (X = = 0 ):
# If N is divisible
# by M
if (N % M = = 0 ):
global res
res = N
return True
return False
# Iterate over the range [0, 9]
for i in range ( 10 ):
# if N is Divisible by M upon appending X digits
if (isDiv(N * 10 + i, X - 1 , M)):
return True
# Driver Code if __name__ = = "__main__" :
N, M, X = 4 , 50 , 2
if (isDiv(N, X, M)):
print (res)
else :
print ( "-1" )
|
// C# program to implement // the above approach using System;
class GFG
{ // Function to check if the value of N by
// appending X digits on right side of N
// is divisible by M or not
static int isDiv( int N, int X, int M, int res)
{
// Base Case
if (X == 0)
{
// If N is divisible
// by M
if (N % M == 0)
{
// Update res
res = N;
return res;
}
return res;
}
// Iterate over the range [0, 9]
for ( int i = 0; i < 9; i++)
{
// If N is divisible by M by
// appending X digits
int temp = isDiv(N * 10 + i, X - 1, M, res);
if (temp != -1)
{
return temp;
}
}
return res;
}
// Driver code
public static void Main()
{
int N = 4, M = 50, X = 2;
// Stores the number by appending
// X digits on the right side of N
int res = -1;
res = isDiv(N, X, M, res);
Console.WriteLine(res);
}
} // This code is contributed by sanjoy_62 |
<script> // Javascript program to implement // the above approach var res = -1;
// Function to check if the value of N by // appending X digits on right side of N // is divisible by M or not function isDiv(N, X, M)
{ // Base Case
if (X == 0) {
// If N is divisible
// by M
if (N % M == 0) {
// Update res
res = N;
return true ;
}
return false ;
}
// Iterate over the range [0, 9]
for ( var i = 0; i <= 9; i++) {
// If N is divisible by M by
// appending X digits
if (isDiv(N * 10 + i, X - 1, M)) {
return true ;
}
}
} // Driver Code var N = 4, M = 50, X = 2;
// Stores the number by appending // X digits on the right side of N isDiv(N, X, M, res); document.write(res); </script> |
Output
400
Time Complexity: O(10X)
Auxiliary Space: O(1)