Longest Harmonious Subsequence 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 arr...

read more
CodingNinjas
author
2y
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...
see more
CodingNinjas
author
2y
Brute ForceThe 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...
see more
CodingNinjas
author
2y
OptimizedThe 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...
see more
CodingNinjas
author
2y
Using HashMapThe 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...
see more
Add answer anonymously...
L&T Infotech Java Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
55 L+

Reviews

5 L+

Interviews

2 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2023 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Hello, Guest
Contribute to help millions!
Write a review
Share interview
Contribute salary
Add office photos
Add office benefits