Smallest integer with digit sum M and multiple of N | Java implementation of the above approach ; Function to return digit sum ; Function to find out the smallest integer ; Start of the iterator ( Smallest multiple of n ) ; Driver code
class GFG { static int digitSum ( int n ) { int ans = 0 ; while ( n != 0 ) { ans += n % 10 ; n /= 10 ; } return ans ; } static int findInt ( int n , int m ) { int minDigit = ( int ) Math . floor ( ( double ) ( m / 9 ) ) ; int start = ( int ) Math . pow ( 10 , minDigit ) - ( int ) Math . pow ( 10 , minDigit ) % n ; while ( start < Integer . MAX_VALUE ) { if ( digitSum ( start ) == m ) return start ; else start += n ; } return - 1 ; } static public void main ( String args [ ] ) { int n = 13 , m = 32 ; System . out . print ( findInt ( n , m ) ) ; } }