在不改变给定数组的平均值的情况下可以删除的三元组数
给定一个数组arr[] ,任务是计算可能的三元组的计数,以便可以在不改变数组算术平均值的情况下将它们从数组中删除。
例子:
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
方法:给定的问题可以通过观察来解决,即为了使剩余数组的平均值保持不变,删除的三元组的平均值必须等于初始数组的平均值。因此,给定的问题被简化为找到具有给定总和的三元组的计数,这可以通过以下步骤使用散列来解决:
- 为(a, b)对的所有可能值迭代给定数组arr[]并将它们的总和插入到映射中。
- 在迭代数组时,检查 ( TargetSum – (a + b) ) 是否已经存在于地图中。如果是,则按其频率增加所需计数的值。
下面是上述方法的实现:
C++
// C++ code for the above approach
#include
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 occurred elements
unordered_map 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 occurrences
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;
}
Java
// Java code for the above approach
import java.util.HashMap;
class GFG {
// Function to count the number of
// triplets with the given sum
static int countTriplets(int[] arr, int n, int sum)
{
// Stores the final count
int cnt = 0;
// Map to store occurred elements
HashMap m = new HashMap<>();
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.containsKey(k))
// Increment count
cnt += m.get(k);
}
// Store the occurrences
if (m.containsKey(arr[i]))
m.put(arr[i],m.get(arr[i])+1);
else
m.put(arr[i],1);
}
// Return Answer
return cnt;
}
// Function to C=find count of triplets
// that can be removed without changing
// arithmetic mean of the given array
static 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
public static void main (String[] args)
{
int[] arr = { 5, 5, 5, 5 };
int N = arr.length;
System.out.println(count_triplets(arr, N));
}
}
// This code is contributed by Shubham Singh.
Python3
# python code for the above approach
# Function to count the number of
# triplets with the given sum
def countTriplets(arr, n, sum):
# Stores the final count
cnt = 0
# Map to store occurred elements
m = {}
for i in range(0, n-1):
for j in range(i+1, n):
# Check if Sum - (a + b)
# is present in map
k = sum - (arr[i] + arr[j])
if (k in m):
# Increment count
cnt += m[k]
# Store the occurrences
if arr[i] in m:
m[arr[i]] += 1
else:
m[arr[i]] = 1
# Return Answer
return cnt
# Function to C=find count of triplets
# that can be removed without changing
# arithmetic mean of the given array
def count_triplets(arr, n):
# Stores sum of all elements
# of the given array
sum = 0
# Calculate the sum of the array
for i in range(0, n):
sum = sum + arr[i]
# Store the arithmetic mean
mean = sum // n
reqSum = 3 * mean
if ((3 * sum) % n != 0):
return 0
# Return count
return countTriplets(arr, n, reqSum)
# Driver Code
if __name__ == "__main__":
arr = [5, 5, 5, 5]
N = len(arr)
print(count_triplets(arr, N))
# This code is contributed by rakeshsahni
C#
// C# code for the above approach
using System;
using System.Collections.Generic;
class GFG {
// Function to count the number of
// triplets with the given sum
static int countTriplets(int[] arr, int n, int sum)
{
// Stores the final count
int cnt = 0;
// Map to store occurred elements
Dictionary m = new Dictionary();
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.ContainsKey(k))
// Increment count
cnt += m[k];
}
// Store the occurrences
if (m.ContainsKey(arr[i]))
m[arr[i]]++;
else
m[arr[i]] = 1;
}
// Return Answer
return cnt;
}
// Function to C=find count of triplets
// that can be removed without changing
// arithmetic mean of the given array
static 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
public static void Main()
{
int[] arr = { 5, 5, 5, 5 };
int N = arr.Length;
Console.WriteLine(count_triplets(arr, N));
}
}
// This code is contributed by ukasp.
Javascript
输出:
4
时间复杂度: O(N 2 )
辅助空间: O(N)