The Wayback Machine - https://web.archive.org/web/20211224042901/https://www.geeksforgeeks.org/count-of-triplets-that-can-be-removed-without-changing-mean-of-given-array/
Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Count of triplets that can be removed without changing Mean of given Array

  • Last Updated : 23 Dec, 2021

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: 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

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. Ther 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++




// C++ code for the above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to count the number of
// triplets with the given sum
int countTriplets(int arr[], int n, int sum)
{
    // Stores the final count
    int cnt = 0;
  
    // Map to store occured elements
    unordered_map<int, int> m;
  
    for (int i = 0; i < n - 1; i++) {
        for (int j = i + 1; j < n; j++) {
  
            // Check if Sum - (a + b)
            // is present in map
            int k = sum - (arr[i] + arr[j]);
            if (m.find(k) != m.end())
  
                // Increment count
                cnt += m[k];
        }
  
        // Store the occurences
        m[arr[i]]++;
    }
  
    // Return Answer
    return cnt;
}
  
// Function to C=find count of triplets
// that can be removed without changing
// arithmetic mean of the given array
int count_triplets(int arr[], int n)
{
    // Stores sum of all elements
    // of the given array
    int sum = 0;
  
    // Calculate the sum of the array
    for (int i = 0; i < n; i++) {
        sum = sum + arr[i];
    }
    // Store the arithmetic mean
    int mean = sum / n;
    int reqSum = 3 * mean;
  
    if ((3 * sum) % n != 0)
        return 0;
  
    // Return count
    return countTriplets(arr, n, reqSum);
}
  
// Driver Code
int main()
{
    int arr[] = { 5, 5, 5, 5 };
    int N = sizeof(arr) / sizeof(arr[0]);
    cout << count_triplets(arr, N);
  
    return 0;
}
Output
3

Time Complexity: O(N2)
Auxiliary Space: O(N) 




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!