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.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Implementation
  4. Picking Numbers
  5. Discussions

Picking Numbers

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 2245 Discussions, By:

recency

Please Login in order to post a comment

  • alban_tyrex
    4 hours ago+ 0 comments

    Here is my O(n) c++ solution, you can find the explanation here : https://youtu.be/0zvqEO1gDRw

    int pickingNumbers(vector<int> a) {
        map<int, int> mp;
        for(int e : a) mp[e]++;
        int ans = mp[0];
        for(int i = 1; i < 99; i++){
            int curr = max(mp[i] + mp[i+1], mp[i] + mp[i-1]);
            ans = max(ans, curr);
        }
        return ans;
    }
    
    0|
    Permalink
  • den_mytsak
    4 days ago+ 0 comments

    Python:

    def pickingNumbers(a):
        return max(
            (max(a.count(e + 1), a.count(e - 1)) + a.count(e)) for e in set(a)
        )
    
    0|
    Permalink
  • josephlmyers
    5 days ago+ 0 comments

    I had a lot of confusion and had to read the discussions for it to click that the entire subset had to be within 0-1 integers from one another. I feel like the problem is worded poorly, or ambiguously at best. I was under the assumption initially that the next integer in the set had to be the same, one more, or one less than the previous.

    Anyway, my working Java Code:

     public static int pickingNumbers(List<Integer> a) {
        // Write your code here
        int longestSubArrayLength = 0;
        Collections.sort(a);
        
        for(int i = 0; i < a.size() - 1; i++){
          int comparator = a.get(i);
          List<Integer> subArray = new ArrayList<Integer>();
          subArray.add(comparator);
          for(int j = i + 1; j < a.size(); j++){
            int range = Math.abs(comparator - a.get(j));
            if(range == 0 || range == 1) subArray.add(a.get(j));  
            else break;
    
          }
          if(subArray.size() > longestSubArrayLength)
           longestSubArrayLength = subArray.size();
           
        }
        return longestSubArrayLength;
    
      }
    
    0|
    Permalink
  • trinhhai_dmc
    5 days ago+ 0 comments

    Python:

    def pickingNumbers(a):
        s=[]
        c1,c2=0,0
        for i in range(len(a)):
            for j in range(len(a)):
                if a[i] in [a[j],a[j]-1]:
                    c1+=1
                if a[i] in [a[j],a[j]+1]:
                    c2+=1
            s.append(c1)
            s.append(c2)
            c1=0
            c2=0
        return max(s)
    
    0|
    Permalink
  • muhamadalinurza1
    5 days ago+ 0 comments

    SIMPLE JAVA 8

    public static int pickingNumbers(List<Integer> a) {
        // Write your code here
            Collections.sort(a);
            int max = 0;
            int total = 0;
            boolean diffNumber = false;
            for (int i = 0; i < a.size() - 1; i++) {
                if (a.get(i + 1) - a.get(i) == 0) {
                    total++;   
                } else if (a.get(i + 1) - a.get(i) == 1 && !diffNumber) {
                    total++;
                    diffNumber = true;
                } else {
                    total = 0;
                    diffNumber = false;
                }
                
                if (total > max)
                    max = total;
            }
            return max + 1;
        }
    
    0|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy