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