Count of pairs ( i , j ) in the array such that arr [ i ] is a factor of arr [ j ] | C ++ Program to find the number of pairs ( i , j ) such that arr [ i ] is a factor of arr [ j ] ; Function to return the count of Pairs ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int numPairs ( int arr [ ] , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( arr [ j ] % arr [ i ] == 0 ) ans ++ ; } } return ans ; } int main ( ) { int arr [ ] = { 1 , 1 , 2 , 2 , 3 , 3 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << numPairs ( arr , n ) << endl ; return 0 ; }