Count distinct elements after adding each element of First Array with Second Array | Java program for the above approach ; Function to find Occurrence of each element from 1 to 2 * MAX ; Initialise MAX ; Count vector to store count of each element from 1 to 2 * MAX ; Size of Arr1 and Arr2 ; Find the elements of arr3 [ ] and increase count of element by 1 ; Print the result ; Driver Code ; Given arrays arr1 [ ] and arr2 [ ] ; Function call
import java . util . * ; class GFG { static void findCount ( int [ ] Arr1 , int [ ] Arr2 ) { int MAX = Math . max ( Arrays . stream ( Arr1 ) . max ( ) . getAsInt ( ) , Arrays . stream ( Arr2 ) . max ( ) . getAsInt ( ) ) ; int [ ] Count = new int [ 2 * MAX + 1 ] ; int n = Arr1 . length , m = Arr2 . length ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { int element = Arr1 [ i ] + Arr2 [ j ] ; Count [ element ] ++ ; } } for ( int i = 1 ; i <= 2 * MAX ; i ++ ) { if ( Count [ i ] > 0 ) { System . out . print ( i + " - > " + Count [ i ] + "NEW_LINE"); } } } public static void main ( String [ ] args ) { int [ ] arr1 = { 1 , 2 } ; int [ ] arr2 = { 1 , 2 , 1 } ; findCount ( arr1 , arr2 ) ; } }