Sum of Bitwise AND of all unordered triplets of an array | Java program for the above approach ; Function to calculate sum of Bitwise AND of all unordered triplets from a given array such that ( i < j < k ) ; Stores the resultant sum of Bitwise AND of all triplets ; Generate all triplets of ( arr [ i ] , arr [ j ] , arr [ k ] ) ; Add Bitwise AND to ans ; Print the result ; Driver Code
import java . io . * ; class GFG { public static void tripletAndSum ( int arr [ ] , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { for ( int k = j + 1 ; k < n ; k ++ ) { ans += arr [ i ] & arr [ j ] & arr [ k ] ; } } } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 5 , 4 , 7 } ; int N = arr . length ; tripletAndSum ( arr , N ) ; } }