Smallest value of N such that the sum of all natural numbers from K to N is at least X | Function to check if the sum of natural numbers from K to N is >= X ; Function to find the minimum value of N such that the sum of natural numbers from K to N is at least X ; If K is greater than X ; Perform the Binary Search ; If the sum of the natural numbers from K to mid is atleast X ; Update res ; Update high ; Otherwise , update low ; Print the value of res as the answer ; Driver Code
def isGreaterEqual ( N , K , X ) : NEW_LINE INDENT return ( ( ( N * ( N + 1 ) // 2 ) - ( ( K - 1 ) * K // 2 ) ) >= X ) NEW_LINE DEDENT def minimumNumber ( K , X ) : NEW_LINE INDENT if ( K > X ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE return NEW_LINE DEDENT low = K NEW_LINE high = X NEW_LINE res = - 1 NEW_LINE while ( low <= high ) : NEW_LINE INDENT mid = low + ( ( high - low ) // 2 ) NEW_LINE if ( isGreaterEqual ( mid , K , X ) ) : NEW_LINE INDENT res = mid NEW_LINE high = mid - 1 NEW_LINE DEDENT else : NEW_LINE INDENT low = mid + 1 NEW_LINE DEDENT DEDENT print ( res ) NEW_LINE DEDENT K = 5 NEW_LINE X = 13 NEW_LINE minimumNumber ( K , X ) NEW_LINE