Related Articles

# Sum of Bitwise AND of all unordered triplets of an array

• Last Updated : 15 Jul, 2021

Given an array arr[] consisting of N positive integers, the task is to find the sum of Bitwise AND of all possible triplets (arr[i], arr[j], arr[k]) such that i < j < k.

Examples:

Input: arr[] = {3, 5, 4, 7}
Output: 5
Explanation: Sum of Bitwise AND of all possible triplets = (3 & 5 & 4) + (3 & 5 & 7) + (3 & 4 & 7) + (5 & 4 & 7) = 0 + 1 + 0 + 4 = 5.

Input: arr[] = {4, 4, 4}
Output: 4

Naive Approach: The simplest approach to solve the given problem is to generate all possible triplets (i, j, k) of the given array such that i < j < k  and print the sum of Bitwise AND of all possible triplets.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach` `#include ``using` `namespace` `std;` `// Function to calculate sum of Bitwise``// AND of all unordered triplets from``// a given array such that (i < j < k)``void` `tripletAndSum(``int` `arr[], ``int` `n)``{``    ``// Stores the resultant sum of``    ``// Bitwise AND of all triplets``    ``int` `ans = 0;` `    ``// Generate all triplets of``    ``// (arr[i], arr[j], arr[k])``    ``for` `(``int` `i = 0; i < n; i++) {``        ``for` `(``int` `j = i + 1; j < n; j++) {``            ``for` `(``int` `k = j + 1; k < n; k++) {` `                ``// Add Bitwise AND to ans``                ``ans += arr[i] & arr[j] & arr[k];``            ``}``        ``}``    ``}` `    ``// Print the result``    ``cout << ans;``}` `// Driver Code``int` `main()``{``    ``int` `arr[] = { 3, 5, 4, 7 };``    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr);``    ``tripletAndSum(arr, N);``//This code is contributed by Potta Lokesh``    ``return` `0;``}`

## Java

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

## Python3

 `# Python3 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)``def` `tripletAndSum(arr, n):``    ` `    ``# Stores the resultant sum of``    ``# Bitwise AND of all triplets``    ``ans ``=` `0` `    ``# Generate all triplets of``    ``# (arr[i], arr[j], arr[k])``    ``for` `i ``in` `range``(n):``        ``for` `j ``in` `range``(i ``+` `1``, n, ``1``):``            ``for` `k ``in` `range``(j ``+` `1``, n, ``1``):``                ` `                ``# Add Bitwise AND to ans``                ``ans ``+``=` `arr[i] & arr[j] & arr[k]` `    ``# Print the result``    ``print``(ans)` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:``    ` `    ``arr ``=` `[ ``3``, ``5``, ``4``, ``7` `]``    ``N ``=` `len``(arr)``    ` `    ``tripletAndSum(arr, N)` `# This code is contributed by bgangwar59`

## C#

 `// C# program for the above approach``using` `System;` `class` `GFG{``    ` `    ``// Function to calculate sum of Bitwise``    ``// AND of all unordered triplets from``    ``// a given array such that (i < j < k)``    ``public` `static` `void` `tripletAndSum(``int``[] arr, ``int` `n)``    ``{``      ` `        ``// Stores the resultant sum of``        ``// Bitwise AND of all triplets``        ``int` `ans = 0;` `        ``// Generate all triplets of``        ``// (arr[i], arr[j], arr[k])``        ``for` `(``int` `i = 0; i < n; i++) {``            ``for` `(``int` `j = i + 1; j < n; j++) {``                ``for` `(``int` `k = j + 1; k < n; k++) {` `                    ``// Add Bitwise AND to ans``                    ``ans += arr[i] & arr[j] & arr[k];``                ``}``            ``}``        ``}` `        ``// Print the result``        ``Console.WriteLine(ans);``    ``}`  `// Driver code``static` `public` `void` `Main()``{``    ``int``[] arr = { 3, 5, 4, 7 };``        ``int` `N = arr.Length;``        ``tripletAndSum(arr, N);``}``}` `// This code is contributed by splevel62.`

## Javascript

 ``
Output:
`5`

Time Complexity: O(N3)
Auxiliary Space: O(1)

