• + 0 comments

    C#: O(nlogn)
    public static int pickingNumbers(List a) { if (a.Count <= 1) return a.Count;

    a.Sort(); 
    int result = 0;
    int currentCount = 1;
    
    for (int i = 0, j = 1; j < a.Count; j++)
    {
    
        if (a[j] - a[i] <= 1)
        {
            currentCount++;
        }
        else
        {
    
            result = Math.Max(result, currentCount);
            currentCount = 1; 
            i = j; 
        }
    }
    
    result = Math.Max(result, currentCount);
    return result;
    }
    

    }