• + 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;
        }