Given two integers **N** and **K**, the task is to find **K** consecutive integers such that their sum if **N**.

**Note:** If there is no such K integers print -1.

**Examples:**

Input:N = 15, K = 5

Output:1 2 3 4 5

Explanation:

N can be represented as sum of 5 consecutive integers as follows –

=> N => 1 + 2 + 3 + 4 + 5 = 15

Input:N = 33, K = 6

Output:3 4 5 6 7 8

Explanation:

N can be represented as sum of 6 consecutive integers as follows –

=> N => 3 + 4 + 5 + 6 + 7 + 8 = 33

**Naive Approach:** A simple solution is to run a loop from i = 0 to N – (K – 1) to check if K consecutive integers starting from i is having sum as N.

**Efficient Approach:** The idea is to use Arithmetic Progression to solve this problem, where sum of K terms of arithmetic progression with common difference is 1 can be defined as follows –

- Sum of K Terms –

=> - Solving the equation further to get the first term possible

=> - Here a
_{K}is the K^{th}term which can be written as a_{1}+ K – 1

=>

=> - Finally, check the first term computed is an integer, If yes then K consecutive number exists whose sum if N.

Below is the implementation of the above approach:

## 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; ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

**Output:**

3 4 5 6 7 8

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Find the first N integers such that the sum of their digits is equal to 10
- Find any K distinct odd integers such that their sum is equal to N
- Count pairs from 1 to N such that their Sum is divisible by their XOR
- Possible values of Q such that, for any value of R, their product is equal to X times their sum
- Find three integers less than or equal to N such that their LCM is maximum
- Count of pairs in a given range with sum of their product and sum equal to their concatenated number
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Sum of product of all integers upto N with their count of divisors
- Rearrange an Array such that Sum of same-indexed subsets differ from their Sum in the original Array
- Find the pair (a, b) with minimum LCM such that their sum is equal to N
- Check if a number can be written as sum of three consecutive integers
- Number expressed as sum of five consecutive integers
- Remove two consecutive integers from 1 to N to make sum equal to S
- Check if N can be represented as sum of squares of two consecutive integers
- Count integers in a range which are divisible by their euler totient value
- Sort integers in array according to their distance from the element K
- Pair of integers having difference of their fifth power as X
- Find K numbers with sum equal to N and sum of their squares maximized
- Count ordered pairs of positive numbers such that their sum is S and XOR is K
- Print n numbers such that their sum is a perfect square

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.