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
- Discussions
Picking Numbers
Picking Numbers
Sort by
recency
|
175 Discussions
|
Please Login in order to post a comment
The common definition of a subarray consists of a list of contiguous elements within an arary. The problem statement is inconsistent with the test cases. I coded a solution considering an unordered array of integers and failed nearly all test cases. However, after I pre-sorting the array 'a' I immediately passed the test cases.
public static int pickingNumbers(List a) { // Write your code here Collections.sort(a); int t = 0; for(int i = 0 ; i < a.size();i++){ int tt = 0; for(int j = i+1; j< a.size(); j++){ if(a.get(j) - a.get(i) <=1){ tt++; } } if(t < tt){ t = tt; } } return t+1; }
Java O(n)
Solution in Python using two pointer, I guess: