New update is available. Click here to update.

Last Updated: 14 Mar, 2021

Difficulty: Hard

```
[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.
```

```
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.
```

```
For each test case, return the size of the longest harmonious subsequence.
```

```
You are not required to print the expected output; it has already been taken care of. Just implement the function.
```

```
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 subsequences.

The steps are as follows:

- Initialize 'ANSWER' to 0, which denotes the longest length of the Harmonic subsequence.
- Iterate from ‘i’ = 0 to ‘2^N - 1’, where ‘N’ is the length of the given Array, ‘ARR’.
- Initialize three integers ‘MIN_VALUE’, ‘MAX_VALUE’, and ‘COUNT_LENGTH’ with INT_MAX, INT_MIN, and 0 respectively which stores the minimum, maximum values of the current subsequence and length of the current subsequence respectively.
- Iterate from ‘j’ = 0 to ‘N-1’:
- If a jth bit of i is set and ‘MIN_VALUE’ is less than ARR[j], then update ‘MIN_VALUE’ to ARR[j].
- If a jth bit of i is set and ‘MAX_VALUE’ is greater than ARR[j], then update ‘MAX_VALUE’ to ARR[j].
- Increment the count as we are including the current element in the subsequence.

- If the difference between the ‘MAX_VALUE’ and ‘MIN_VALUE’ is 1. It means it is a valid subsequence. Update 'ANSWER' to a maximum of 'ANSWER' and ‘COUNT_LENGTH’. Also, take care of the integer overflow.

- Return 'ANSWER’ as the final answer.

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 steps are as follows:

**Initialize answer to 0, which denotes the longest length of the Harmonic subsequence.****Iterate from ‘i’ = 0 to ‘N - 1’:****Initialize ‘COUNT_LENGTH’ with 0 respectively which stores the length of the current subsequence.****Initialize a boolean flag to false, which will be true if it is possible to make the harmonic subsequence. Otherwise, it is false.****Iterate from ‘j’ = 0 to ‘N - 1’:****If the element at the jth index is the same as the element at the ith index, then increment the count.****If the element at the jth index is 1 greater than the element at the ith index, then increment the count and set the flag to true as the maximum difference of the current subsequence is 1.**

**If the flag is true, Update the answer to the maximum of ‘ANSWER’ and ‘CURRENT_LENGTH’.**

**Return ‘ANSWER’ as the final answer.**

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 length of the subsequence will be the number of occurrences of ARR[i] + number of occurrences of (ARR[i] + 1).
- If ARR[i] - 1 is present in the HashMap or not. If present, then our current length of the subsequence will be a number of occurrences of ARR[i] + number of occurrences of (ARR[i] - 1).

The steps are as follows:

- Initialize 'ANSWER' to 0, which denotes the longest length of the Harmonic subsequence.
- Define a HashMap ‘FREQUENCY’ which stores the number of occurrences of each element in the ARR.
- Iterate from ‘i’ = 0 to ‘N - 1’:
- Increment the count of ARR[i] in the HashMap.
- If ARR[i] + 1 is present in the HashMap, ie. FREQUENCY[ARR[i] + 1] >0, then update the 'ANSWER' to the maximum of 'ANSWER' and (FREQUENCY[ARR[i]] + FREQUENCY[ARR[i] + 1]).
- If ARR[i] - 1 is present in the HashMap, ie. FREQUENCY[ARR[i] - 1] >0, then update the 'ANSWER' to maximum of 'ANSWER' and (FREQUENCY[ARR[i]] + FREQUENCY[ARR[i] - 1]).

- Return
**‘ANSWER’**.

SIMILAR PROBLEMS

Ninja And The Strictly Increasing Array

Posted: 27 Nov, 2022

Difficulty: Moderate

Maximum GCD

Posted: 8 Dec, 2022

Difficulty: Hard

Maximize

Posted: 9 Dec, 2022

Difficulty: Easy

Negative To The End

Posted: 16 Dec, 2022

Difficulty: Easy

Find Duplicate in Array

Posted: 5 Jun, 2023

Difficulty: Easy

Popular Interview Problems: