• + 0 comments

    This was pretty fun to solve in C, here is my solution:

    int* cutTheSticks(int arr_count, int* arr, int* result_count) {
        int* result = malloc(sizeof (int));
        *result_count = 1;
        
        int* aux = malloc(arr_count * sizeof(int));
        int aux_count = arr_count, smallest_val;
        for (int i = 0; i < arr_count; i++) {
            aux[i] = arr[i];
        }
        
        do {
            result[*result_count-1] = aux_count;
            smallest_val = aux[0];
            for (int i = 0; i < aux_count; i++) {
                if (smallest_val > aux[i]) smallest_val = aux[i];
            }
            
            int j = 0;
            for (int i = 0; i < aux_count; i++) {
                if (aux[i] != smallest_val) {
                    aux[j] = aux[i] - smallest_val;
                    j++;
                }
            }
            
            aux_count = j;
            aux = realloc(aux, aux_count * sizeof(int));
            if (aux_count > 0) {
                (*result_count)++;
                result = realloc(result, *result_count * sizeof(int));
            }
        } while (aux_count > 0);
        
        return result;
    }