We define a harmonious array as an array where the difference between its maximum value and its minimum value is exactly 1.

Given an integer array nums, return the length of its longest harmonious subsequence among all its possible subsequences.

A subsequence of array is a sequence that can be derived from the array by deleting some or no elements without changing the order of the remaining elements.

&nbsp;
Example 1:

Input: nums = [1,3,2,2,5,2,3,7]
Output: 5
Explanation: The longest harmonious subsequence is [3,2,2,2,3].


Example 2:

Input: nums = [1,2,3,4]
Output: 2


Example 3:

Input: nums = [1,1,1,1]
Output: 0


&nbsp;
Constraints:


	1 &lt;= nums.length &lt;= 2 * 104
	-109 &lt;= nums[i] &lt;= 109

""
from collections import Counter
class Solution:
	def findLHS(self, nums: List[int]) -> int:
		counter=Counter(nums)
		# values=set(nums)
		res=0
		# if len(values)==1:return 0
		for num in nums:
			if num+1 in counter or num-1 in counter:
				res=max(res,counter[num]+counter.get(num+1,0))
				res=max(res,counter[num]+counter.get(num-1,0))

		return res
""
class Solution {
    public static int firstOccurence(int[] arr,int target)
    {
        int res=-1;
        int start=0;
        int end=arr.length-1;
        while(start<=end)
        {
            int mid=start + (end-start)/2;
            if(arr[mid]==target)
            {
                res=mid;
                end=mid-1;
            }
            else if(arr[mid]<target)
            {
                start=mid+1;
            }
            else
            {
                end=mid-1;
            }
        }
        return res;
    }
    public static int lastOccurence(int[] arr,int target)
    {
        int res=-1;
        int start=0;
        int end=arr.length-1;
        while(start<=end)
        {
            int mid=start + (end-start)/2;
            if(arr[mid]==target)
            {
                res=mid;
                start=mid+1;
            }
            else if(arr[mid]<target)
            {
                start=mid+1;
            }
            else
            {
                end=mid-1;
            }
        }
        return res;
    }
    public int findLHS(int[] nums) {
        Arrays.sort(nums);
        int maxLen=0;
        for(int i=0;i<nums.length-1;i++)
        {
            if(nums[i+1]==nums[i]+1)
            {
            int a=firstOccurence(nums,nums[i]);
            int b=lastOccurence(nums,nums[i]+1);
            if(b-a+1>maxLen)
                maxLen=b-a+1;
            }
        }
        return maxLen;
    }
}
class Solution {
public:
    int findLHS(vector<int>& nums)
    {
        map<int,int> m;
        for(int i=0;i<nums.size();i++)
        {
            m[nums[i]]++;
        }
        int ans=0;
        int x=-1,y=0;
        for(auto i=m.begin();i!=m.end();i++)
        {
            if(i->first-x==1)
            {
                ans=max(ans,y+i->second);
            }
            x=i->first;
            y=i->second;
        }
        return ans;

    }
};
//if you like the solution plz upvote.
var findLHS = function(nums) {
    let countNumber = {};
    let result = [];
    for (let i=0; i<nums.length; i++) {
        if (!countNumber[nums[i]]) {
            countNumber[nums[i]] = 1;
        } else {
            countNumber[nums[i]]++;
        }
    }
    for (let value of Object.keys(countNumber)) {
        let count;
        let current = parseInt(value);
        if (countNumber[current + 1]) {
            count = countNumber[current] + countNumber[current + 1];    
            result.push(count);
        }
    };
    if (result.length === 0) {
        return 0;
    }
    return Math.max(...result);
};
Longest Harmonious Subsequence