<p>We define a harmonious array as an array where the difference between its maximum value and its minimum value is <b>exactly</b> <code>1</code>.</p>

<p>Given an integer array <code>nums</code>, return <em>the length of its longest harmonious subsequence among all its possible subsequences</em>.</p>

<p>A <strong>subsequence</strong> 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.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

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

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> 2
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,1,1,1]
<strong>Output:</strong> 0
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>4</sup></code></li>
	<li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
</ul>
class Solution {
public:
    int findLHS(vector<int>& nums) {
        /*
        nums = [1,3,2,2,5,2,3,7]
        set = [1,2,3,5,7];
        1 -> (1-1=0) is present in the set or not
        2 -> (2-1=1) is present in the set
                max = 2 and min = 1
                travewrsing the nums array
                and now i will storing those elements whicherver come in between min and max;
        */

        int n = nums.size();
        set<int> temp;
        for(int i=0; i<n; i++){
            if(temp.count(nums[i])==0){
                temp.insert(nums[i]);  
            }
        }
        int count = 0;
        for(auto i: temp){
            if(temp.count(i-1)>0){
                int min = i-1;
                int maxi = i;
                vector<int> ans;
                for(int j=0; j<n; j++){
                    if(min <= nums[j] && nums[j] <= maxi){
                        ans.push_back(nums[j]);
                    }
                }
                int length =ans.size();
                count = max(length, count);
            }
        }
        return count;
    }
};
50900