Longest Harmonious Subsequence in JavaAn array of n integers is given to us. The task is to find the size of the longest harmonious subsequence of the array. A subsequence is said to be harmonious if the difference between the maximum value element and the minimum value element in the subsequence is 1. Example 1: Input int inArr1[] = {11, 12, 12, 13, 14, 15, 11, 11, 11, 11} Output 7 Explanation: The longest harmonious subsequence is {11, 12, 12, 11, 11, 11, 11} whose size is 7. Hence, the output is 7. Example 2: Input int inArr1[] = {0, 1, 5, 8, 10, 19, 15, 25, 30, 40, 45, 7, 11} Output 2 Explanation: The longest harmonious subsequences are {0, 1} or {7, 8}, and {10, 11}. Each harmonious subsequence is of size 2. Hence, the output is 2. Brute Force Approach: Using Bit MaskingThe concept is to generate all of the possible subsequences with the help of the bit masking technique. For every subsequence, check whether the subsequence is the Harmonious subsequence or not. Take out the maximum length of all of the valid subsequences that are possible. Observe the following steps: Step 1: Take a variable ans. Initialize it with the value 0. The variable "ans" will contain the final answer. Step 2: Iterate from 'j' = 0 to '2len - 1', where 'len is the length of the input array, 'inputArr[]'.
Step 3: Return the answer 'ans'. Now, let's observe the implementation of the above algorithm. FileName: LongestHarmoniousSubsequence.java Output: For the input array: 11 12 12 13 14 15 11 11 11 11 The length of the longest Harmonic subsequence is: 7 For the input array: 0 1 5 8 10 19 15 25 30 40 45 7 11 The length of the longest Harmonic subsequence is: 2 Complexity Analysis: The program is generation all of the binary values from 0 to 2len - 1. After that, the program is iteration through the array. Therefore, the overall time complexity of the program is O(len x 2len), where 'len' is the length of the input array. Also, the program is not using extra space, making the space complexity of the program O(1). The time complexity of the program clearly indicates that it is not suitable for larger inputs. Therefore, it is required to do some optimization to reduce the time complexity. OptimizationThe concept is to pivot every element of the array and then take only those elements that are equal or have a difference of 1 such that the maximum difference in the subsequence becomes 1. Observe the following steps. Step 1: Take a variable ans. Initialize it with the value 0. The variable "ans" will contain the final answer. Step 2: Iterate from 'j' = 0 to 'len - 1':
Step 3: Return 'ans' as the final answer. Now, let's observe the implementation of the above algorithm. FileName: LongestHarmonioussubsequence1.java Output: For the input array: 11 12 12 13 14 15 11 11 11 11 The length of the longest Harmonic subsequence is: 7 For the input array: 0 1 5 8 10 19 15 25 30 40 45 7 11 The length of the longest Harmonic subsequence is: 2 Complexity Analysis: In the program, each element of the input array is getting pivoted. Also, a loop is iterating through the array that takes O(len) time. Thus, the overall time complexity of the program O(len2), where 'len' is the size of the input array. The space complexity of the program is constant, i.e., O(1). Another Approach: Using HashMap The approach is to keep the number of times an element occurs in a HashMap. In every iteration, check two things:
The steps are as follows: Step 1: Take a variable ans. Initialize it with the value 0. The variable "ans" will contain the final answer. Step 2: Define a HashMap 'freqHashMap' for keeping the number of times each element is occurring in the input array inputArr[]. Step 3: Iterate from 'j' = 0 to 'len - 1':
Step 4: return 'ans'. The following program implements the above-mentioned steps. FileName: LongestHarmonioussubsequence2.java Output: For the input array: 11 12 12 13 14 15 11 11 11 11 The length of the longest Harmonic subsequence is: 7 For the input array: 0 1 5 8 10 19 15 25 30 40 45 7 11 The length of the longest Harmonic subsequence is: 2 Complexity Analysis: In program, a loop is used to iterate over the input array and incrementing the count of each element in the HashMap that has the amortized time complexity of O(1). Therefore, the overall time complexity of the program is O(N). Also, the HashMap can grow maximum up to the size of the input array. Therefore, the overall space complexity of the program also O(N). Here, N is the total number of elements in the input array. Next TopicCount OR Pairs in Java |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India