• + 20 comments

    the question says they want set of all those number which differ only by 1 or 0.

    So by this we know our answer set will include some number say x and x+1. i.e. they would be consequtive.

    Now all we need to do is find frequency of all the numbers in the given question.

    and find a consecutive pair whose frequencies when added up would be max.

    In the code - a is the array of frequency of each number present in array. (It is mentioned in the constraint that ai<100 , so size of the array is 100)

    and in the loop every 2 consequtive frequencies of number are added and compared with maximum.