<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) {

        if (*max_element(nums.begin(), nums.end()) -
                *min_element(nums.begin(), nums.end()) <
            1)
            return 0;
        std::sort(nums.begin(), nums.end());
        int ans = INT_MIN;
        std::deque<int> d1;
        for (int i = 0; i < nums.size(); i++) {
            std::cout <<"incoming nums[i] = "<<nums[i]<<endl;
            if (!d1.empty() &&
                (d1.front() == nums[i] - 1 || d1.front() == nums[i])) {
                    std::cout<<"pushing "<<nums[i]<<"\n";
                d1.push_back(nums[i]);
            } else if (d1.empty()) {
                std::cout<<"pushing "<<nums[i]<<"\n";
                d1.push_back(nums[i]);
            } else {
                std::cout<<"popping "<<d1.front()<<"\n";
                d1.pop_front();
               // if(d1.empty() ||nums[i]-d1.back() < 2 ) {
                    std::cout<<"pushing "<<nums[i]<<"\n";
                    d1.push_back(nums[i]);
             //   }
            }
            if (d1.back() - d1.front() == 1)
                ans = std::max(ans, (int)d1.size()); 
        }
        if (d1.front() == d1.back() || ans == INT_MIN)
            return 0;
        
        return ans;
    }
};
37900