Find element position in given monotonic sequence | Javascript implementation of the approach ; Function to return the value of f ( n ) for given values of a , b , c , n ; If c is 0 , then value of n can be in order of 10 ^ 15. If c != 0 , then n ^ 3 value has to be in order of 10 ^ 18 so maximum value of n can be 10 ^ 6. ; For efficient searching , use binary search . ; Driver code
const SMALL_N = 1000000 ; const LARGE_N = 1000000000000000 ; function func ( a , b , c , n ) { let res = a * n ; let logVlaue = Math . floor ( Math . log ( n ) / Math . log ( 2 ) ) ; res += b * n * logVlaue ; res += c * ( n * n * n ) ; return res ; } function getPositionInSeries ( a , b , c , k ) { let start = 1 , end = SMALL_N ; if ( c == 0 ) { end = LARGE_N ; } let ans = 0 ; while ( start <= end ) { let mid = parseInt ( ( start + end ) / 2 ) ; let val = func ( a , b , c , mid ) ; if ( val == k ) { ans = mid ; break ; } else if ( val > k ) { end = mid - 1 ; } else { start = mid + 1 ; } } return ans ; } let a = 2 , b = 1 , c = 1 ; let k = 12168587437017 ; document . write ( getPositionInSeries ( a , b , c , k ) ) ;