Count occurrences of the average of array elements with a given number
Last Updated :
10 Oct, 2022
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:
C++
#include<bits/stdc++.h>
using namespace std;
void getAverageCountArray( int a[], int x, int N)
{
map< int , int > mp;
int avg[N] = {0};
int val, av;
for ( int i = 0; i < N; i++)
{
if (mp[a[i]] == 0)
mp[a[i]] = 1;
else
mp[a[i]]++;
}
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;
}
}
for ( int i = 0; i < N; i++)
{
cout << avg[i] << " " ;
}
}
int main()
{
int a[] = { 2, 0, 4, 6, 2 };
int x = 2;
int N = sizeof (a)/ sizeof (a[0]);
getAverageCountArray(a, x, N);
}
|
Java
import java.io.*;
import java.util.*;
import java.lang.*;
class GFG {
static void getAverageCountArray( int [] a, int x, int N)
{
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
int [] avg = new int [N];
int val, av;
for ( int i = 0 ; i < N; i++) {
if (!map.containsKey(a[i])) {
map.put(a[i], 1 );
}
else {
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;
}
}
for ( int i = 0 ; i < N; i++) {
System.out.print(avg[i] + " " );
}
}
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
def getAverageCountArray(a, x, N):
map = {}
avg = [ 0 ] * N
for i in range (N):
if a[i] not in map :
map [a[i]] = 1
else :
map [a[i]] + = 1
for i in range (N):
av = (a[i] + x) / / 2
if av in map :
val = map [av]
avg[i] = val
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)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void getAverageCountArray( int [] a, int x,
int N)
{
Dictionary< int ,
int > map = new Dictionary< int ,
int >();
int [] avg = new int [N];
int val, av;
for ( int i = 0; i < N; i++)
{
if (!map.ContainsKey(a[i]))
{
map.Add(a[i], 1);
}
else
{
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;
}
}
for ( int i = 0; i < N; i++)
{
Console.Write(avg[i] + " " );
}
}
public static void Main()
{
int [] a = { 2, 0, 4, 6, 2 };
int x = 2;
int N = a.Length;
getAverageCountArray(a, x, N);
}
}
|
Javascript
<script>
function getAverageCountArray(a, x, N)
{
let map = new Map();
let avg = Array.from({length: N}, (_, i) => 0);
let val, av;
for (let i = 0; i < N; i++) {
if (!map.has(a[i])) {
map.set(a[i], 1);
}
else {
val = map.get(a[i]);
val++;
map. delete (a[i]);
map.set(a[i], val);
}
}
for (let i = 0; i < N; i++) {
av = (a[i] + x) / 2;
if (map.has(av)) {
val = map.get(av);
avg[i] = val;
}
}
for (let i = 0; i < N; i++) {
document.write(avg[i] + " " );
}
}
let a = [ 2, 0, 4, 6, 2 ];
let x = 2;
let N = a.length;
getAverageCountArray(a, x, N);
</script>
|
Time Complexity: O(n log n), where n is the size of the given array.
Auxiliary Space: O(n)
Share your thoughts in the comments
Please Login to comment...