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.
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
Explanation:
The longest harmonious subsequences are [1,2], [2,3], and [3,4], all of which have a length of 2.
Example 3:
Input: nums = [1,1,1,1]
Output: 0
Explanation:
No harmonic subsequence exists.
Constraints:
1 <= nums.length <= 2 * 104-109 <= nums[i] <= 109When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method for finding the longest harmonious subsequence is all about trying every possible combination. We'll examine each possible group of numbers within the input and see if it meets the harmonious criteria. Then, we'll keep track of the longest one we find that fits the rules.
Here's how the algorithm would work step-by-step:
def longest_harmonious_subsequence_brute_force(numbers):
max_length_harmonious_subsequence = 0
# Iterate through all possible subsequences
for i in range(1 << len(numbers)):
subsequence = []
for j in range(len(numbers)):
# Construct the subsequence
if (i >> j) & 1:
subsequence.append(numbers[j])
# Skip empty subsequences
if not subsequence:
continue
# Check if subsequence is harmonious
min_value_subsequence = min(subsequence)
max_value_subsequence = max(subsequence)
# Harmonious if difference between max and min is 1
if max_value_subsequence - min_value_subsequence == 1:
current_subsequence_length = len(subsequence)
# Keep track of the maximum length
if current_subsequence_length > max_length_harmonious_subsequence:
max_length_harmonious_subsequence = current_subsequence_length
return max_length_harmonious_subsequenceThe key to efficiently finding the longest harmonious subsequence is to focus on counting the frequency of each number, not comparing every pair. We can significantly reduce the work needed by cleverly organizing how we assess harmonious possibilities. By only counting numbers and their neighbors, we determine the longest sequence.
Here's how the algorithm would work step-by-step:
def find_longest_harmonious_subsequence(number_list):
number_counts = {}
for number in number_list:
number_counts[number] = number_counts.get(number, 0) + 1
longest_harmonious_sequence = 0
# Iterate through each unique number to find harmonious pairs.
for number in number_counts:
# Check if the number + 1 also exists in the list.
if (number + 1) in number_counts:
# Calculating the length of the harmonious subsequence.
harmonious_length = number_counts[number] + number_counts[number + 1]
# Keep track of the largest harmonious subsequence length.
longest_harmonious_sequence = max(longest_harmonious_sequence, harmonious_length)
return longest_harmonious_sequence| Case | How to Handle |
|---|---|
| Empty input array | Return 0 since there is no subsequence in an empty array. |
| Input array with a single element | Return 0 because a harmonious subsequence needs at least two elements with a difference of 1. |
| Input array contains only identical numbers | Return 0 since the difference between the maximum and minimum of such a subsequence is 0, not 1. |
| Input array with extremely large positive or negative numbers | The solution should correctly handle large numbers if implemented properly with integer types that have sufficient range (e.g., long in Java, int64 in Go, etc.) and avoid overflow. |
| Input array contains duplicates of numbers which are 1 apart. | The frequency map will accurately reflect the counts of each number, ensuring the correct length is calculated when checking for harmonious subsequences. |
| No harmonious subsequence exists in the input array | The solution should correctly identify that no harmonious subsequence is present and return 0. |
| Array with a large number of elements, potentially causing memory issues if not handled efficiently. | Using a hash map to store element counts ensures that the solution has a time complexity of O(n) and a space complexity of O(n), avoiding memory issues for reasonable input sizes. |
| Input array contains both positive and negative integers, as well as zero | The difference calculations and comparison will still operate correctly, as long as no integer overflow happens. |