Number from a range [ L , R ] having Kth minimum cost of conversion to 1 by given operations | Function to calculate the cost ; Base case ; Even condition ; Odd condition ; Return cost ; Function to find Kth element ; Array to store the costs ; Sort the array based on cost ; Given range and6 K ; Function call
function func ( n ) { var count = 0 ; if ( n == 2 n == 1 ) return 1 ; if ( n % 2 == 0 ) count = 1 + func ( n / 2 ) ; if ( n % 2 != 0 ) count = 1 + func ( n * 3 + 1 ) ; return count ; } function findKthElement ( l , r , k ) { var arr = [ ] ; for ( var i = l ; i <= r ; i ++ ) arr . push ( i ) ; var result = [ ] ; arr . forEach ( i => { result . push ( [ i , func ( i ) ] ) ; } ) ; result . sort ( ) ; document . write ( result [ k - 1 ] [ 0 ] ) ; } var l = 12 ; var r = 15 ; var k = 2 ; findKthElement ( l , r , k ) ;