Count of triplets that can be removed without changing Mean of given Array
Last Updated :
12 Jan, 2023
Given an array arr[], the task is to calculate the count of possible triplets such that they can be removed from the array without changing the arithmetic mean of the array.
Example:
Input: arr[] = {8, 7, 4, 6, 3, 0, 7}
Output: 3
Explanation: The given array has 3 possible triplets such that removing them will not affect the arithmetic mean of the array. There are {7, 3, 0}, {4, 6, 0} and {3, 0, 7}.
Input: arr[] = {5, 5, 5, 5}
Output: 4
Approach: The given problem can be solved using the observation that for the mean of the remaining array to be constant, the mean of the removed triplet must be equal to the mean of the initial array. Hence the given problem is reduced to finding the count of triplets with the given sum which can be solved using hashing by following the below steps:
- Iterate the given array arr[] for all possible values of pairs (a, b) and insert their sum into a map.
- While iterating the array, check if (TargetSum – (a + b)) already exists in the map. If yes, then increment the value of the required count by its frequency.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countTriplets( int arr[], int n, int sum)
{
int cnt = 0;
unordered_map< int , int > m;
for ( int i = 0; i < n - 1; i++) {
for ( int j = i + 1; j < n; j++) {
int k = sum - (arr[i] + arr[j]);
if (m.find(k) != m.end())
cnt += m[k];
}
m[arr[i]]++;
}
return cnt;
}
int count_triplets( int arr[], int n)
{
int sum = 0;
for ( int i = 0; i < n; i++) {
sum = sum + arr[i];
}
int mean = sum / n;
int reqSum = 3 * mean;
if ((3 * sum) % n != 0)
return 0;
return countTriplets(arr, n, reqSum);
}
int main()
{
int arr[] = { 5, 5, 5, 5 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << count_triplets(arr, N);
return 0;
}
|
Java
import java.util.HashMap;
class GFG {
static int countTriplets( int [] arr, int n, int sum)
{
int cnt = 0 ;
HashMap<Integer, Integer> m = new HashMap<>();
for ( int i = 0 ; i < n - 1 ; i++) {
for ( int j = i + 1 ; j < n; j++) {
int k = sum - (arr[i] + arr[j]);
if (m.containsKey(k))
cnt += m.get(k);
}
if (m.containsKey(arr[i]))
m.put(arr[i],m.get(arr[i])+ 1 );
else
m.put(arr[i], 1 );
}
return cnt;
}
static int count_triplets( int [] arr, int n)
{
int sum = 0 ;
for ( int i = 0 ; i < n; i++) {
sum = sum + arr[i];
}
int mean = sum / n;
int reqSum = 3 * mean;
if (( 3 * sum) % n != 0 )
return 0 ;
return countTriplets(arr, n, reqSum);
}
public static void main (String[] args)
{
int [] arr = { 5 , 5 , 5 , 5 };
int N = arr.length;
System.out.println(count_triplets(arr, N));
}
}
|
Python3
def countTriplets(arr, n, sum ):
cnt = 0
m = {}
for i in range ( 0 , n - 1 ):
for j in range (i + 1 , n):
k = sum - (arr[i] + arr[j])
if (k in m):
cnt + = m[k]
if arr[i] in m:
m[arr[i]] + = 1
else :
m[arr[i]] = 1
return cnt
def count_triplets(arr, n):
sum = 0
for i in range ( 0 , n):
sum = sum + arr[i]
mean = sum / / n
reqSum = 3 * mean
if (( 3 * sum ) % n ! = 0 ):
return 0
return countTriplets(arr, n, reqSum)
if __name__ = = "__main__" :
arr = [ 5 , 5 , 5 , 5 ]
N = len (arr)
print (count_triplets(arr, N))
|
C#
using System;
using System.Collections.Generic;
class GFG {
static int countTriplets( int [] arr, int n, int sum)
{
int cnt = 0;
Dictionary< int , int > m = new Dictionary< int , int >();
for ( int i = 0; i < n - 1; i++) {
for ( int j = i + 1; j < n; j++) {
int k = sum - (arr[i] + arr[j]);
if (m.ContainsKey(k))
cnt += m[k];
}
if (m.ContainsKey(arr[i]))
m[arr[i]]++;
else
m[arr[i]] = 1;
}
return cnt;
}
static int count_triplets( int [] arr, int n)
{
int sum = 0;
for ( int i = 0; i < n; i++) {
sum = sum + arr[i];
}
int mean = sum / n;
int reqSum = 3 * mean;
if ((3 * sum) % n != 0)
return 0;
return countTriplets(arr, n, reqSum);
}
public static void Main()
{
int [] arr = { 5, 5, 5, 5 };
int N = arr.Length;
Console.WriteLine(count_triplets(arr, N));
}
}
|
Javascript
<script>
const countTriplets = (arr, n, sum) => {
let cnt = 0;
let m = {};
for (let i = 0; i < n - 1; i++) {
for (let j = i + 1; j < n; j++) {
let k = sum - (arr[i] + arr[j]);
if (k in m)
cnt += m[k];
}
if (arr[i] in m) m[arr[i]]++;
else m[arr[i]] = 1;
}
return cnt;
}
const count_triplets = (arr, n) => {
let sum = 0;
for (let i = 0; i < n; i++) {
sum = sum + arr[i];
}
let mean = parseInt(sum / n);
let reqSum = 3 * mean;
if ((3 * sum) % n != 0)
return 0;
return countTriplets(arr, n, reqSum);
}
let arr = [5, 5, 5, 5];
let N = arr.length;
document.write(count_triplets(arr, N));
</script>
|
Output:
4
Time Complexity: O(N2), since there runs a nested loop both for N times.
Auxiliary Space: O(N), since N extra space is taken for hashing values.
Share your thoughts in the comments
Please Login to comment...