We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
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.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Picking Numbers
You are viewing a single comment's thread. Return to all 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.