Find the smallest value of N such that sum of first N natural numbers is â ‰¥ X | Function to check if sum of first N natural numbers is >= X ; Finds minimum value of N such that sum of first N natural number >= X ; Binary Search ; Checks if sum of first ' mid ' natural numbers is greater than equal to X ; Update res ; Update high ; Update low ; Input ; Finds minimum value of N such that sum of first N natural number >= X
function isGreaterEqual ( N , X ) { return parseInt ( ( N * ( N + 1 ) ) / 2 ) >= X ; } function minimumPossible ( X ) { let low = 1 , high = X , res = - 1 ; while ( low <= high ) { let mid = low + parseInt ( ( high - low ) / 2 ) ; if ( isGreaterEqual ( mid , X ) ) { res = mid ; high = mid - 1 ; } else low = mid + 1 ; } return res ; } let X = 14 ; document . write ( minimumPossible ( X ) ) ;