Minimize insertions required to make ratio of maximum and minimum of all pairs of adjacent array elements at most K | Function to count the minimum number of insertions required ; Stores the number of insertions required ; Traverse the array ; Store the minimum array element ; Store the maximum array element ; Checking condition ; Increase current count ; Return the count of insertions ; Driver Code
function mininsert ( arr , K , N ) { var ans = 0 ; for ( i = 0 ; i < N - 1 ; i ++ ) { var a = Math . min ( arr [ i ] , arr [ i + 1 ] ) ; var b = Math . max ( arr [ i ] , arr [ i + 1 ] ) ; while ( K * a < b ) { a *= K ; ans ++ ; } } return ans ; } var arr = [ 2 , 10 , 25 , 21 ] ; var K = 2 ; var N = arr . length ; document . write ( mininsert ( arr , K , N ) ) ;