Count maximum concatenation of pairs from given array that are divisible by 3 | Java program to implement the above approach ; Function to count pairs whose concatenation is divisible by 3 and each element can be present in at most one pair ; Stores count pairs whose concatenation is divisible by 3 and each element can be present in at most one pair ; Check if an element present in any pair or not ; Generate all possible pairs ; If the element already present in a pair ; If the element already present in a pair ; If concatenation of elements is divisible by 3 ; Update ans ; Mark i is True ; Mark j is True ; Driver Code ; To display the result
import java . io . * ; import java . util . Arrays ; class GFG { public static int countDivBy3InArray ( int [ ] arr ) { int ans = 0 ; boolean [ ] taken = new boolean [ arr . length ] ; Arrays . fill ( taken , false ) ; for ( int i = 0 ; i < arr . length ; i ++ ) { if ( taken [ i ] == true ) { continue ; } for ( int j = i + 1 ; j < arr . length ; j ++ ) { if ( taken [ j ] == true ) { continue ; } if ( Integer . parseInt ( Integer . toString ( arr [ i ] ) + Integer . toString ( arr [ j ] ) ) % 3 == 0 || Integer . parseInt ( Integer . toString ( arr [ j ] ) + Integer . toString ( arr [ i ] ) ) % 3 == 0 ) { ans += 1 ; taken [ i ] = true ; taken [ j ] = true ; } } } return ans ; } public static void main ( String [ ] args ) { int [ ] arr = { 5 , 3 , 2 , 8 , 7 } ; System . out . println ( countDivBy3InArray ( arr ) ) ; } }