Longest subArray with no more than two distinct values that differ by no more than 1

not able to solve this question correctly please check this

Hello @rohitkandpal683,

I would suggest you refer to the following article:

Let me know if you still face any issue.
I’ll help you with that.

thanks for help i understand the article but in the question that i mentioned above i think that there is no need to find the sum , i tried to approach this question simple sliding window concept that til the diff between starting of subarray is smaller then the rest of element by 1 that is their is only 1 distinct element i will increse my array when the difference become greater than 1 i will incrse the j becuase their is now more than 1 distinct element and calculate the length . what is the problem in this approach if their is any test case please tell me because the test case that fail have very large value so i cannot figure out what’s going wrong

Hey @rohitkandpal683.

Are you talking about the question longest subarray with sum k? Aren’t you?

no i am not talking about that question my question is : Longest subArray with no more than two distinct values that differ by no more than 1 and my solutions is https://ide.codingblocks.com/s/257437 .

Hello @rohitkandpal683,

After looking at your solution:
A possible mistake i can think of is in the condition you have used: if(arr[i]-arr[j]<=1)
For example:
arr[] = {5, 5, 3}

As per my understanding:
The answer should be 2 for either {5, 5}

But, your output is 3 for {5, 5, 3}.
As, 3-5 is -2 which is smaller than 1.

I think the actual check should be: if(arr[i]-arr[j]<=1 && arr[j] -arr[i] <=1)

Let me know if this works.

this may be correct but it is hackerank basic skill certfication question and i give this test few days back so i can’t give again this test it’s hackerank policy. if possible can you please submit this question from your hackerank account by giving this test.you may quit the test after the solution is submitted

Hello @rohitkandpal683,

Isn’t the test for python?