Check if N can be divided into K consecutive elements with a sum equal to N | Java 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
class GFG { public static void canBreakN ( long n ) { for ( long i = 2 ; ; i ++ ) { long m = i * ( i + 1 ) / 2 ; if ( m > n ) break ; long k = n - m ; if ( k % i != 0 ) continue ; System . out . println ( i ) ; return ; } System . out . println ( " - 1" ) ; } public static void main ( String [ ] args ) { long N = 12 ; canBreakN ( N ) ; } }