Count pairs in an array which have at least one digit common | Returns true if the pair is valid , otherwise false ; converting integers to strings ; Iterate over the strings and check if a character in first string is also present in second string , return true ; No common digit found ; Returns the number of valid pairs ; Iterate over all possible pairs ; Driver Code
< ? php function checkValidPair ( $ num1 , $ num2 ) { $ s1 = ( string ) $ num1 ; $ s2 = ( string ) $ num2 ; for ( $ i = 0 ; $ i < strlen ( $ s1 ) ; $ i ++ ) for ( $ j = 0 ; $ j < strlen ( $ s2 ) ; $ j ++ ) if ( $ s1 [ $ i ] == $ s2 [ $ j ] ) return true ; return false ; } function countPairs ( & $ arr , $ n ) { $ numberOfPairs = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) for ( $ j = $ i + 1 ; $ j < $ n ; $ j ++ ) if ( checkValidPair ( $ arr [ $ i ] , $ arr [ $ j ] ) ) $ numberOfPairs ++ ; return $ numberOfPairs ; } $ arr = array ( 10 , 12 , 24 ) ; $ n = sizeof ( $ arr ) ; echo ( countPairs ( $ arr , $ n ) ) ; ? >