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.
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
Constraints:
1 <= nums.length <= 2 * 104
-109 <= nums[i] <= 109
program main
! Solves the problem "Harmonious Array"
implicit none
integer, parameter :: n = 8
integer, dimension(n) :: nums
integer :: i, j, max_diff, min_diff
integer :: longest_harmonious_subsequence
! Examples
nums = [1, 3, 2, 2, 5, 2, 3, 7]
write (*, '(a, i0)') 'Example 1: ', longest_harmonious_subsequence(nums)
nums = [1, 2, 3, 4]
write (*, '(a, i0)') 'Example 2: ', longest_harmonious_subsequence(nums)
nums = [1, 1, 1, 1]
write (*, '(a, i0)') 'Example 3: ', longest_harmonious_subsequence(nums)
contains
function longest_harmonious_subsequence(nums) result(max_len)
implicit none
integer, intent(in) :: nums(:)
integer :: max_len, len, i, j
do i = 1, size(nums) - 1
do j = i + 1, size(nums)
if (abs(nums(j) - nums(i)) == 1) then
len = len + 1
end if
end do
if (len > max_len) then
max_len = len
end if
len = 0
end do
end function longest_harmonious_subsequence
end program main
temp.f95:20:44: 20 | function longest_harmonious_subsequence(nums) result(max_len) | 1 Error: Procedure ‘longest_harmonious_subsequence’ at (1) has an explicit interface from a previous declaration temp.f95:21:21: 21 | implicit none | 1 Error: Unexpected IMPLICIT NONE statement in CONTAINS section at (1) temp.f95:22:38: 22 | integer, intent(in) :: nums(:) | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:23:37: 23 | integer :: max_len, len, i, j | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:25:32: 25 | do i = 1, size(nums) - 1 | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:26:36: 26 | do j = i + 1, size(nums) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:27:53: 27 | if (abs(nums(j) - nums(i)) == 1) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:28:33: 28 | len = len + 1 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:29:19: 29 | end if | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:30:15: 30 | end do | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:31:35: 31 | if (len > max_len) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:32:29: 32 | max_len = len | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:33:15: 33 | end if | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:34:19: 34 | len = 0 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:35:11: 35 | end do | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:36:7: 36 | end function longest_harmonious_subsequence | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:13:4: 13 | nums = [1, 2, 3, 4] | 1 Error: Different shape for array assignment at (1) on dimension 1 (8 and 4) temp.f95:15:4: 15 | nums = [1, 1, 1, 1] | 1 Error: Different shape for array assignment at (1) on dimension 1 (8 and 4)
```
program longest_harmonious_subsequence
implicit none
integer, parameter :: INT_MAX = huge(1)
integer, parameter :: INT_MIN = -huge(1)
integer, dimension(:), allocatable :: nums
integer :: n, i, j, max_diff, diff
integer :: longest_subsequence
! Read the number of elements in the array
read(*,*) n
! Allocate memory for the array
allocate(nums(n))
! Read the elements of the array
do i = 1, n
read(*,*) nums(i)
end do
! Initialize the maximum difference and the longest subsequence
max_diff = 0
longest_subsequence = 0
! Iterate over the elements of the array
do i = 1, n - 1
! Iterate over the remaining elements of the array
do j = i + 1, n
! Calculate the difference between the current element and the next element
diff = abs(nums(i) - nums(j))
! If the difference is greater than the maximum difference, update the maximum difference
if (diff > max_diff) then
max_diff = diff
end if
! If the difference is exactly 1, update the longest subsequence
if (diff == 1) then
longest_subsequence = longest_subsequence + 1
end if
end do
end do
! Print the longest harmonious subsequence
write(*,*) longest_subsequence
! Deallocate the memory for the array
deallocate(nums)
end program longest_harmonious_subsequence
```
temp.f95:1:2: 1 | ``` | 1 Error: Invalid character in name at (1) temp.f95:52:2: 52 | ``` | 1 Error: Invalid character in name at (1)
def findLHS(nums):
count = {}
for num in nums:
count[num] = count.get(num, 0) + 1
longest_sequence = 0
for key in count:
if key + 1 in count:
longest_sequence = max(longest_sequence, count[key] + count[key + 1])
return longest_sequence
#include <vector>
#include <unordered_map>
using namespace std;
int findLHS(vector<int>& nums) {
unordered_map<int, int> count;
for (int num : nums)
count[num]++;
int longest_sequence = 0;
for (auto &elem : count) {
if (count.find(elem.first + 1) != count.end())
longest_sequence = max(longest_sequence, elem.second + count[elem.first + 1]);
}
return longest_sequence;
}