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