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++
#include <bits/stdc++.h>
using namespace std;
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];
}
}
}
cout << ans;
}
int main()
{
int arr[] = { 3, 5, 4, 7 };
int N = sizeof (arr) / sizeof (arr[0]);
tripletAndSum(arr, N);
return 0;
}
|
Java
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);
}
}
|
Python3
def tripletAndSum(arr, n):
ans = 0
for i in range (n):
for j in range (i + 1 , n, 1 ):
for k in range (j + 1 , n, 1 ):
ans + = arr[i] & arr[j] & arr[k]
print (ans)
if __name__ = = '__main__' :
arr = [ 3 , 5 , 4 , 7 ]
N = len (arr)
tripletAndSum(arr, N)
|
C#
using System;
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];
}
}
}
Console.WriteLine(ans);
}
static public void Main()
{
int [] arr = { 3, 5, 4, 7 };
int N = arr.Length;
tripletAndSum(arr, N);
}
}
|
Javascript
<script>
function tripletAndSum(arr, n) {
let ans = 0;
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
for (let k = j + 1; k < n; k++) {
ans += arr[i] & arr[j] & arr[k];
}
}
}
document.write(ans);
}
let arr = [3, 5, 4, 7];
let N = arr.length
tripletAndSum(arr, N);
</script>
|
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:
- Initialize a variable, say ans as 0 that stores the resultant sum of Bitwise AND of all possible triplets.
- Iterate a loop over the range [0, 31] and perform the following steps:
- After completing the above steps, print the value of ans as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int tripletAndSum( int arr[], int n)
{
int ans = 0;
for ( int bit = 0; bit < 32; bit++) {
int cnt = 0;
for ( int i = 0; i < n; i++) {
if (arr[i] & (1 << bit))
cnt++;
}
ans += (1 << bit) * cnt
* (cnt - 1) * (cnt - 2) / 6;
}
return ans;
}
int main()
{
int arr[] = { 3, 5, 4, 7 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << tripletAndSum(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int tripletAndSum( int [] arr, int n)
{
int ans = 0 ;
for ( int bit = 0 ; bit < 32 ; bit++)
{
int cnt = 0 ;
for ( int i = 0 ; i < n; i++)
{
if ((arr[i] & ( 1 << bit)) != 0 )
cnt++;
}
ans += ( 1 << bit) * cnt *
(cnt - 1 ) * (cnt - 2 ) / 6 ;
}
return ans;
}
public static void main(String[] args)
{
int arr[] = { 3 , 5 , 4 , 7 };
int N = arr.length;
System.out.print(tripletAndSum(arr, N));
}
}
|
Python3
def tripletAndSum(arr, n):
ans = 0
for bit in range ( 32 ):
cnt = 0
for i in range (n):
if (arr[i] & ( 1 << bit)):
cnt + = 1
ans + = ( 1 << bit) * cnt * (cnt - 1 ) * (cnt - 2 ) / / 6
return ans
arr = [ 3 , 5 , 4 , 7 ]
N = len (arr)
print (tripletAndSum(arr, N))
|
C#
using System;
class GFG{
static int tripletAndSum( int [] arr, int n)
{
int ans = 0;
for ( int bit = 0; bit < 32; bit++)
{
int cnt = 0;
for ( int i = 0; i < n; i++)
{
if ((arr[i] & (1 << bit)) != 0)
cnt++;
}
ans += (1 << bit) * cnt * (cnt - 1) *
(cnt - 2) / 6;
}
return ans;
}
public static void Main()
{
int [] arr = { 3, 5, 4, 7 };
int N = arr.Length;
Console.Write(tripletAndSum(arr, N));
}
}
|
Javascript
<script>
function tripletAndSum(arr, n) {
let ans = 0;
for (let bit = 0; bit < 32; bit++) {
let cnt = 0;
for (let i = 0; i < n; i++) {
if (arr[i] & (1 << bit))
cnt++;
}
ans += (1 << bit) * cnt
* (cnt - 1) * (cnt - 2) / 6;
}
return ans;
}
let arr = [3, 5, 4, 7];
let N = arr.length;
document.write(tripletAndSum(arr, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...