0
How to solve the approch using c++
Given an array of integers what is the length of the longest subArray containing no more than two distinct values such that the distinct values differ by no more than 1 For Example: arr = [0, 1, 2, 1, 2, 3] -> length = 4; [1,2,1,2] arr = [1, 2, 3, 4, 5] -> length = 2; [1,2] arr = [1, 1, 1, 3, 3, 2, 2] -> length = 4; [3,3,2,2]
1 Odpowiedź
0
I'm quite sure there's a shorter implementation, but you can basically iterate over your given array and take each item into a new array, if it's not yet contained in this array. Pseudo code:
for item in give array
if item not in result array
resultArray.add(item)