Count of triplets in an Array with odd sum
Last Updated :
13 Sep, 2021
Given an array arr[] with N integers, find the number of triplets of i, j and k such that 1<= i < j < k <= N and arr[i] + arr[j] + arr[k] is odd.
Example:
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
Naive Approach: The given problem can be solved by iterating over all possible unordered triplets in the array and keep a track of the number of triplets such that their sum is odd.
Time Complexity: O(N3)
Efficient Approach: The above approach can be optimized using the below property of integers:
- odd + even + even = odd
- odd + odd + odd = odd
Therefore, to implement the above idea, we can count the number of even and odd integers in the array. Suppose the count of odd integers is O and the count of even integers is E. So the distinct ways to arrange one odd integer and two even integers is OC1 * EC2 -> (O * E * (E-1))/2 and the distinct ways to arrange three odd integers is OC3 -> (O * (O-1) * (O-2)) / 6. The final answer will be the sum of the above two values.
Below is the implementation of the above approach:
C++
// C++ Program for the above approach
#include <iostream>
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
<script>
// JavaScript program for the above approach;
// Function to count the number of
// unordered triplets such that their
// sum is an odd integer
function countTriplets(arr, n)
{
// Count the number of odd and
// even integers in the array
let odd = 0, even = 0;
for (let 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
let c1 = Math.floor(odd * (even * (even - 1)) / 2);
// Number of ways to create triplets
// using three odd integers
let c2 = Math.floor((odd * (odd - 1) * (odd - 2)) / 6);
// Return answer
return c1 + c2;
}
// Driver Code
let arr = [4, 5, 6, 4, 5, 10, 1, 7];
let n = arr.length;
// Function Call
let ans = countTriplets(arr, n);
// Print Answer
document.write(ans);
// This code is contributed by Potta Lokesh
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
3 Sum - Count Triplets With Given Sum In Sorted Array Given a sorted array arr[] and a target value, the task is to find the count of triplets present in the given array having sum equal to the given target. More specifically, the task is to count triplets (i, j, k) of valid indices, such that arr[i] + arr[j] + arr[k] = target and i < j < k.Examp
10 min read
3 Sum - Count all triplets with given sum Given an array arr[] and a target value, the task is to find the count of triplets present in the given array having sum equal to the given target. Examples:Input: arr[] = [0, -1, 2, -3, 1], target = -2Output: 2Explanation: Two triplets that add up to -2 are:arr[0] + arr[3] + arr[4] = 0 + (-3) + (1)
10 min read
Count of sub-arrays with odd product Given an integer array arr[] of size N, the task is to count the number of sub-arrays that have an odd product.Examples: Input : arr[] = {5, 1, 2, 3, 4} Output : 4 Explanation: The sub-arrays with odd product are- {5}, {1}, {3}, {5, 1}. Hence the count is 4.Input : arr[] = {12, 15, 7, 3, 25, 6, 2, 1
7 min read
Count of even and odd power pairs in an Array Given an array arr[] of length N, the task is to count the number of pairs (X, Y) such that XY is even and count the number of pairs such that XY is odd.Examples: Input: arr[] = {2, 3, 4, 5} Output: 6 6 Explanation: (2, 3), (2, 4), (2, 5), (4, 2), (4, 3) and (4, 5) are the pairs with even values and
5 min read
Count of odd and even sum pairs in an array Given an array arr[] of N positive integers, the task is to find the number of pairs with odd sum and the number of pairs with even sum.Examples: Input: arr[] = {1, 2, 3, 4, 5} Output: Odd pairs = 6 Even pairs = 4Input: arr[] = {7, 4, 3, 2} Output: Odd pairs = 4 Even pairs = 2 Naive Approach: The na
8 min read