text: Closest pair in an Array such that one number is multiple of the other | Function to find the minimum distance pair where one is the multiple of the other ; Initialize the variables ; Iterate for all the elements ; Loop to make pairs ; Check for minimum distance ; Check if one is a multiple of other ; Update the distance ; Store indexes ; If no such pair exists ; Print the answer ; Given array arr [ ] ; Function Call
code: function findPair ( a , n ) { let min_dist = Number . MAX_VALUE ; let index_a = - 1 , index_b = - 1 ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = i + 1 ; j < n ; j ++ ) { if ( j - i < min_dist ) { if ( a [ i ] % a [ j ] == 0 a [ j ] % a [ i ] == 0 ) { min_dist = j - i ; index_a = i ; index_b = j ; } } } } if ( index_a == - 1 ) { document . write ( " " ) ; } else { document . write ( " " + a [ index_a ] + " " + a [ index_b ] + " " ) ; } } let a = [ 2 , 3 , 4 , 5 , 6 ] ; let n = a . length ; findPair ( a , n ) ;