Efficient Approach: The above approach can also be optimized by taking into account the binary representation of the numbers. Follow the steps below to solve the problem:

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach` `#include ``using` `namespace` `std;` `// Function to calculate sum of Bitwise``// AND of all unordered triplets from``// a given array such that (i < j < k)``int` `tripletAndSum(``int` `arr[], ``int` `n)``{``    ``// Stores the resultant sum of``    ``// Bitwise AND of all triplets``    ``int` `ans = 0;` `    ``// Traverse over all the bits``    ``for` `(``int` `bit = 0; bit < 32; bit++) {``        ``int` `cnt = 0;` `        ``// Count number of elements``        ``// with the current bit set``        ``for` `(``int` `i = 0; i < n; i++) {``            ``if` `(arr[i] & (1 << bit))``                ``cnt++;``        ``}` `        ``// There are (cnt)C(3) numbers``        ``// with the current bit set and``        ``// each triplet contributes``        ``// 2^bit to the result``        ``ans += (1 << bit) * cnt``               ``* (cnt - 1) * (cnt - 2) / 6;``    ``}` `    ``// Return the resultant sum``    ``return` `ans;``}` `// Driver Code``int` `main()``{``    ``int` `arr[] = { 3, 5, 4, 7 };``    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr);``    ``cout << tripletAndSum(arr, N);` `    ``return` `0;``}`

## Java

 `// Java program for the above approach``import` `java.util.*;` `class` `GFG{` `// Function to calculate sum of Bitwise``// AND of all unordered triplets from``// a given array such that (i < j < k)``static` `int` `tripletAndSum(``int``[] arr, ``int` `n)``{``    ` `    ``// Stores the resultant sum of``    ``// Bitwise AND of all triplets``    ``int` `ans = ``0``;` `    ``// Traverse over all the bits``    ``for``(``int` `bit = ``0``; bit < ``32``; bit++)``    ``{``        ``int` `cnt = ``0``;` `        ``// Count number of elements``        ``// with the current bit set``        ``for``(``int` `i = ``0``; i < n; i++)``        ``{``            ``if` `((arr[i] & (``1` `<< bit)) != ``0``)``                ``cnt++;``        ``}` `        ``// There are (cnt)C(3) numbers``        ``// with the current bit set and``        ``// each triplet contributes``        ``// 2^bit to the result``        ``ans += (``1` `<< bit) * cnt *``               ``(cnt - ``1``) * (cnt - ``2``) / ``6``;``    ``}` `    ``// Return the resultant sum``    ``return` `ans;``}` `// Driver Code``public` `static` `void` `main(String[] args)``{``    ``int` `arr[] = { ``3``, ``5``, ``4``, ``7` `};``    ``int` `N = arr.length;``    ` `    ``System.out.print(tripletAndSum(arr, N));``}``}` `// This code is contributed by subham348`

## Python3

 `# Python 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)``def` `tripletAndSum(arr, n):``    ` `    ``# Stores the resultant sum of``    ``# Bitwise AND of all triplets``    ``ans ``=` `0``    ` `    ``# Traverse over all the bits``    ``for` `bit ``in` `range``(``32``):``        ``cnt ``=` `0``        ` `        ``# Count number of elements``        ``# with the current bit set``        ``for` `i ``in` `range``(n):``            ``if``(arr[i] & (``1` `<< bit)):``                ``cnt``+``=``1``                ` `        ``# There are (cnt)C(3) numbers``        ``# with the current bit set and``        ``# each triplet contributes``        ``# 2^bit to the result``        ``ans ``+``=` `(``1` `<< bit) ``*` `cnt ``*` `(cnt ``-` `1``) ``*` `(cnt ``-` `2``) ``/``/` `6``    ` `    ``# Return the resultant sum``    ``return` `ans` `# Driver Code``arr ``=`  `[``3``, ``5``, ``4``, ``7``]``N ``=` `len``(arr)``print``(tripletAndSum(arr, N))` `# this code is contributed by shivanisinghss2110`

## C#

 `// C# program for the above approach``using` `System;` `class` `GFG{` `// Function to calculate sum of Bitwise``// AND of all unordered triplets from``// a given array such that (i < j < k)``static` `int` `tripletAndSum(``int``[] arr, ``int` `n)``{``    ` `    ``// Stores the resultant sum of``    ``// Bitwise AND of all triplets``    ``int` `ans = 0;` `    ``// Traverse over all the bits``    ``for``(``int` `bit = 0; bit < 32; bit++)``    ``{``        ``int` `cnt = 0;` `        ``// Count number of elements``        ``// with the current bit set``        ``for``(``int` `i = 0; i < n; i++)``        ``{``            ``if` `((arr[i] & (1 << bit)) != 0)``                ``cnt++;``        ``}` `        ``// There are (cnt)C(3) numbers``        ``// with the current bit set and``        ``// each triplet contributes``        ``// 2^bit to the result``        ``ans += (1 << bit) * cnt * (cnt - 1) *``                                  ``(cnt - 2) / 6;``    ``}` `    ``// Return the resultant sum``    ``return` `ans;``}` `// Driver Code``public` `static` `void` `Main()``{``    ``int``[] arr = { 3, 5, 4, 7 };``    ``int` `N = arr.Length;` `    ``Console.Write(tripletAndSum(arr, N));``}``}` `// This code is contributed by rishavmahato348`

## Javascript

 ``
Output:
`5`

Time Complexity: O(N)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up