📌  相关文章
📜  计算具有给定数量的数组元素平均值的出现次数

📅  最后修改于: 2021-10-27 07:31:56             🧑  作者: Mango

给定一个数组N 整数和整数x .对于数组a[i] 的每个整数,任务是计算数组中值等于元素 a[i] 和 x 平均值的数字的计数。即,数组中(元素 a[i] 和 x 的平均值)的出现次数。
例子:

方法:

  1. 遍历数组并映射每个元素及其在数组中的出现次数。
  2. 现在再次遍历数组,取数组元素的平均值并给出x 并在地图中检查它的值。

下面是上述方法的实现:

C++
// CPP program to find the count of
// occurrences of the average of array
// elements with a given number
#include
 
using namespace std;
 
 
    // Function to find the count of
    // occurrences of the average of array
    // elements with a given number
    void getAverageCountArray(int a[], int x, int N)
    {
        // mp to store count of occurrence
        // of every array element in the array
        map mp;
 
        // Array that stores the average
        // count for given array
        int avg[N] = {0};
        int val, av;
 
        for (int i = 0; i < N; i++)
        {
            // first occurrence of a[i]
            if (mp[a[i]] == 0)
                mp[a[i]] = 1;
             
            else
            mp[a[i]]++;
             
 
            // element has already occurred before
            // so increase its count
        }
 
        for (int i = 0; i < N; i++)
        {
            av = int((a[i] + x) / 2);
            if (mp.find(av) != mp.end())
            {
                val = mp[av];
                avg[i] = val;
            }
        }
 
        // Printing the average count array
        for (int i = 0; i < N; i++)
        {
            cout << avg[i] << " ";
        }
    }
 
    // Driver code
    int main()
    {
        int a[] = { 2, 0, 4, 6, 2 };
        int x = 2;
 
        int N = sizeof(a)/sizeof(a[0]);
        getAverageCountArray(a, x, N);
    }
 
// This code is contributed by
// Surendra_Gangwar


Java
// Java program to find the count of
// occurrences of the average of array
// elements with a given number
import java.io.*;
import java.util.*;
import java.lang.*;
 
class GFG {
 
    // Function to find the count of
    // occurrences of the average of array
    // elements with a given number
    static void getAverageCountArray(int[] a, int x, int N)
    {
        // Map to store count of occurrence
        // of every array element in the array
        HashMap map = new HashMap();
 
        // Array that stores the average
        // count for given array
        int[] avg = new int[N];
        int val, av;
 
        for (int i = 0; i < N; i++) {
            // first occurrence of a[i]
            if (!map.containsKey(a[i])) {
                map.put(a[i], 1);
            }
 
            // element has already occurred before
            // so increase its count
            else {
                // gives current count of a[i]
                val = map.get(a[i]);
                val++;
                map.remove(a[i]);
                map.put(a[i], val);
            }
        }
 
        for (int i = 0; i < N; i++) {
            av = (a[i] + x) / 2;
            if (map.containsKey(av)) {
                val = map.get(av);
                avg[i] = val;
            }
        }
 
        // Printing the average count array
        for (int i = 0; i < N; i++) {
            System.out.print(avg[i] + " ");
        }
    }
 
    // Driver code
    public static void main(String args[])
    {
        int[] a = { 2, 0, 4, 6, 2 };
        int x = 2;
 
        int N = a.length;
 
        getAverageCountArray(a, x, N);
    }
}


Python3
# Python3 program to find the count of
# occurrences of the average of array
# elements with a given number
 
# Function to find the count of
# occurrences of the average of array
# elements with a given number
def getAverageCountArray(a, x, N):
      
    # Dictionary to store count of occurrence
    # of every array element in the array
    map = {}
     
    # Array that stores the average
    # count for given array
    avg = [0] * N
   
    for i in range(N): 
        # first occurrence of a[i]
        if a[i] not in map: 
            map[a[i]] =  1
              
        # element has already occurred before
        # so increase its count
        else:
            # gives current count of a[i]
            map[a[i]] += 1
             
    for i in range(N): 
        av = (a[i] + x) // 2
         
        if av in map:
            val = map[av]
            avg[i] = val
              
    # Printing the average count array
    for i in range(N):
        print(avg[i], end = " ")
          
      
if __name__ == "__main__":
      
    a = [2, 0, 4, 6, 2] 
    x = 2
   
    N = len(a)
    getAverageCountArray(a, x, N)
     
# This code is contributed by Rituraj Jain


C#
// C# program to find the count of
// occurrences of the average of array
// elements with a given number
using System;
using System.Collections.Generic;
 
class GFG
{
 
// Function to find the count of
// occurrences of the average of array
// elements with a given number
static void getAverageCountArray(int[] a, int x,
                                          int N)
{
    // Map to store count of occurrence
    // of every array element in the array
    Dictionary map = new Dictionary();
 
    // Array that stores the average
    // count for given array
    int[] avg = new int[N];
    int val, av;
 
    for (int i = 0; i < N; i++)
    {
        // first occurrence of a[i]
        if (!map.ContainsKey(a[i]))
        {
            map.Add(a[i], 1);
        }
 
        // element has already occurred before
        // so increase its count
        else
        {
            // gives current count of a[i]
            val = map[a[i]];
            val++;
            map.Remove(a[i]);
            map.Add(a[i], val);
        }
    }
 
    for (int i = 0; i < N; i++)
    {
        av = (a[i] + x) / 2;
        if (map.ContainsKey(av))
        {
            val = map[av];
            avg[i] = val;
        }
    }
 
    // Printing the average count array
    for (int i = 0; i < N; i++)
    {
        Console.Write(avg[i] + " ");
    }
}
 
// Driver code
public static void Main()
{
    int[] a = { 2, 0, 4, 6, 2 };
    int x = 2;
 
    int N = a.Length;
 
    getAverageCountArray(a, x, N);
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
2 0 0 1 2

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程