• + 1 comment

    create an array of arraylist and initialize them

        ArrayList<Integer>[] group = new ArrayList[N];
        for (int i = 0; i < N; i++) {
             group[i] = new ArrayList<Integer>();
       }
    
       then the rest is quite easy