```
public int findLHS(int[] nums) {
Arrays.sort(nums);
int max = 0;
int start;
int end = 0;
for (start = 0; start < nums.length && end < nums.length; start++) {
while (end < nums.length && nums[end] - nums[start] <= 1) {
end++;
}
if (nums[end - 1] == nums[start] + 1) {
max = Math.max(max, end - start);
}
}
return max;
}
```