具有奇数和的数组中的三元组数
给定一个包含N个整数的数组arr[] ,找出i 、 j和k三元组的数量,使得1<= i < j < k <= N并且arr[i] + arr[j] + arr[k]是奇怪的。
例子:
Input: arr[] = {1, 2, 3, 4, 5}
Output: 4
Explanation: The given array contains 4 triplets with an odd sum. They are {1, 2, 4}, {1, 3, 5}, {2, 3, 4} and {2, 4, 5}.
Input: arr[] ={4, 5, 6, 4, 5, 10, 1, 7}
Output: 28
朴素方法:给定的问题可以通过遍历数组中所有可能的无序三元组来解决,并跟踪三元组的数量,使它们的总和为奇数。
时间复杂度: O(N 3 )
有效的方法:上述方法可以使用整数的以下属性进行优化:
- 奇数 + 偶数 + 偶数 = 奇数
- 奇数 + 奇数 + 奇数 = 奇数
因此,要实现上述思想,我们可以统计数组中偶数和奇数的个数。假设奇数的个数是O ,偶数个数是E 。所以排列一个奇数和两个偶数的不同方法是O C 1 * E C 2 -> (O * E * (E-1))/2排列三个奇数的不同方法是O C 3 - > (O * (O-1) * (O-2)) / 6 。最终答案将是上述两个值的总和。
下面是上述方法的实现:
C++
// C++ Program for the above approach
#include
using namespace std;
// Function to count the number of
// unordered triplets such that their
// sum is an odd integer
int countTriplets(int arr[], int n)
{
// Count the number of odd and
// even integers in the array
int odd = 0, even = 0;
for (int i = 0; i < n; i++) {
if (arr[i] & 1)
odd++;
else
even++;
}
// Number of ways to create triplets
// using one odd and two even integers
int c1 = odd * (even * (even - 1)) / 2;
// Number of ways to create triplets
// using three odd integers
int c2 = (odd * (odd - 1) * (odd - 2)) / 6;
// Return answer
return c1 + c2;
}
// Driver Code
int main()
{
int arr[] = { 4, 5, 6, 4, 5, 10, 1, 7 };
int n = sizeof(arr) / sizeof(int);
// Function Call
int ans = countTriplets(arr, n);
// Print Answer
cout << ans;
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG {
// Function to count the number of
// unordered triplets such that their
// sum is an odd integer
static int countTriplets(int arr[], int n)
{
// Count the number of odd and
// even integers in the array
int odd = 0, even = 0;
for (int i = 0; i < n; i++) {
if ((arr[i] & 1) != 0)
odd++;
else
even++;
}
// Number of ways to create triplets
// using one odd and two even integers
int c1 = odd * (even * (even - 1)) / 2;
// Number of ways to create triplets
// using three odd integers
int c2 = (odd * (odd - 1) * (odd - 2)) / 6;
// Return answer
return c1 + c2;
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 4, 5, 6, 4, 5, 10, 1, 7 };
int n = arr.length;
// Function Call
int ans = countTriplets(arr, n);
// Print Answer
System.out.println(ans);
}
}
// This code is contributed by code_hunt.
Python3
# Python Program for the above approach
# Function to count the number of
# unordered triplets such that their
# sum is an odd integer
def countTriplets(arr, n):
# Count the number of odd and
# even integers in the array
odd = 0
even = 0
for i in range(n):
if (arr[i] & 1):
odd+=1
else:
even+=1
# Number of ways to create triplets
# using one odd and two even integers
c1 = odd * (even * (even - 1)) // 2
# Number of ways to create triplets
# using three odd integers
c2 = (odd * (odd - 1) * (odd - 2)) // 6
# Return answer
return c1 + c2
# Driver Code
arr = [4, 5, 6, 4, 5, 10, 1, 7]
n = len(arr)
# Function Call
ans = countTriplets(arr, n)
# Print Answer
print(ans)
# This code is contributed by Shivani
C#
// C# program for the above approach
using System;
class GFG{
// Function to count the number of unordered
// triplets such that their sum is an odd integer
static int countTriplets(int []arr, int n)
{
// Count the number of odd and
// even integers in the array
int odd = 0, even = 0;
for(int i = 0; i < n; i++)
{
if ((arr[i] & 1) != 0)
odd++;
else
even++;
}
// Number of ways to create triplets
// using one odd and two even integers
int c1 = odd * (even * (even - 1)) / 2;
// Number of ways to create triplets
// using three odd integers
int c2 = (odd * (odd - 1) * (odd - 2)) / 6;
// Return answer
return c1 + c2;
}
// Driver code
public static void Main(String[] args)
{
int []arr = { 4, 5, 6, 4, 5, 10, 1, 7 };
int n = arr.Length;
// Function Call
int ans = countTriplets(arr, n);
// Print Answer
Console.Write(ans);
}
}
// This code is contributed by shivanisinghss2110.
Javascript
输出
28
时间复杂度: O(N)
辅助空间: O(1)