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
belongs to collection: Interview C++ coding problems/challenges | Coding Algorithms
All Answers
total answers (1)
Algorithm:
This problem can be implemented with help of map data structure. We have used STL for map implementation. (For details regarding STL map, C++ STL Map)
Algorithm is pretty simple.
We first extract the unique numbers and store their frequencies. Then we simply check for two unique number's additive frequency or any one unique number's frequency itself and return the greater one.
Let's solve the above example.
C++ implementation:
Output