problem no 72: find the difference between highest occurence and lowest occurance of any element inside an array.
Given an array, the task is to find the difference between highest occurrence and lowest occurrence of any number in an array.
Note: If only one type of element is present in the array return 0.
Example 1:
Input:
arr[] = [1, 2, 2]
Output:
1
Explanation:
Lowest occurring element (1) occurs once.
Highest occurring element (2) occurs 2 times
Example 2:
Input :
arr[] = [7, 8, 4, 5, 4, 1, 1, 7, 7, 2, 5]
Output :
2
Explanation :
Lowest occurring element (5) occurs once.
Highest occurring element (1 or 7) occurs 3 times
Example 3:
Input :
arr[] = [1, 1, 1, 3, 3, 3]
Output :
0
Your Task:
You don't need to read input or print anything. Your task is to complete the function leftElement() which takes the array arr[] and its size N as inputs and returns the difference between highest occurrence and lowest occurrence of any number in the array.
code:
int findDiff(int arr[], int n) {
map<int ,int > mapp;
for(int i=0;i<n;i++)
{
mapp[arr[i]]++;
}
int min=INT32_MAX;
int max=INT32_MIN;
for(auto ele:mapp)
{
if(ele.second<min)
{
min=ele.second;
}
if(ele.second>max)
{
max=ele.second;
}
}
return max-min;
}
Comments
Post a Comment