Append X digits to the end of N to make it divisible by M
Last Updated :
27 Apr, 2021
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++
#include <bits/stdc++.h>
using namespace std;
bool isDiv( int N, int X, int M, int & res)
{
if (X == 0) {
if (N % M == 0) {
res = N;
return true ;
}
return false ;
}
for ( int i = 0; i <= 9; i++) {
if (isDiv(N * 10 + i, X - 1, M, res)) {
return true ;
}
}
}
int main()
{
int N = 4, M = 50, X = 2;
int res = -1;
isDiv(N, X, M, res);
cout << res;
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
static int isDiv( int N, int X, int M, int res)
{
if (X == 0 )
{
if (N % M == 0 )
{
res = N;
return res;
}
return res;
}
for ( int i = 0 ; i < 9 ; i++)
{
int temp = isDiv(N * 10 + i, X - 1 , M, res);
if (temp != - 1 )
{
return temp;
}
}
return res;
}
public static void main(String[] args)
throws java.lang.Exception
{
int N = 4 , M = 50 , X = 2 ;
int res = - 1 ;
res = isDiv(N, X, M, res);
System.out.println(res);
}
}
|
Python3
res = - 1
def isDiv(N, X, M):
if (X = = 0 ):
if (N % M = = 0 ):
global res
res = N
return True
return False
for i in range ( 10 ):
if (isDiv(N * 10 + i, X - 1 , M)):
return True
if __name__ = = "__main__" :
N, M, X = 4 , 50 , 2
if (isDiv(N, X, M)):
print (res)
else :
print ( "-1" )
|
C#
using System;
class GFG
{
static int isDiv( int N, int X, int M, int res)
{
if (X == 0)
{
if (N % M == 0)
{
res = N;
return res;
}
return res;
}
for ( int i = 0; i < 9; i++)
{
int temp = isDiv(N * 10 + i, X - 1, M, res);
if (temp != -1)
{
return temp;
}
}
return res;
}
public static void Main()
{
int N = 4, M = 50, X = 2;
int res = -1;
res = isDiv(N, X, M, res);
Console.WriteLine(res);
}
}
|
Javascript
<script>
var res = -1;
function isDiv(N, X, M)
{
if (X == 0) {
if (N % M == 0) {
res = N;
return true ;
}
return false ;
}
for ( var i = 0; i <= 9; i++) {
if (isDiv(N * 10 + i, X - 1, M)) {
return true ;
}
}
}
var N = 4, M = 50, X = 2;
isDiv(N, X, M, res);
document.write(res);
</script>
|
Time Complexity: O(10X)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...