Count pairs from an array having GCD equal to the minimum element in the pair | java program for the above approach ; Function to count pairs from an array having GCD equal to minimum element of that pair ; Stores the resultant count ; Iterate over the range [ 0 , N - 2 ] ; Iterate over the range [ i + 1 , N ] ; If arr [ i ] % arr [ j ] is 0 or arr [ j ] % arr [ i ] is 0 ; Increment count by 1 ; Return the resultant count ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; public class GFG { static int countPairs ( int arr [ ] , int N ) { int count = 0 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { if ( arr [ i ] % arr [ j ] == 0 arr [ j ] % arr [ i ] == 0 ) { count ++ ; } } } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 1 , 2 } ; int N = arr . length ; System . out . print ( countPairs ( arr , N ) ) ; } }