solution : CodeChef: Practical coding for everyone
works , and has fixed memory allocation, but the same logic in solution : CodeChef: Practical coding for everyone doesnt work and its dynamically allocated.
can someone please explain me why?
solution : CodeChef: Practical coding for everyone
works , and has fixed memory allocation, but the same logic in solution : CodeChef: Practical coding for everyone doesnt work and its dynamically allocated.
can someone please explain me why?
For one thing, you’re using the wrong kind of delete - if you’re delete’ing a dynamically-allocated array, you use:
delete[]
instead of:
delete
You’re also not initialising your dynamic arrays:
but perhaps this doesn’t matter - it’s hard to tell.
You’re also leaking memory everywhere ![]()
I’d just use vectors.
Edit:
You also have out-of-bounds accesses on these lines:
dp[i][j] = arr[i][j] +dp[i-1][j-1];
dp[i][j]= arr[i][j] + max(dp[i-1][j], dp[i-1][j-1]);
on the sample testcase (exposed by using vectors and the gcc debug mode).
Oh: also shows up in your AC solution:
[simon@simon-laptop][09:23:26]
[~/devel/hackerrank/otherpeoples]>./compile-latest-cpp.sh
Compiling xym991-SUMTRIAN-ac.cpp
Executing command:
g++ -std=c++17 xym991-SUMTRIAN-ac.cpp -O3 -g3 -Wall -Wextra -Wconversion -DONLINE_JUDGE -D_GLIBCXX_DEBUG -fsanitize=undefined -ftrapv
Successful
[simon@simon-laptop][09:23:28]
[~/devel/hackerrank/otherpeoples]>echo "2
3
1
2 1
1 2 3
4
1
1 2
4 1 2
2 3 1 1
" | ./a.out
xym991-SUMTRIAN-ac.cpp:36:63: runtime error: index -1 out of bounds for type 'int [100]'
5
9
ahhh… i see . thank you so much dude.