Search This Blog

Sunday, November 18, 2018

The maximum number of integers you can select from the array

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 subarray has 5 elements.

We need to check numbers that are adjacent or equal(i.e. absolute difference<=1). freq[] has got frequency of occurrence of each element of input array. We just need to return which two adjacent elements appear highest in array.(sum of their frequencies)