Given two integers X and Y, the task is to find the smallest number greater than or equal to X whose sum of digits is divisible by Y.
Note: 1 <= X <= 1000, 1 <= Y <= 50.
Examples:
Input: X = 10, Y = 5
Output: 14
Explanation:
14 is the smallest number greater than 10 whose sum of digits (1+4 = 5) is divisible by 5.
Input: X = 5923, Y = 13
Output: 5939
Approach: The idea for this problem is to run a loop from X and check for each integer if its sum of digits is divisible by Y or not. Return the first number whose sum of digits is divisible by Y. Given the constraints of X and Y, the answer always exist.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define MAXN 10000000
int sumOfDigits( int n)
{
int sum = 0;
while (n > 0) {
sum += n % 10;
n /= 10;
}
return sum;
}
int smallestNum( int X, int Y)
{
int res = -1;
for ( int i = X; i < MAXN; i++) {
int sum_of_digit = sumOfDigits(i);
if (sum_of_digit % Y == 0) {
res = i;
break ;
}
}
return res;
}
int main()
{
int X = 5923, Y = 13;
cout << smallestNum(X, Y);
return 0;
}
|
C
#include <stdio.h>
#define MAXN 10000000
int sumOfDigits( int n)
{
int sum = 0;
while (n > 0) {
sum += n % 10;
n /= 10;
}
return sum;
}
int smallestNum( int X, int Y)
{
int res = -1;
for ( int i = X; i < MAXN; i++) {
int sum_of_digit = sumOfDigits(i);
if (sum_of_digit % Y == 0) {
res = i;
break ;
}
}
return res;
}
int main()
{
int X = 5923, Y = 13;
printf ( "%d" , smallestNum(X, Y));
return 0;
}
|
Java
class GFG{
static final int MAXN = 10000000 ;
static int sumOfDigits( int n)
{
int sum = 0 ;
while (n > 0 )
{
sum += n % 10 ;
n /= 10 ;
}
return sum;
}
static int smallestNum( int X, int Y)
{
int res = - 1 ;
for ( int i = X; i < MAXN; i++)
{
int sum_of_digit = sumOfDigits(i);
if (sum_of_digit % Y == 0 )
{
res = i;
break ;
}
}
return res;
}
public static void main(String[] args)
{
int X = 5923 , Y = 13 ;
System.out.print(smallestNum(X, Y));
}
}
|
Python3
MAXN = 10000000
def sumOfDigits(n):
sum = 0
while (n > 0 ):
sum + = n % 10
n / / = 10
return sum
def smallestNum(X, Y):
res = - 1 ;
for i in range (X, MAXN):
sum_of_digit = sumOfDigits(i)
if sum_of_digit % Y = = 0 :
res = i
break
return res
if __name__ = = '__main__' :
(X, Y) = ( 5923 , 13 )
print (smallestNum(X, Y))
|
C#
using System;
class GFG{
static readonly int MAXN = 10000000;
static int sumOfDigits( int n)
{
int sum = 0;
while (n > 0)
{
sum += n % 10;
n /= 10;
}
return sum;
}
static int smallestNum( int X, int Y)
{
int res = -1;
for ( int i = X; i < MAXN; i++)
{
int sum_of_digit = sumOfDigits(i);
if (sum_of_digit % Y == 0)
{
res = i;
break ;
}
}
return res;
}
public static void Main(String[] args)
{
int X = 5923, Y = 13;
Console.Write(smallestNum(X, Y));
}
}
|
Javascript
<script>
var MAXN = 10000000;
function sumOfDigits(n)
{
var sum = 0;
while (n > 0)
{
sum += n % 10;
n = parseInt(n/10);
}
return sum;
}
function smallestNum(X , Y)
{
var res = -1;
for (i = X; i < MAXN; i++)
{
var sum_of_digit = sumOfDigits(i);
if (sum_of_digit % Y == 0)
{
res = i;
break ;
}
}
return res;
}
var X = 5923, Y = 13;
document.write(smallestNum(X, Y));
</script>
|
Time Complexity: O(MAXN)
Auxiliary Space: O(1)