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
  • Apply
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Implementation
  4. Picking Numbers
  5. Discussions

Picking Numbers

Problem
Submissions
Leaderboard
Discussions
Editorial

    You are viewing a single comment's thread. Return to all comments →

  • muhamadalinurza1
    4 months 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
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy