Count occurrences of the average of array elements with a given number in C++

C++Server Side ProgrammingProgramming

Given an array arr[] containing integer elements and an integer num. The goal is to find the average of each element arr[i] and num and print the count of the number of times that average appeared in the original array.

If array arr[] is [ 5, 2, 3 ] and num is 2. Averages will be [ 3, 2, 2 ] occurrences in arr[] is [ 1,1,1 ]

For Example

Input

arr[] = { 1, 6, 4, 3, 6, 4 } num=2

Output

1 2 1 0 2 1

Count of occurrences of the average of array elements with a given number are − 5

Explanation

The num is 4 and averages with all other numbers in arr[] is :
[ 1, 4, 3, 2, 4, 3 ] occurrences of these in arr[]= [ 1, 2, 1, 0, 2, 1 ]

Input

arr[] = { 4, 8, 24, 16, 20, 40 } num=4

Output

1 0 0 0 0 0

Count of occurrences of the average of array elements with a given number are − 1

Explanation

The num is 4 and averages with all other numbers in arr[] is :
[ 4, 6, 14, 10, 12, 22 ] occurrences of these in arr[]= [ 1, 0, 0, 0, 0, 0 ]

Approach used in the below program is as follows

In this approach we will create a map for storing the averages and their count in the original array. We will add these counts to a separate array to print the number of occurrences.

  • Take an arr[] of integer type.

  • Take input num as integer.

  • Function occurrence_average(int arr[], int size, int num) takes the input array and num and prints the occurrence array of averages in arr[]. It returns the count of non−zero occurrences.

  • Take initial count=0.

  • Take a map<int,int> map_pair to store the count of unique numbers in arr[].

  • Take an array total[] for storing the counts of each average.

  • Traverse the array arr[] using for loop fromindex i=0 to i<size. For each element increment value for corresponding key using map_pair[arr[i]]++.

  • At the end of the loop we have unique numbers as keys and their counts in arr[] as values.

  • Traverse the array again using a for loop to calculate average of num with each individual element and store in temp.

  • If that temp is found in map_pair using map_pair.find(temp) != map_pair.end(), then add it to the array total.

  • Print the array total for counts of averages occurring in arr[]. For each non zero element, increment count.

  • Return count as result.

Example

 Live Demo

#include<bits/stdc++.h>
using namespace std;
int occurrence_average(int arr[], int size, int num){
   int count = 0;
   map<int,int> map_pair;
   int total[size] = {0};
   int val, av;
   for (int i = 0; i < size; i++){
      if (map_pair[arr[i]] == 0){
         map_pair[arr[i]] = 1;
      } else {
         map_pair[arr[i]]++;
      }
   }
   for (int i = 0; i < size; i++){
      int temp = int((arr[i] + num) / 2);
      if(map_pair.find(temp) != map_pair.end()){
         int set = map_pair[temp];
         total[i] = set;
      }
   }
   cout<<endl;
   for(int i=0;i<size;i++){
      cout<<total[i]<<" ";
      if(total[i]>0){
         count++;
      }
   }
   return count;
}
int main(){
   int arr[] = { 4, 8, 24, 16, 20, 40 };
   int size = sizeof(arr)/sizeof(arr[0]);
   int num = 4;
   cout<<endl<<"Count of occurrences of the average of array elements with a given
      number are: "<<occurrence_average(arr, size, num);
}

Output

If we run the above code it will generate the following output −

Count of occurrences of the average of array elements with a given number are:
1 0 0 0 0 0 1
raja
Published on 05-Jan-2021 09:28:33
Advertisements