Given an integer value n, find out the n-th positive integer whose sum is 10.
Examples:
Input: n = 2
Output: 28
The first number with sum of digits as
10 is 19. Second number is 28.
Input: 15
Output: 154
Method 1 (Simple):
We traverse through all numbers. For every number, we find the sum of digits. We stop when we find the n-th number with the sum of digits as 10.
C++
#include <bits/stdc++.h>
using namespace std;
int findNth( int n)
{
int count = 0;
for ( int curr = 1;; curr++) {
int sum = 0;
for ( int x = curr; x > 0; x = x / 10)
sum = sum + x % 10;
if (sum == 10)
count++;
if (count == n)
return curr;
}
return -1;
}
int main()
{
printf ( "%d\n" , findNth(5));
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
public class GFG {
public static int findNth( int n)
{
int count = 0 ;
for ( int curr = 1 ;; curr++) {
int sum = 0 ;
for ( int x = curr; x > 0 ; x = x / 10 )
sum = sum + x % 10 ;
if (sum == 10 )
count++;
if (count == n)
return curr;
}
}
public static void main(String[] args)
{
System.out.print(findNth( 5 ));
}
}
|
Python3
import itertools
def findNth(n):
count = 0
for curr in itertools.count():
sum = 0
x = curr
while (x):
sum = sum + x % 10
x = x / / 10
if ( sum = = 10 ):
count = count + 1
if (count = = n):
return curr
return - 1
if __name__ = = '__main__' :
print (findNth( 5 ))
|
C#
using System;
class GFG {
public static int findNth( int n)
{
int count = 0;
for ( int curr = 1;; curr++) {
int sum = 0;
for ( int x = curr; x > 0; x = x / 10)
sum = sum + x % 10;
if (sum == 10)
count++;
if (count == n)
return curr;
}
}
static public void Main()
{
Console.WriteLine(findNth(5));
}
}
|
Javascript
<script>
function findNth(n)
{
let count = 0;
for (let curr = 1;; curr++) {
let sum = 0;
for (let x = curr; x > 0; x = Math.floor(x / 10))
sum = sum + x % 10;
if (sum == 10)
count++;
if (count == n)
return curr;
}
return -1;
}
document.write(findNth(5));
</script>
|
PHP
<?php
function findNth( $n )
{
$count = 0;
for ( $curr = 1; ; $curr ++)
{
$sum = 0;
for ( $x = $curr ;
$x > 0; $x = $x / 10)
$sum = $sum + $x % 10;
if ( $sum == 10)
$count ++;
if ( $count == $n )
return $curr ;
}
return -1;
}
echo findNth(5);
?>
|
Time Complexity: O(N*log10(N))
Auxiliary Space: O(1)
Method 2 (Efficient):
If we take a closer look, we can notice that all multiples of 9 are present in arithmetic progression 19, 28, 37, 46, 55, 64, 73, 82, 91, 100, 109,… However, there are numbers in the above series whose sum of digits is not 10, for example, 100. So instead of checking one by one, we start with 19 and increment by 9.
C++
#include <bits/stdc++.h>
using namespace std;
int findNth( int n)
{
int count = 0;
for ( int curr = 19;; curr += 9) {
int sum = 0;
for ( int x = curr; x > 0; x = x / 10)
sum = sum + x % 10;
if (sum == 10)
count++;
if (count == n)
return curr;
}
return -1;
}
int main()
{
printf ( "%d\n" , findNth(5));
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
public class GFG {
public static int findNth( int n)
{
int count = 0 ;
for ( int curr = 19 ;; curr += 9 ) {
int sum = 0 ;
for ( int x = curr; x > 0 ; x = x / 10 )
sum = sum + x % 10 ;
if (sum == 10 )
count++;
if (count == n)
return curr;
}
}
public static void main(String[] args)
{
System.out.print(findNth( 5 ));
}
}
|
Python3
def findNth(n):
count = 0 ;
curr = 19 ;
while ( True ):
sum = 0 ;
x = curr;
while (x > 0 ):
sum = sum + x % 10 ;
x = int (x / 10 );
if ( sum = = 10 ):
count + = 1 ;
if (count = = n):
return curr;
curr + = 9 ;
return - 1 ;
print (findNth( 5 ));
|
C#
using System;
class GFG {
public static int findNth( int n)
{
int count = 0;
for ( int curr = 19;; curr += 9) {
int sum = 0;
for ( int x = curr;
x > 0; x = x / 10)
sum = sum + x % 10;
if (sum == 10)
count++;
if (count == n)
return curr;
}
}
static public void Main()
{
Console.WriteLine(findNth(5));
}
}
|
Javascript
<script>
function findNth(n)
{
let count = 0;
for (let curr = 19; ;curr += 9)
{
let sum = 0;
for (let x = curr; x > 0;
x = parseInt(x / 10))
sum = sum + x % 10;
if (sum == 10)
count++;
if (count == n)
return curr;
}
return -1;
}
document.write(findNth(5));
</script>
|
PHP
<?php
function findNth( $n )
{
$count = 0;
for ( $curr = 19; ; $curr += 9)
{
$sum = 0;
for ( $x = $curr ; $x > 0;
$x = (int) $x / 10)
$sum = $sum + $x % 10;
if ( $sum == 10)
$count ++;
if ( $count == $n )
return $curr ;
}
return -1;
}
echo findNth(5);
?>
|
Time Complexity: O(N*log10(N))
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
17 Aug, 2023
Like Article
Save Article