Smallest number whose product with N has sum of digits equal to that of N
Last Updated :
17 Aug, 2021
Given an integer N, the task is to find the smallest positive integer, which when multiplied by N, has sum of digits equal to the sum of digits of N.
Examples:
Input: N = 4
Output: 28
Explanation:
- Sum of digits of N = 4
- 4 * 28 = 112
- Sum of digits = 1 + 1 + 2 = 4, which is equal to sum of digits of N.
Input: N = 3029
Output: 37
Explanation:
- Sum of digits of N = 3 + 0 + 2 + 9 = 14
- 3029 * 37 = 112073
- Sum of digits = 1 + 1 + 2 + 0 + 7 + 3 = 14, which is equal to sum of digits of N.
Approach: Follow the steps to solve the problem:
- Since N can be large, take the input of N as a string. Calculate the sum of digits of N and store it in a variable, say S.
- Since the answer needs to exceed 10, starting from number 11, multiply it with N and store it in a variable, say res.
- Calculate the sum of digits of res and check if the sum of digits of res is equal to the S or not. If found to be true, then print the integer.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
void find_num(string n)
{
int ans = 0;
int sumOfDigitsN = 0;
for ( int c = 0; c < n.length(); c++)
{
sumOfDigitsN += n - '0' ;
}
int x=11;
while ( true )
{
int newNum = x * stoi(n);
int tempSumDigits = 0;
string temp = to_string(newNum);
for ( int c = 0; c < temp.length(); c++)
{
tempSumDigits += temp - '0' ;
}
if (tempSumDigits == sumOfDigitsN)
{
ans = x;
break ;
}
x++;
}
cout << ans << endl;
}
int main()
{
string N = "3029" ;
find_num(N);
return 0;
}
|
Java
class GFG {
static void find_num(String n)
{
int ans = 0 ;
char [] digitsOfN
= n.toCharArray();
int sumOfDigitsN = 0 ;
for ( char c : digitsOfN) {
sumOfDigitsN
+= Integer.parseInt(
Character.toString(c));
}
for ( int x = 11 ; x > 0 ; x++) {
int newNum
= x * Integer.parseInt(n);
int tempSumDigits = 0 ;
char [] temp
= Integer.toString(
newNum)
.toCharArray();
for ( char c : temp) {
tempSumDigits
+= Integer.parseInt(
Character.toString(c));
}
if (tempSumDigits == sumOfDigitsN) {
ans = x;
break ;
}
}
System.out.println(ans);
}
public static void main(String[] args)
{
String N = "3029" ;
find_num(N);
}
}
|
Python3
def find_num(n):
ans = 0
digitsOfN = str (n)
sumOfDigitsN = 0
for c in digitsOfN:
sumOfDigitsN + = int (c)
for x in range ( 11 , 50 ):
newNum = x * int (n)
tempSumDigits = 0
temp = str (newNum)
for c in temp:
tempSumDigits + = int (c)
if (tempSumDigits = = sumOfDigitsN):
ans = x
break
print (ans)
if __name__ = = '__main__' :
N = "3029"
find_num(N)
|
C#
using System;
using System.Globalization;
class GFG{
static void find_num( string n)
{
int ans = 0;
char [] digitsOfN = n.ToCharArray();
int sumOfDigitsN = 0;
foreach ( char c in digitsOfN)
{
sumOfDigitsN += Int32.Parse(
Char.ToString(c));
}
for ( int x = 11; x > 0; x++)
{
int newNum = x * Int32.Parse(n);
int tempSumDigits = 0;
string str = newNum.ToString();
char [] temp = str.ToCharArray();
foreach ( char c in temp)
{
tempSumDigits += Int32.Parse(
Char.ToString(c));
}
if (tempSumDigits == sumOfDigitsN)
{
ans = x;
break ;
}
}
Console.WriteLine(ans);
}
public static void Main()
{
string N = "3029" ;
find_num(N);
}
}
|
Javascript
<script>
function find_num(n)
{
var ans = 0;
var sumOfDigitsN = 0;
for ( var c = 0; c < n.length; c++)
{
sumOfDigitsN += n - '0' ;
}
var x=11;
while ( true )
{
var newNum = x * parseInt(n);
var tempSumDigits = 0;
var temp = (newNum.toString());
for ( var c = 0; c < temp.length; c++)
{
tempSumDigits += temp - '0' ;
}
if (tempSumDigits == sumOfDigitsN)
{
ans = x;
break ;
}
x++;
}
document.write( ans );
}
var N = "3029" ;
find_num(N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(log N)
Share your thoughts in the comments
Please Login to comment...