📜  数组的所有无序三元组的按位与之和

📅  最后修改于: 2022-05-13 01:56:05.137000             🧑  作者: Mango

数组的所有无序三元组的按位与之和

给定一个由N个正整数组成的数组arr[] ,任务是找到所有可能的三元组(arr[i], arr[j], arr[k])的按位与之和,使得i < j < k

例子:

朴素方法:解决给定问题的最简单方法是生成给定数组的所有可能的三元组(i, j, k) ,使得i < j < k并打印所有可能的三元组的按位与之和。

下面是上述方法的实现:

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[0]);
    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


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[0]);
    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


输出:
5

时间复杂度: O(N 3 )
辅助空间: O(1)

有效方法:上述方法也可以通过考虑数字的二进制表示来优化。请按照以下步骤解决问题:

  • 初始化一个变量,比如ans0 ,它存储所有可能的三元组的按位与的结果总和。
  • [0, 31]范围内迭代一个循环并执行以下步骤:
    • 初始化一个变量,例如将cnt设置为0 ,该变量存储设置了第i的数组元素的计数。
    • 遍历给定的数组,如果设置了第i,则将cnt的值增加1
    • 在上述步骤之后,对于具有第i的所有可能的三元组,它会将值(( cnt C 3 )*2 i )贡献给结果和。因此,将此值添加到变量cnt
  • 完成上述步骤后,打印ans的值作为结果。

下面是上述方法的实现:

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[0]);
    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


输出:
5

时间复杂度: O(N)
辅助空间: O(1)