Given an integer **N**, our task is to check if **N** can be divided into **K** consecutive elements with a sum equal to N. Print **-1** if it is not possible to divide in this manner, otherwise print the value **K**.**Examples:**

Input:N = 12Output:3Explanation:

The integer N = 12 can be divided into 3 consecutive elements {3, 4, 5} where 3 + 4 + 5 = 12.

Input:N = 8Output:-1Explanation:

No such division of integer 8 is possible.

**Approach:** To solve the problem mentioned above let us divide the integer **N** into *i* consecutive numbers. The terms of the sequence will look like **(d+1), (d+2), (d+3)…..(d+i)** where d is the common difference present in each of the integers and the sum of this sequence should be equal to **N**.

So, the sum of these number can also be expressed as:

As the **sum = i * (i + 1) / 2** grows quadratically, we have, **N – sum = i * d**. Hence, for a solution to exist, the number of integers should evenly divide the quantity **N – sum.** Below are the steps:

- Iterate from index(say
**i**) from 2. - Find the sum of first i numbers(say
**sum**). - For any iteration if
**(N – sum)**is divisible by i then print that value of**i**. - For any iteration if N exceeds the sum then print
**“-1”**.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the K consecutive ` `// elements with a sum equal to N ` `void` `canBreakN(` `long` `long` `n) ` `{ ` ` ` `// Iterate over [2, INF] ` ` ` `for` `(` `long` `long` `i = 2;; i++) { ` ` ` ` ` `// Store the sum ` ` ` `long` `long` `m = i * (i + 1) / 2; ` ` ` ` ` `// If the sum exceeds N ` ` ` `// then break the loop ` ` ` `if` `(m > n) ` ` ` `break` `; ` ` ` ` ` `long` `long` `k = n - m; ` ` ` ` ` `// Common difference should be ` ` ` `// divisible by number of terms ` ` ` `if` `(k % i) ` ` ` `continue` `; ` ` ` ` ` `// Print value of i & return ` ` ` `cout << i << endl; ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// Print "-1" if not possible ` ` ` `// to break N ` ` ` `cout << ` `"-1"` `; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `// Given N ` ` ` `long` `long` `N = 12; ` ` ` ` ` `// Function Call ` ` ` `canBreakN(N); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program for the above approach ` `class` `GFG{ ` ` ` `// Function to find the K consecutive ` `// elements with a sum equal to N ` `public` `static` `void` `canBreakN(` `long` `n) ` `{ ` ` ` ` ` `// Iterate over [2, INF] ` ` ` `for` `(` `long` `i = ` `2` `;; i++) ` ` ` `{ ` ` ` ` ` `// Store the sum ` ` ` `long` `m = i * (i + ` `1` `) / ` `2` `; ` ` ` ` ` `// If the sum exceeds N ` ` ` `// then break the loop ` ` ` `if` `(m > n) ` ` ` `break` `; ` ` ` ` ` `long` `k = n - m; ` ` ` ` ` `// Common difference should be ` ` ` `// divisible by number of terms ` ` ` `if` `(k % i != ` `0` `) ` ` ` `continue` `; ` ` ` ` ` `// Print value of i & return ` ` ` `System.out.println(i); ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// Print "-1" if not possible ` ` ` `// to break N ` ` ` `System.out.println(` `"-1"` `); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` ` ` `// Given N ` ` ` `long` `N = ` `12` `; ` ` ` ` ` `// Function call ` ` ` `canBreakN(N); ` `} ` `} ` ` ` `// This code is contributed by jrishabh99 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program for the above approach ` ` ` `# Function to find the K consecutive ` `# elements with a sum equal to N ` `def` `canBreakN(n): ` ` ` ` ` `# Iterate over [2, INF] ` ` ` `for` `i ` `in` `range` `(` `2` `, n): ` ` ` ` ` `# Store the sum ` ` ` `m ` `=` `i ` `*` `(i ` `+` `1` `) ` `/` `/` `2` ` ` ` ` `# If the sum exceeds N ` ` ` `# then break the loop ` ` ` `if` `(m > n): ` ` ` `break` ` ` ` ` `k ` `=` `n ` `-` `m ` ` ` ` ` `# Common difference should be ` ` ` `# divisible by number of terms ` ` ` `if` `(k ` `%` `i): ` ` ` `continue` ` ` ` ` `# Print value of i & return ` ` ` `print` `(i) ` ` ` `return` ` ` ` ` `# Print "-1" if not possible ` ` ` `# to break N ` ` ` `print` `(` `"-1"` `) ` ` ` `# Driver Code ` ` ` `# Given N ` `N ` `=` `12` ` ` `# Function call ` `canBreakN(N) ` ` ` `# This code is contributed by code_hunt ` |

*chevron_right*

*filter_none*

## C#

`// C# program for the above approach ` `using` `System; ` `class` `GFG{ ` ` ` `// Function to find the K consecutive ` `// elements with a sum equal to N ` `public` `static` `void` `canBreakN(` `long` `n) ` `{ ` ` ` ` ` `// Iterate over [2, INF] ` ` ` `for` `(` `long` `i = 2;; i++) ` ` ` `{ ` ` ` ` ` `// Store the sum ` ` ` `long` `m = i * (i + 1) / 2; ` ` ` ` ` `// If the sum exceeds N ` ` ` `// then break the loop ` ` ` `if` `(m > n) ` ` ` `break` `; ` ` ` ` ` `long` `k = n - m; ` ` ` ` ` `// Common difference should be ` ` ` `// divisible by number of terms ` ` ` `if` `(k % i != 0) ` ` ` `continue` `; ` ` ` ` ` `// Print value of i & return ` ` ` `Console.Write(i); ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// Print "-1" if not possible ` ` ` `// to break N ` ` ` `Console.Write(` `"-1"` `); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main(` `string` `[] args) ` `{ ` ` ` ` ` `// Given N ` ` ` `long` `N = 12; ` ` ` ` ` `// Function call ` ` ` `canBreakN(N); ` `} ` `} ` ` ` `// This code is contributed by rock_cool` |

*chevron_right*

*filter_none*

**Output:**

3

**Time Complexity:** O(K), where K is the number of element whose sum is K. **Auxiliary Space:** O(1)

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:

- Check if an array of 1s and 2s can be divided into 2 parts with equal sum
- Find the sums for which an array can be divided into sub-arrays of equal sum
- Minimize the sum of the squares of the sum of elements of each group the array is divided into
- First N natural can be divided into two sets with given difference and co-prime sums
- Minimum Cuts can be made in the Chessboard such that it is not divided into 2 parts
- Number of ways N can be divided into four parts to construct a rectangle
- Number of K's such that the given array can be divided into two sets satisfying the given conditions
- Count of Array elements to be divided by 2 to make at least K elements equal
- Count array elements that can be represented as sum of at least two consecutive array elements
- Check if a number can be expressed as a sum of consecutive numbers
- Check if a number can be written as sum of three consecutive integers
- Check if N can be represented as sum of squares of two consecutive integers
- Check if a number can be represented as sum of two consecutive perfect cubes
- Find all the possible numbers in a range that can be evenly divided by its digits
- Largest number less than or equal to Z that leaves a remainder X when divided by Y
- Check if elements of array can be made equal by multiplying given prime numbers
- Check if all elements of a Circular Array can be made equal by increments of adjacent pairs
- Construct an Array of size N in which sum of odd elements is equal to sum of even elements
- Maximize the sum of differences of consecutive elements after removing exactly K elements
- Check if the remainder of N-1 factorial when divided by N is N-1 or not

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.