Dados dos números enteros N y K , la tarea es verificar si N puede representarse como la suma de K números enteros positivos distintos.
Ejemplos:
Entrada: N = 12, K = 4
Salida: Sí
N = 1 + 2 + 4 + 5 = 12 (12 como suma de 4 enteros distintos)Entrada: N = 8, K = 4
Salida: No
Enfoque: considere la serie 1 + 2 + 3 + … + K que tiene exactamente K enteros distintos con la suma mínima posible, es decir, Sum = (K * (K – 1)) / 2 . Ahora, si N < Suma , entonces no es posible representar a N como la suma de K enteros positivos distintos, pero si N ≥ Suma , cualquier entero, digamos X ≥ 0 , se puede agregar a Suma para generar la suma igual a N , es decir , 1 + 2. + 3 + … + (K – 1) + (K + X) asegurando que haya exactamente K enteros positivos distintos.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <iostream> using namespace std; // Function that returns true if n // can be represented as the sum of // exactly k distinct positive integers bool solve(int n, int k) { // If n can be represented as // 1 + 2 + 3 + ... + (k - 1) + (k + x) if (n >= (k * (k + 1)) / 2) { return true; } return false; } // Driver code int main() { int n = 12, k = 4; if (solve(n, k)) cout << "Yes"; else cout << "No"; return 0; }
Java
// Java implementation of the approach import java.io.*; class GFG { // Function that returns true if n // can be represented as the sum of // exactly k distinct positive integers static boolean solve(int n, int k) { // If n can be represented as // 1 + 2 + 3 + ... + (k - 1) + (k + x) if (n >= (k * (k + 1)) / 2) { return true; } return false; } // Driver code public static void main(String[] args) { int n = 12, k = 4; if (solve(n, k)) System.out.println("Yes"); else System.out.println("No"); } } // This code is contributed by anuj_67..
Python3
# Python 3 implementation of the approach # Function that returns true if n # can be represented as the sum of # exactly k distinct positive integers def solve(n,k): # If n can be represented as # 1 + 2 + 3 + ... + (k - 1) + (k + x) if (n >= (k * (k + 1)) // 2): return True return False # Driver code if __name__ == '__main__': n = 12 k = 4 if (solve(n, k)): print("Yes") else: print("No") # This code is contributed by # Surendra_Gangwar
C#
// C# implementation of the approach using System; class GFG { // Function that returns true if n // can be represented as the sum of // exactly k distinct positive integers static bool solve(int n, int k) { // If n can be represented as // 1 + 2 + 3 + ... + (k - 1) + (k + x) if (n >= (k * (k + 1)) / 2) { return true; } return false; } // Driver code static public void Main () { int n = 12, k = 4; if (solve(n, k)) Console.WriteLine("Yes"); else Console.WriteLine("No"); } } // This code is contributed by ajit.
PHP
<?php // PHP implementation of the approach // Function that returns true if n // can be represented as the sum of // exactly k distinct positive integers function solve($n, $k) { // If n can be represented as // 1 + 2 + 3 + ... + (k - 1) + (k + x) if ($n >= ($k * ($k + 1)) / 2) { return true; } return false; } // Driver code $n = 12; $k = 4; if (solve($n, $k)) echo "Yes"; else echo "No"; // This code is contributed by ihritik ?>
Javascript
<script> // Javascript implementation of the approach // Function that returns true if n // can be represented as the sum of // exactly k distinct positive integers function solve(n, k) { // If n can be represented as // 1 + 2 + 3 + ... + (k - 1) + (k + x) if (n >= (k * (k + 1)) / 2) { return true; } return false; } // Driver code var n = 12, k = 4; if (solve(n, k)) document.write("Yes"); else document.write("No"); // This code is contributed by todaysgaurav </script>
Yes
Complejidad de tiempo: O(1)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por andrew1234 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA