You are given an array ‘ARR’ of 'N' integers. Your task is to find the longest harmonious subsequence of this array.
A sequence is Harmonious if the difference between the maximum and the minimum of all the elements is exactly 1.
For example
[3, 4, 3, 3, 3, 3, 4] is a harmonic array as the maximum of all the elements is 4 and minimum of all the elements is 3. So, the difference between the maximum and the minimum = 4 - 3 = 1.
Input Format:
The first line of input contains an integer ‘T’, denoting the number of test cases. The test cases follow.
The first line of each test case contains an integer N, which denotes the number of integers in the array ‘ARR’.
The second line of each test case contains 'N' integers of the array ‘ARR’, separated by space.
Output Format:
For each test case, return the size of the longest harmonious subsequence.
Note:
You are not required to print the expected output; it has already been taken care of. Just implement the function.
Constraints:
1<= T <= 100
1 <= N <= 10^4
1 <= ARR[i] <= 10^8
Time Limit: 1 sec
The idea is to generate all the possible subsequences using the bit masking technique. For each subsequence, check if it is a Harmonious subsequence or not. Take the maximum length of all the possible...read more
The idea is to generate all the possible subsequences using the bit masking technique. For each subsequence, check if it is a Harmonious subsequence or not. Take the maximum length of all t...read more
The idea is to pivot each element of the array and then take only those elements which are equal or have a difference = 1 so that the maximum difference in the subsequence becomes 1.
The step...read more
The idea is to store the occurrences of each element in a HashMap. In each iteration, we will check two things:
- If ARR[i] + 1 is present in the HashMap or not. If present, then our current...read more
Top LTIMindtree Java Developer interview questions & answers
Popular interview questions of Java Developer
Top HR questions asked in LTIMindtree Java Developer
Reviews
Interviews
Salaries
Users/Month