all test cases are not passing
Element with max frequency
If two or more elements contend for the maximum frequency, return the element which occurs in the array first.
for input
13
2 11 2 11 11 2 11 2 2 11 11 2 6
your output
11
but correct output is 2
modified code
#include<unordered_map>
#include<algorithm>
int highestFrequency(int *input, int n){
/* Don't write main().
* the input array is already passed as function argument.
* Taking input and printing output is handled automatically.
*/
unordered_map<int,int>m;
for(int i=0;i<n;i++){
m[input[i]] = m[input[i]]+1;
}
int freq = 0;
int key = 0;
for(auto p:m){
if(freq==p.second){
auto itr2=find(input,input+n,key);
auto itr1=find(input,input+n,p.first);
if(itr1<itr2){
key=p.first;
}
}
if(freq<p.second){
freq = p.second;
key = p.first;
}
}
return key;
}
i hope this helps
if yes hit a like and don’t forgot to mark doubt as resolved
if you have more doubts regarding this feel free to ask
for this input ???
13
2 11 2 11 11 2 11 2 2 11 11 2 6
check again
you have not consider this
If two or more elements contend for the maximum frequency, return the element which occurs in the array first.
yes got it, i have another dbt
this below code is also giving error
#include<unordered_map>
int lengthOfLongestSubsetWithZeroSum(int* arr, int size){
// Write your code here
unordered_map<int,int> m;
int pre = 0;
int len = 0;
for(int i=0;i<size;i++){
pre+=arr[i];
if(arr[i]==0 && len==0){
len = 1;
}
if(pre == 0){
len = max(len,i+1);
}
if(m.find(pre)!=m.end()){
len = max(len, i-m[pre]);
}
else{
m[pre] = i;
}
}
return len;
}
i am glad to solve your doubt
could you please post a new doubt for this
i have few doubts… can i please get your contact number
as raising dbts is a very time consuming process
this code is not passing all the test cases
#include
#include<unordered_map>
#include
using namespace std;
int highestFrequency(int arr[],int n){
unordered_map<int,int>m;
for(int i=0;i<n;i++){
m[arr[i]] = m[arr[i]] + 1;
}
int freq = 0;
int key;
for(auto p:m){
if(freq == p.second){
auto itr2 = find(arr,arr+n,key);
auto itr1 = find(arr,arr+n,p.first);
if(itr1<itr2){
key = p.first;
}
}
if(freq<p.second){
key = p.first;
freq = p.second;
}
}
return key;
}
int main() {
unordered_map<int,int>m;
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
int ans = highestFrequency(arr,n);
cout<<ans;
return 0;
}
the code is same to the code i provide
or you have done some changes
because when i submit it has passed all testcase
and also send link of code please
yes this approach will give tle in 2 testcase on hacker blocks
watch hint video of same for optimised code