Check if N can be divided into K consecutive elements with a sum equal to N

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 = 12 
Output:
Explanation: 
The integer N = 12 can be divided into 3 consecutive elements {3, 4, 5} where 3 + 4 + 5 = 12.

Input: N = 8 
Output: -1 
Explanation: 
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:

Sum = \frac{i*(i+1)}{2} + (i*d) = N



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:

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

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


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.




My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

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.



Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.