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++
#include <iostream>
using namespace std;
int countTriplets( int arr[], int n)
{
int odd = 0, even = 0;
for ( int i = 0; i < n; i++) {
if (arr[i] & 1)
odd++;
else
even++;
}
int c1 = odd * (even * (even - 1)) / 2;
int c2 = (odd * (odd - 1) * (odd - 2)) / 6;
return c1 + c2;
}
int main()
{
int arr[] = { 4, 5, 6, 4, 5, 10, 1, 7 };
int n = sizeof (arr) / sizeof ( int );
int ans = countTriplets(arr, n);
cout << ans;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int countTriplets( int arr[], int n)
{
int odd = 0 , even = 0 ;
for ( int i = 0 ; i < n; i++) {
if ((arr[i] & 1 ) != 0 )
odd++;
else
even++;
}
int c1 = odd * (even * (even - 1 )) / 2 ;
int c2 = (odd * (odd - 1 ) * (odd - 2 )) / 6 ;
return c1 + c2;
}
public static void main(String[] args)
{
int arr[] = { 4 , 5 , 6 , 4 , 5 , 10 , 1 , 7 };
int n = arr.length;
int ans = countTriplets(arr, n);
System.out.println(ans);
}
}
|
Python3
def countTriplets(arr, n):
odd = 0
even = 0
for i in range (n):
if (arr[i] & 1 ):
odd + = 1
else :
even + = 1
c1 = odd * (even * (even - 1 )) / / 2
c2 = (odd * (odd - 1 ) * (odd - 2 )) / / 6
return c1 + c2
arr = [ 4 , 5 , 6 , 4 , 5 , 10 , 1 , 7 ]
n = len (arr)
ans = countTriplets(arr, n)
print (ans)
|
C#
using System;
class GFG{
static int countTriplets( int []arr, int n)
{
int odd = 0, even = 0;
for ( int i = 0; i < n; i++)
{
if ((arr[i] & 1) != 0)
odd++;
else
even++;
}
int c1 = odd * (even * (even - 1)) / 2;
int c2 = (odd * (odd - 1) * (odd - 2)) / 6;
return c1 + c2;
}
public static void Main(String[] args)
{
int []arr = { 4, 5, 6, 4, 5, 10, 1, 7 };
int n = arr.Length;
int ans = countTriplets(arr, n);
Console.Write(ans);
}
}
|
Javascript
<script>
function countTriplets(arr, n)
{
let odd = 0, even = 0;
for (let i = 0; i < n; i++) {
if (arr[i] & 1)
odd++;
else
even++;
}
let c1 = Math.floor(odd * (even * (even - 1)) / 2);
let c2 = Math.floor((odd * (odd - 1) * (odd - 2)) / 6);
return c1 + c2;
}
let arr = [4, 5, 6, 4, 5, 10, 1, 7];
let n = arr.length;
let ans = countTriplets(arr, n);
document.write(ans);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...