Dados dos enteros positivos N y M, la tarea es encontrar el M-ésimo número cuya suma de dígitos de un número hasta que la suma se convierte en un solo dígito es N.
Ejemplos:
Input: N = 1, M = 3 Output: 19 The first two numbers being 1 and 9. Input: N = 2, M = 5 Output: 38 The first four numbers being 2, 11, 20 and 29.
Un enfoque ingenuo es iterar para todos los números y mantener un conteo de números cuya suma devuelva N.
Un enfoque eficiente es encontrar la suma de dígitos hasta que se convierta en un solo dígito en O(1) que se ha discutido aquí . Por lo tanto, la fórmula para encontrar el M-ésimo número será:
M número : (M-1)*9 + N
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to Find m-th number whose // sum of digits of a number until // sum becomes single digit is N #include <bits/stdc++.h> using namespace std; // Function to find the M-th // number whosesum till one digit is N int findNumber(int n, int m) { int num = (m - 1) * 9 + n; return num; } // Driver Code int main() { int n = 2, m = 5; cout << findNumber(n, m); return 0; }
Java
// Java program to Find m-th number whose // sum of digits of a number until // sum becomes single digit is N class GFG { // Function to find the M-th // number whosesum till one digit is N static int findNumber(int n, int m) { int num = (m - 1) * 9 + n; return num; } // Driver Code public static void main(String args[]) { int n = 2, m = 5; System.out.print(findNumber(n, m)); } } // This code is contributed // by Akanksha Rai
Python3
# Python3 program to Find m-th number # whose sum of digits of a number # until sum becomes single digit is N # Function to find the M-th # number whosesum till one digit is N def findNumber(n, m) : num = (m - 1) * 9 + n; return num; # Driver Code if __name__ == "__main__" : n = 2 ; m = 5 ; print(findNumber(n, m)) # This code is contributed by Ryuga
C#
// C# program to Find m-th number whose // sum of digits of a number until // sum becomes single digit is N using System; class GFG { // Function to find the M-th // number whosesum till one digit is N static int findNumber(int n, int m) { int num = (m - 1) * 9 + n; return num; } // Driver Code public static void Main() { int n = 2, m = 5; Console.Write(findNumber(n, m)); } } // This code is contributed // by Akanksha Rai
PHP
<?php // PHP program to Find m-th number whose // sum of digits of a number until // sum becomes single digit is N // number whosesum till one digit is N function findNumber($n, $m) { $num = ($m - 1) * 9 + $n; return $num; } // Driver Code $n = 2; $m = 5; echo findNumber($n, $m); // This code is contributed // by Akanksha Rai ?>
Javascript
<script> // JavaScript program to Find m-th number whose // sum of digits of a number until // sum becomes single digit is N // Function to find the M-th // number whosesum till one digit is N function findNumber(n , m) { var num = (m - 1) * 9 + n; return num; } // Driver Code var n = 2, m = 5; document.write(findNumber(n, m)); // This code contributed by Rajput-Ji </script>
38
Complejidad de tiempo: O (1), ya que estamos haciendo operaciones de tiempo constante sin usar bucles ni recursividad.
Espacio auxiliar: O(1), ya que no estamos utilizando ningún espacio adicional.