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
|
2569 Discussions
|
Please Login in order to post a comment
Brute force approach is too slow the best approach or the solution for this is the frequency array approach (C++ solution)
`int pickingNumbers(vector a) {
int maxLength = 0;
//brute force approach on n^2
/for (int i = 0; i < a.size(); i++) { int count = 0;
for (int j = 0; j < a.size(); j++) { if (a[j] == a[i] || a[j] == a[i] + 1) { count++; } } maxLength = max(maxLength, count); }/
//frequency array approach vector freq(101, 0); for (int i = 0; i < a.size(); i++) { freq[a[i]]++; // Increment the count for number a[i] }
for (int i = 1; i < 100; i++) { maxLength = max(maxLength, freq[i] + freq[i + 1]); } return maxLength;
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
The Picking Numbers problem typically involves finding the longest subarray where the absolute difference between any two elements is at most 1. Are you looking for an efficient way to implement this in code? 2. How can we implement code related to Cookout Barbecue Menu for some delicious options?
C++ Solution using sliding window:
Why are we sorting it actually, not fair to sort if the problem hasnt said to follow the contiguous condition, right?
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