📌  相关文章
📜  计算给定数组中具有乘积0的三元组

📅  最后修改于: 2021-04-22 02:03:08             🧑  作者: Mango

给定大小为N的数组arr [] ,任务是计算三元组(arr [i],arr [j],arr [k])的数量,以使arr [i] * arr [j] = arr [j ] * arr [k] = 0 (i 例子:

天真的方法:解决此问题的最简单方法是从给定数组生成所有可能的三元组,并打印满足给定条件的三元组的计数。

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

高效方法:为了优化上述方法,其思想是使用Prefix Sum技术将0 s的计数存储在每个数组元素的左侧和右侧。通过将数组的当前元素视为arr [j]的值,遍历数组并计算满足给定条件的三元组的数量。最后,打印满足给定条件的三元组的计数。请按照以下步骤解决问题:

  • 初始化一个数组,例如prefixZero [] ,为每个索引存储前面的索引中存在的0 s计数。
  • 初始化变量TripletCnt以存储满足给定条件的三元组的数量。
  • 遍历数组并检查arr [i]是否等于0 。如果发现为真,则将cntTriplet的值增加i *(N – i -1)
  • 否则,将TripletCnt的值增加prefixZero [i] *(prefixZero [N – 1] – prefixZero [i])
  • 最后,输出TripletCnt的值。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to get the count
// of triples that satisfy
// the given condition
int cntTriplet(int arr[], int N)
{
 
    // preZero[i] stores count
    // of 0 up to index i
    int preZero[N] = { 0 };
 
    // Traverse the array and
    // Count 0s up to index i
    for (int i = 0; i < N; i++) {
        if (arr[i] == 0) {
            preZero[i]
                = preZero[max(i - 1, 0)] + 1;
        }
        else {
            preZero[i]
                = preZero[max(i - 1, 0)];
        }
    }
 
    // Stores count of triplet that
    // satisfy the given conditions
    int tripletCount = 0;
 
    // Traverse the given array
    for (int i = 0; i < N; i++) {
        if (arr[i] == 0) {
 
            // Stores count of elements
            // on  the left side of arr[i]
            int X = i;
 
            // Stores count of elements
            // on  the right side of arr[i]
            int Y = N - i - 1;
 
            tripletCount += X * Y;
        }
 
        else {
 
            // Stores count of 0s on
            // the left side of arr[i]
            int X = preZero[i];
 
            // Stores count of 0s on
            // the right side of arr[i]
            int Y = preZero[N - 1]
                    - preZero[i];
 
            tripletCount += X * Y;
        }
    }
 
    return tripletCount;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 0, 2, 3 };
    int N = sizeof(arr) / sizeof(arr[0]);
    cout << cntTriplet(arr, N);
 
    return 0;
}


Java
// Java program to implement
// the above approach
import java.util.*;
class GFG{
 
// Function to get the count
// of triples that satisfy
// the given condition
static int cntTriplet(int arr[],
                      int N)
{
  // preZero[i] stores count
  // of 0 up to index i
  int []preZero = new int[N];
 
  // Traverse the array and
  // Count 0s up to index i
  for (int i = 0; i < N; i++)
  {
    if (arr[i] == 0)
    {
      preZero[i] = preZero[Math.max(i - 1,
                                    0)] + 1;
    }
    else
    {
      preZero[i] = preZero[Math.max(i - 1, 0)];
    }
  }
 
  // Stores count of triplet that
  // satisfy the given conditions
  int tripletCount = 0;
 
  // Traverse the given array
  for (int i = 0; i < N; i++)
  {
    if (arr[i] == 0)
    {
      // Stores count of elements
      // on  the left side of arr[i]
      int X = i;
 
      // Stores count of elements
      // on  the right side of arr[i]
      int Y = N - i - 1;
 
      tripletCount += X * Y;
    }
    else
    {
      // Stores count of 0s on
      // the left side of arr[i]
      int X = preZero[i];
 
      // Stores count of 0s on
      // the right side of arr[i]
      int Y = preZero[N - 1] -
              preZero[i];
 
      tripletCount += X * Y;
    }
  }
 
  return tripletCount;
}
 
// Driver Code
public static void main(String[] args)
{
  int arr[] = {1, 0, 2, 3};
  int N = arr.length;
  System.out.print(cntTriplet(arr, N));
}
}
 
// This code contributed by gauravrajput1


Python3
# Python3 program to implement
# the above appraoch
 
# Function to get the count
# of triples that satisfy
# the given condition
def cntTriplet(arr, N):
 
    # preZero[i] stores count
    # of 0 up to index i
    preZero = [0] * N
 
    # Traverse the array and
    # Count 0s up to index i
    for i in range(N):
        if (arr[i] == 0):
            preZero[i] = preZero[
            max(i - 1, 0)] + 1
        else:
            preZero[i] = preZero[
            max(i - 1, 0)]
 
    # Stores count of triplet that
    # satisfy the given conditions
    tripletCount = 0
 
    # Traverse the given array
    for i in range(N):
        if (arr[i] == 0):
 
            # Stores count of elements
            # on  the left side of arr[i]
            X = i
 
            # Stores count of elements
            # on  the right side of arr[i]
            Y = N - i - 1
 
            tripletCount += X * Y
 
        else:
             
            # Stores count of 0s on
            # the left side of arr[i]
            X = preZero[i]
 
            # Stores count of 0s on
            # the right side of arr[i]
            Y = preZero[N - 1] - preZero[i]
 
            tripletCount += X * Y
 
    return tripletCount
 
# Driver code
if __name__ == '__main__':
 
    arr = [ 1, 0, 2, 3 ]
    N = len(arr)
     
    print(cntTriplet(arr, N))
 
# This code is contributed by Shivam Singh


C#
// C# program to implement
// the above approach
using System;
class GFG{
 
// Function to get the count
// of triples that satisfy
// the given condition
static int cntTriplet(int[] arr,
                      int N)
{
  // preZero[i] stores count
  // of 0 up to index i
  int[] preZero = new int[N];
 
  // Traverse the array and
  // Count 0s up to index i
  for (int i = 0; i < N; i++)
  {
    if (arr[i] == 0)
    {
      preZero[i] = preZero[Math.Max(i - 1,
                                    0)] + 1;
    }
    else
    {
      preZero[i] = preZero[Math.Max(i - 1, 0)];
    }
  }
 
  // Stores count of triplet that
  // satisfy the given conditions
  int tripletCount = 0;
 
  // Traverse the given array
  for (int i = 0; i < N; i++)
  {
    if (arr[i] == 0)
    {
      // Stores count of elements
      // on  the left side of arr[i]
      int X = i;
 
      // Stores count of elements
      // on  the right side of arr[i]
      int Y = N - i - 1;
 
      tripletCount += X * Y;
    }
    else
    {
      // Stores count of 0s on
      // the left side of arr[i]
      int X = preZero[i];
 
      // Stores count of 0s on
      // the right side of arr[i]
      int Y = preZero[N - 1] -
              preZero[i];
 
      tripletCount += X * Y;
    }
  }
 
  return tripletCount;
}
 
// Driver Code
public static void Main(String[] args)
{
  int[] arr = {1, 0, 2, 3};
  int N = arr.Length;
  Console.Write(cntTriplet(arr, N));
}
}
 
// This code is contributed by Chitranayal


输出:
2









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