Sort by

recency

|

2569 Discussions

|

  • + 0 comments

    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;

  • + 0 comments

    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

  • + 0 comments

    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?

  • + 1 comment

    C++ Solution using sliding window:

    int pickingNumbers(vector<int> a) {
        sort(a.begin(),a.end());
        int slow = 0;
        int fast = 0;
        int len = 1;
        while(fast <= a.size()){
           if(abs((a[fast]-a[slow]))<=1 ){
            if((fast-slow)+1>len){
                len = (fast-slow)+1;
            }
            fast++;
           }else if(slow == fast){
             fast++;
           }else{
            slow++;
           }
        }
        return len;
    }
    
    • + 0 comments

      Why are we sorting it actually, not fair to sort if the problem hasnt said to follow the contiguous condition, right?

  • + 0 comments

    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