Picking Numbers HackerRank Exercise
Given an array of integers, find and print the maximum number of integers you can select from the array such that the absolute difference between any two of the chosen integers is less than or equal to 1. For example, if your array is [1, 1, 2, 2, 4, 4, 5, 5, 5], you can create two subarrays meeting the criterion: [1, 1, 2, 2] and [4, 4, 5, 5, 5]. The maximum length of the subarray has 5 elements. Programm should return an integer that represents the length of the longest array that can be created.
- a: an array of integers
- 2 <= n <= 100
- 0 < a[i] < 100
- The answer will be >= 2
Make your attention to the constraints of this exercise. Elements can be in the range from 0 to 100. So the maximum we can have 100 numbers. I decide to do the next steps:
- Generate array repeatingList with length 100. And set all of the items to 0. This array will contain the number of all repeating of each item.
- For through the input array, and increment the element on an index which equal to iterable item — 1.
- For through the repeatingList and sum each element with the next element in the sequence. The biggest met sum is our answer