给定大小为N ( 1≤N≤10 5 )的数组arr [] ,任务是找到选择三元组i,j和k的方式数,以使i
注意:每个三元组最多可以包含一个负数元素。
例子:
Input: arr[] = { 2, 5, -9, -3, 6}
Output: 1
Explanation: The total number ways to obtain a triplet i, j and k to satisfy given conditions is 1 {0, 1, 4}.
Input : arr[] = {2, 5, 6, -2, 5}
Output : 4
Explanation: The total number ways to obtain a triplet i, j and k to satisfy given conditions are 4 {0, 1, 2}, {0, 1, 4}, {1, 2, 4} and {0, 2, 4}.
方法:三元组的所有可能组合如下:
- #个负数元素或2个负数元素和1个正数元素。不能将这两种组合视为三元组中允许的最大负元素为1。
- 2个负( -ve )元素和1个正( + ve )元素。由于三元组的乘积将为负,因此无法考虑三元组。
- 3个积极因素。
请按照以下步骤解决问题:
- 遍历数组并计算正数组元素的频率,例如freq 。
- 使用公式PnC = N C 3 =(N *(N – 1)*(N – 2))/ 6从数组元素的频率中选择有效三元组的方法计数。将获得的计数加到答案中。
- 打印获得的计数。
下面是上述方法的实现:
C++
// C++ Program to implement
// the above approach
#include
using namespace std;
// Function to calculate
// possible number of triplets
long long int possibleTriplets(int arr[], int N)
{
int freq = 0;
// counting frequency of positive numbers
// in array
for (int i = 0; i < N; i++) {
// If current array
// element is positive
if (arr[i] > 0) {
// Increment frequency
freq++;
}
}
// Select a triplet from freq
// elements such that i < j < k.
return (freq * 1LL * (freq - 1)
* (freq - 2))
/ 6;
}
// Driver Code
int main()
{
int arr[] = { 2, 5, -9, -3, 6 };
int N = sizeof(arr) / sizeof(arr[0]);
cout << possibleTriplets(arr, N);
return 0;
}
Java
// Java Program to implement
// the above approach
import java.util.*;
class GFG
{
// Function to calculate
// possible number of triplets
static int possibleTriplets(int arr[], int N)
{
int freq = 0;
// counting frequency of positive numbers
// in array
for (int i = 0; i < N; i++)
{
// If current array
// element is positive
if (arr[i] > 0)
{
// Increment frequency
freq++;
}
}
// Select a triplet from freq
// elements such that i < j < k.
return (int) ((freq * 1L * (freq - 1)
* (freq - 2))
/ 6);
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 2, 5, -9, -3, 6 };
int N = arr.length;
System.out.print(possibleTriplets(arr, N));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 Program to implement
# the above approach
# Function to calculate
# possible number of triplets
def possibleTriplets(arr, N):
freq = 0
# counting frequency of positive numbers
# in array
for i in range(N):
# If current array
# element is positive
if (arr[i] > 0):
# Increment frequency
freq += 1
# Select a triplet from freq
# elements such that i < j < k.
return (freq * (freq - 1) * (freq - 2)) // 6
# Driver Code
if __name__ == '__main__':
arr = [2, 5, -9, -3, 6]
N = len(arr)
print(possibleTriplets(arr, N))
# This code is contributed by mohit kumar 29
C#
// C# Program to implement
// the above approach
using System;
public class GFG
{
// Function to calculate
// possible number of triplets
static int possibleTriplets(int []arr, int N)
{
int freq = 0;
// counting frequency of positive numbers
// in array
for (int i = 0; i < N; i++)
{
// If current array
// element is positive
if (arr[i] > 0)
{
// Increment frequency
freq++;
}
}
// Select a triplet from freq
// elements such that i < j < k.
return (int) ((freq * 1L * (freq - 1)
* (freq - 2)) / 6);
}
// Driver Code
public static void Main(String[] args)
{
int []arr = { 2, 5, -9, -3, 6 };
int N = arr.Length;
Console.Write(possibleTriplets(arr, N));
}
}
// This code is contributed by 29AjayKumar
输出:
1
时间复杂度: O(N)
辅助空间: O(1)