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.
Picking Numbers
Picking Numbers
Sort by
recency
|
2580 Discussions
|
Please Login in order to post a comment
Here is problem solution in Python, Java, C++, C and Javascript - https://programmingoneonone.com/hackerrank-picking-numbers-problem-solution.html
There is a problem with the first sentence of the problem description: a subarray is a contiguous slice of the parent array. However the actual test cases allow using any subset of the input.
def pickingNumbers(a): se=list(set(a)) maxi=-99 for i in se: ma=0 ma=a.count(i)+max(a.count(i-1),a.count(i+1)) maxi=max(ma,maxi) return maxi play lucky 88
One explicite loop only.
def pickingNumbers(a): a.sort()
The question definetely implies it wants you to solve for a contiguous subarray, but the test cases say otherwise. Here's my accepted submission which checks for the case where numbers are all equal/one-above or equal/one-below.
def pickingNumbers(a): numbers = set(a) max_counter = 0 for n in numbers: counter_plus = 0 counter_minus = 0 for i in a: if i-n == 1 or i-n ==0: counter_plus += 1 if n-i == 1 or n-i ==0: counter_minus += 1 max_counter = max(counter_plus, counter_minus, max_counter) return max_counter acft calculator