Minimize insertions required to make ratio of maximum and minimum of all pairs of adjacent array elements at most K | Java program to implement the above approach ; 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
import java . util . * ; class GFG { static int mininsert ( int arr [ ] , int K , int N ) { int ans = 0 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { int a = Math . min ( arr [ i ] , arr [ i + 1 ] ) ; int b = Math . max ( arr [ i ] , arr [ i + 1 ] ) ; while ( K * a < b ) { a *= K ; ans ++ ; } } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 10 , 25 , 21 } ; int K = 2 ; int N = arr . length ; System . out . print ( mininsert ( arr , K , N ) ) ; } }