Un entero positivo se considera un buen número si la suma de sus dígitos es par. Encuentre el n-ésimo número bueno más pequeño.
Ejemplos:
Input : n = 1 Output : 2 First good number is smallest positive number with sum of digits even which is 2. Input : n = 10 Output : 20
Una solución simple es comenzar desde 1 y atravesar todos los números naturales. Para cada número x, verifica si la suma de los dígitos es par. Si incluso incrementa el conteo de buenos números. Finalmente devuelva el n-ésimo número bueno.
Una solución eficiente se basa en un patrón en la respuesta. Hagamos una lista de los primeros 20 buenos números. Los primeros 20 números buenos son: 2, 4, 6, 8, 11, 13, 15, 17, 19, 20, 22, 24, 26, 28, 31, 33, 35, 37, 39, 40. Observa que si el último dígito de n es de 0 a 4, la respuesta es 2*n y si el último dígito de n es de 5 a 9, la respuesta es 2*n + 1.
C++
// C++ program to find n-th // Good number. #include <bits/stdc++.h> using namespace std; // Function to find kth good number. long long int findKthGoodNo(long long int n) { // Find the last digit of n. int lastDig = n % 10; // If last digit is between // 0 to 4 then return 2 * n. if (lastDig >= 0 && lastDig <= 4) return n << 1; // If last digit is between // 5 to 9 then return 2*n + 1. else return (n << 1) + 1; } // Driver code int main() { long long int n = 10; cout << findKthGoodNo(n); return 0; }
Java
// Java program to find n-th // Good number. class GFG { // Function to find kth good number. static int findKthGoodNo(int n) { // Find the last digit of n. int lastDig = n % 10; // If last digit is between // 0 to 4 then return 2*n. if (lastDig >= 0 && lastDig <= 4) return n << 1; // If last digit is between // 5 to 9 then return 2*n + 1. else return (n << 1) + 1; } // Driver code public static void main(String[] args) { int n = 10; System.out.println(findKthGoodNo(n)); } } // This code is contributed by // Smitha Dinesh Semwal
Python 3
# Python 3 program to find # n-th Good number. # Function to find kth # good number. def findKthGoodNo(n): # Find the last digit of n. lastDig = n % 10 # If last digit is between # 0 to 4 then return 2 * n. if (lastDig >= 0 and lastDig <= 4) : return n << 1 # If last digit is between # 5 to 9 then return 2 * n + 1. else: return (n << 1) + 1 # Driver code n = 10 print(findKthGoodNo(n)) # This code is contributed by # Smitha Dinesh Semwal
C#
// C# program to find n-th // Good number. using System; class GFG { // Function to find kth // good number public static int findKthGoodNo(int n) { // Find the last digit of n. int lastDig = n % 10; // If last digit is between // 0 to 4 then return 2*n. if (lastDig >= 0 && lastDig <= 4) return n << 1; // If last digit is between // 5 to 9 then return 2*n + 1. else return (n << 1) + 1; } // Driver code static public void Main (string []args) { int n = 10; Console.WriteLine(findKthGoodNo(n)); } } // This code is contributed by Ajit.
PHP
<?php // PHP program to find n-th // Good number. // Function to find kth // good number. function findKthGoodNo($n) { // Find the last digit of n. $lastDig = $n % 10; // If last digit is between // 0 to 4 then return 2*n. if ($lastDig >= 0 && $lastDig <= 4) return $n << 1; // If last digit is between // 5 to 9 then return 2*n + 1. else return ($n << 1) + 1; } // Driver code $n = 10; echo(findKthGoodNo($n)); // This code is contributed by Ajit. ?>
Javascript
<script> // JavaScript program to find n-th // Good number. // Function to find kth good number. function findKthGoodNo(n) { // Find the last digit of n. let lastDig = n % 10; // If last digit is between // 0 to 4 then return 2*n. if (lastDig >= 0 && lastDig <= 4) return n << 1; // If last digit is between // 5 to 9 then return 2*n + 1. else return (n << 1) + 1; } // Driver code let n = 10; document.write(findKthGoodNo(n)); // This code is contributed by souravghosh0416. </script>
20
Tiempo Complejidad: O(1)
Espacio Auxiliar: O(1)