- Related Questions & Answers
- Count subarrays with equal number of occurrences of two given elements in C++
- Count number of elements between two given elements in array in C++
- Count of matrices (of different orders) with given number of elements in C++
- Count number of occurrences (or frequency) in a sorted array in C++
- Unique number of occurrences of elements in an array in JavaScript
- Count number of elements in an array with MongoDB?
- How to count the number of elements in an array below/above a given number (JavaScript)
- Count number of smallest elements in given range in C++
- C++ Program to Find the Number of occurrences of a given Number using Binary Search approach
- How to create permutation of array with the given number of elements in JavaScript
- Count Occurrences of Anagrams in C++
- Count number of occurrences for each char in a string with JavaScript?
- Count the number of occurrences of a string in a VARCHAR field in MySQL?
- How to count number of occurrences of repeated names in an array - JavaScript?
- Count of number of given string in 2D character array in C++

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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**

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

1 2 1 0 2 1

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

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 ]

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

1 0 0 0 0 0

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

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.

#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); }

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

Advertisements