problem link: https://leetcode.com/problems/longest-harmonious-subsequence
class Solution {
public:
int findLHS(vector& nums) {
int len=0;
unordered_map<int,int> eleTOfreq;
for(int i=0;i<nums.size();++i){
eleTOfreq[nums[i]]++;
}
//debugging contents of unordered_map
for(auto i:eleTOfreq){
cout<<i.first<<" "<<i.second<<endl;
}
for(auto it:eleTOfreq){
if(eleTOfreq[it.first+1]>0)
len=max(len,eleTOfreq[it.first]+eleTOfreq[it.first+1]);
//debugging no. of times map is iterated
cout<<len<<endl;
}
// usually for nums=[1,3,5,7,9,11,13]
//-above loop should iterate for 7 times but iterating for 18 TIMES!!!!!!!!!!!!!! HOWWW it is possible!!!!!!!!!
/*
13 1
11 1
9 1
7 1
5 1
3 1
1 1
0
0
0
0
0
0
0
0
0
0
0
0
0
1
1
1
1
1
*/
return len; //returning 1 but answer is 0 }
};