Count maximum concatenation of pairs from given array that are divisible by 3 | Function to count pairs whose concatenation is divisible by 3 and each element can be present in at most one pair ; Stores count of array elements whose remainder is 0 by taking modulo by 3 ; Stores count of array elements whose remainder is 1 by taking modulo by 3 ; Stores count of array elements whose remainder is 2 by taking modulo by 3 ; Traverse the array ; Stores sum of digits of arr [ i ] ; Update digitSum ; If remainder of digitSum by by taking modulo 3 is 0 ; Update rem0 ; If remainder of digitSum by by taking modulo 3 is 1 ; Update rem1 ; Update rem2 ; Driver code ; To display the result
function countDiv ( arr ) { let rem0 = 0 ; let rem1 = 0 ; let rem2 = 0 ; for ( let i = 0 ; i < arr . length ; i ++ ) { let digitSum = 0 ; digitSum += arr [ i ] ; if ( digitSum % 3 == 0 ) { rem0 += 1 ; } else if ( digitSum % 3 == 1 ) { rem1 += 1 ; } else { rem2 += 1 ; } } return ( parseInt ( rem0 / 2 , 10 ) + Math . min ( rem1 , rem2 ) ) ; } let arr = [ 5 , 3 , 2 , 8 , 7 ] ; document . write ( countDiv ( arr ) ) ;