Ad
  • Default User Avatar

    Keep in mind that factorials grow rather rapidly, and you need to handle large inputs.

  • Default User Avatar

    Note that your code still is not correct.
    You have a pointer to a structure array_minimax_t.
    This structure has two members : a pointer to int, and a pointer to unsigned int.
    On 64-bit architectures, pointers are 64 bits = 8 bytes wide. Thus the structure array_minimax_t has a size of 8 + 8 = 16 bytes.
    But you are allocating it this way :

      array_minmax_t* array = malloc(sizeof(array_minmax_t*));
    

    What this is doing is allocating the size of a pointer to a structure array_minimax_t , not the size of the structure itself.
    Since that pointer has a size of 8 bytes too, you are not allocating enough memory - you should be allocating this way :

    array_minimax_t *array = malloc(sizeof(array_minimax_t));
    

    Notice the lack of * after the type name: we are allocating the size of the structure, not the size of a pointer to that structure (on most architectures, pointers effectively always have the same size, independently of the objects they point to).

    A common idiom to avoid that kind of errors is to use this syntax:

    sometype *pointer = malloc(sizeof (*pointer));
    

    or for array-like pointers:

    sometype *array = malloc(array_size * sizeof(*array));
    

    This will still work if you change the type of pointer to something else later on (sometype becoming some_other_type), reducing the number of things that you have to review when refactoring your code.

    As to why your code works at all, it is not very important to know. This is undefined behavior which is the nightmare of C programmers. You are not lucky that it worked : you are unlucky not to have had a segmentation fault which would have made you find the bug ;-) .

  • Default User Avatar

    In array_minmax_create you are dereferencing an array that you just declared without allocating memory for it. This is a dangling pointer problem ;-)

  • Custom User Avatar

    I got it!
    Your string chr_try is to small.
    It is of size 5 but we need 5 chars + \0 as terminator.

    Change

    char chr_try[5];
    

    to

    char chr_try[6];
    
  • Default User Avatar

    You code times out because it is too slow.
    2nd question: I don't think it makes it slower but in some cases can cause undefined behavior.

  • Default User Avatar

    .

  • Default User Avatar

    Thanks and good job for passing!

  • Custom User Avatar

    "ails" can't end with "fails" since the ending is longer than the string you are checking against. So the kata is expecting the right thing.

  • Custom User Avatar

    Yes your loops take too long, after solving checked other people performance (in JS, wonder how C could improve) it was 8,5 - 11 s, so it is quite tight.
    This should be a hint to you, that you have to rethink your solution and be VERY conservative with rereading array values to the point of reading only ...

  • Custom User Avatar

    Inputs can go up to RAND_MAX, which, for Codewars, is 2^31. However, there's nothing special for this problem in handling big numbers, sane approach would handle all range of inputs in the same way. If you get problems with some specific values, you probably have some bug. If you managed to reproduce the issue locally, just debug through it step by step and try to find the mistake in your code.

  • Custom User Avatar

    It's difficult to say exactly witohut seeing the code, but your loops seem to roll too much. To find factors, do a prime factorization, etc. you never have to loop up to the number, required limit is much lower.

  • Custom User Avatar

    Formatting the code properly would make it easier for us to read, copy into IDE, and run.