Closest pair in an Array such that one number is multiple of the other | Java program for the above approach ; 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 ; Driver Code ; Given array arr [ ] ; Function Call
import java . util . * ; class GFG { public static void findPair ( int a [ ] , int n ) { int min_dist = Integer . MAX_VALUE ; int index_a = - 1 , index_b = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int 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 ) { System . out . println ( " - 1" ) ; } else { System . out . print ( " ( " + a [ index_a ] + " , ▁ " + a [ index_b ] + " ) " ) ; } } public static void main ( String [ ] args ) { int a [ ] = { 2 , 3 , 4 , 5 , 6 } ; int n = a . length ; findPair ( a , n ) ; } }