Loading collection data...
Collections are a way for you to organize kata so that you can create your own training routines. Every collection you create is public and automatically sharable with other warriors. After you have added a few kata to a collection you and others can train on the kata contained within the collection.
Get started now by creating a new collection.
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 tounsigned 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 :
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 :
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:
or for array-like pointers:
This will still work if you change the type of
pointer
to something else later on (sometype
becomingsome_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 ;-) .
In
array_minmax_create
you are dereferencing an array that you just declared without allocating memory for it. This is a dangling pointer problem ;-)I got it!
Your string chr_try is to small.
It is of size 5 but we need 5 chars + \0 as terminator.
Change
to
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.
.
Thanks and good job for passing!
"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.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 ...
Inputs can go up to
RAND_MAX
, which, for Codewars, is2^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.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.
Formatting the code properly would make it easier for us to read, copy into IDE, and run.