Given an array of integers and an integer . For every integer of the array **a[i]**, the task is to calculate the count of numbers in the array with value equals to the **average of element a[i] and x**. That is, the number of occurrences of the (*average of element a[i] and x*) in the array.

**Examples:**

Input: arr[] = {2, 0, 4, 6, 2}, x = 2

Output: 2 0 0 1 2

For x = 2, the average values for 2, 0, 4, 6, 2 would be 2, 1, 3, 4, 2 respectively. So, the count array would result in 2, 0, 0, 1, 2.

Input: arr[] = {9, 5, 2, 4, 0, 3}, x = 3

Output: 0 1 1 1 0 1

For x = 3, the average values for 9, 5, 2, 4, 0, 3 would be 6, 4, 2, 3, 1, 2 respectively. So, the count array would result in 0, 1, 1, 1, 0, 1.

**Approach: **

- Traverse the array and map every element with its count of occurrence in the array.
- Now traverse the array again, take the average of the array element and given and check for its value in the map.

Below is the implementation of the above approach:

## 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 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<Integer, Integer> map = ` `new` `HashMap<Integer, Integer>(); ` ` ` ` ` `// 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); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find the count of ` `# occurrences of the average of array ` `# elements with a given number ` ` ` `# Function to 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 ` |

*chevron_right*

*filter_none*

**Output:**

2 0 0 1 2

## Recommended Posts:

- Count subarrays with equal number of occurrences of two given elements
- Count number of occurrences (or frequency) in a sorted array
- Average of remaining elements after removing K largest and K smallest elements from array
- Count number of elements between two given elements in array
- Count number of even and odd elements in an array
- Count the number of elements in an array which are divisible by k
- For each element in 1st array count elements less than or equal to it in 2nd array
- For each element in 1st array count elements less than or equal to it in 2nd array | Set 2
- Count elements present in first array but not in second
- Count and Sum of composite elements in an array
- Count distinct elements in an array
- Count of elements of an array present in every row of NxM matrix
- Count elements that are divisible by at-least one element in another array
- Count of index pairs with equal elements in an array
- Count elements less than or equal to a given value in a sorted rotated array

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.