Neighbours in value
The largest group of values whose max and min differ by exactly 1. About 10 minutes.
Does position even matter?
Some problems ask you to pick any items from a list, not a stretch of neighbours. Then where things sit stops mattering at all, and a count of each value may be all you need.
A quick test: if I shuffled the list, would the answer change? If not, throw the positions away and think in counts.
- best
- 0
We can pick values from anywhere, so all we need is how many of each value there are.
You pick values whose max − min is exactly 1 from [1, 3, 2, 2, 5, 2, 3, 7]. Which values could the group use?
- AOnly 2s and 3s (or only 1s and 2s…)
- BAny values at all
- COne value repeated
Show the answer
Only 2s and 3s (or only 1s and 2s…). The group holds two neighbouring values, x and x + 1.
Neighbours in value
Choose any values from the list (not necessarily next to each other) so that the largest chosen minus the smallest chosen is exactly 1. Return the most values you can choose, or 0 if it's impossible.
nums = [1, 3, 2, 2, 5, 2, 3, 7] → 5
0 ≤ n ≤ 1,000,000