• + 0 comments

    @CS234_1716410234 thank you so much for your explanation, I wasn't able to crack this problem, but now did it, it's not an optimised code at all, it may have huge time complexity but it's working for the time being :

    static int maxLength=0; static boolean status = false; static List array = new ArrayList<>(); static List array1 = new ArrayList<>();

    public static int pickingNumbers(List<Integer> a) 
    {
        Collections.sort(a);
        int min=0;
    
            for(int i=0; i<a.size(); i++)
            {
                min = a.get(i);
                if(i==0)
                {
                    array1.add(min);                    
                }
    
                //System.out.println("New Minimum = " +min);
    
                for(int j=1; j<a.size(); j++)
                {
                    if(a.get(j) == min+1)
                    {
                        array1.add(a.get(j));
                        status=true;
                        //System.out.println("Adding array[j] > " +a.get(j));
                    }
    
                    else if(a.get(j) == min)
                    {
                        array1.add(a.get(j));
                        status=true;
                        //System.out.println("Adding array[j] = " +a.get(j));
                    }
                }
    
                if(status==true)
                {
                    array.add(array1.size());
                    array1.clear();
                }
    
                else if(status==false)
                {
                    array1.clear();
                    //System.out.println("Clearing....");
                }
            }
    
        //System.out.println(array1);
    
        if(array1.size()==1)
        {
            array1.clear();
        }
    
        for(int size: array)
        {
            if(size > maxLength)
            {
                maxLength = size;
            }
        }
    
        return maxLength;
    }