Dados dos enteros N y K , la tarea es encontrar K enteros consecutivos tales que su suma sea N .
Nota: Si no hay tales K enteros imprima -1.
Ejemplos:
Entrada: N = 15, K = 5
Salida: 1 2 3 4 5
Explicación:
N se puede representar como la suma de 5 enteros consecutivos de la siguiente manera –
=> N => 1 + 2 + 3 + 4 + 5 = 15
Entrada: N = 33, K = 6
Salida: 3 4 5 6 7 8
Explicación:
N se puede representar como la suma de 6 enteros consecutivos de la siguiente manera –
=> N => 3 + 4 + 5 + 6 + 7 + 8 = 33
Enfoque ingenuo: una solución simple es ejecutar un bucle de i = 0 a N – (K – 1) para verificar si K enteros consecutivos que comienzan desde i tienen una suma como N.
Enfoque eficiente: la idea es usar la progresión aritmética para resolver este problema, donde la suma de K términos de progresión aritmética con diferencia común es 1, se puede definir de la siguiente manera:
- Suma de K Términos –
=>
- Resolviendo la ecuación aún más para obtener el primer término posible
=>
- Aquí a K es el K -ésimo término que se puede escribir como 1 + K – 1
=>
=>
- Finalmente, verifique que el primer término calculado sea un número entero, si es así, entonces existe K número consecutivo cuya suma es N.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation to check if // a number can be expressed as // sum of K consecutive integer #include <bits/stdc++.h> using namespace std; // Function to check if a number can be // expressed as the sum of k consecutive void checksum(int n, int k) { // Finding the first // term of AP float first_term = ((2 * n) / k + (1 - k)) / 2.0; // Checking if first // term is an integer if (first_term - int(first_term) == 0) { // Loop to print the K // consecutive integers for (int i = first_term; i <= first_term + k - 1; i++) { cout << i << " "; } } else cout << "-1"; } // Driver Code int main() { int n = 33, k = 6; checksum(n, k); return 0; }
Java
// Java implementation to check if // a number can be expressed as // sum of K consecutive integer class GFG{ // Function to check if a number can be // expressed as the sum of k consecutive static void checksum(int n, int k) { // Finding the first // term of AP float first_term = (float) (((2 * n) / k + (1 - k)) / 2.0); // Checking if first // term is an integer if (first_term - (int)(first_term) == 0) { // Loop to print the K // consecutive integers for(int i = (int)first_term; i <= first_term + k - 1; i++) { System.out.print(i + " "); } } else System.out.print("-1"); } // Driver Code public static void main(String[] args) { int n = 33, k = 6; checksum(n, k); } } // This code is contributed by 29AjayKumar
Python3
# Python3 implementation to check # if a number can be expressed as # sum of K consecutive integer # Function to check if a number can be # expressed as the sum of k consecutive def checksum(n, k): # Finding the first # term of AP first_term = ((2 * n) / k + (1 - k)) / 2.0 # Checking if first # term is an integer if (first_term - int(first_term) == 0): # Loop to print the K # consecutive integers for i in range(int(first_term), int(first_term) + k): print(i, end = ' ') else: print('-1') # Driver Code if __name__=='__main__': (n, k) = (33, 6) checksum(n, k) # This code is contributed by rutvik_56
C#
// C# implementation to check if // a number can be expressed as // sum of K consecutive integer using System; class GFG{ // Function to check if a number can be // expressed as the sum of k consecutive static void checksum(int n, int k) { // Finding the first // term of AP float first_term = (float)(((2 * n) / k + (1 - k)) / 2.0); // Checking if first // term is an integer if (first_term - (int)(first_term) == 0) { // Loop to print the K // consecutive integers for(int i = (int)first_term; i <= first_term + k - 1; i++) { Console.Write(i + " "); } } else Console.Write("-1"); } // Driver Code public static void Main(String[] args) { int n = 33, k = 6; checksum(n, k); } } // This code is contributed by sapnasingh4991
Javascript
<script> // javascript implementation to check if // a number can be expressed as // sum of K consecutive integer // Function to check if a number can be // expressed as the sum of k consecutive function checksum(n , k) { // Finding the first // term of AP var first_term = (((2 * n) / k + (1 - k)) / 2.0); // Checking if first // term is an integer if (first_term - parseInt( (first_term)) == 0) { // Loop to print the K // consecutive integers for (i = parseInt( first_term); i <= first_term + k - 1; i++) { document.write(i + " "); } } else document.write("-1"); } // Driver Code var n = 33, k = 6; checksum(n, k); // This code contributed by Rajput-Ji </script>
3 4 5 6 7 8