1
Have this problem to solve in Hacker Rank and my code for the solution of this problem was as follows:
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int **arr, arrqtd, queries;
cin >> arrqtd >> queries;
arr = (int **) malloc(sizeof(int)*arrqtd);
for (int i = 0; i < arrqtd;i++){
int size;
cin >> size;
arr[i] = (int *) malloc(sizeof(int)*size);
for(int j = 0; j < size;j++){
int value;
cin >> value;
arr[i][j] = value;
}
}
for (int i = 0; i < queries;i++){
int row,col;
cin >> row >> col;
cout << arr[row][col] << endl;
}
return 0;
}
The problem is that in an input example like this:
10 10
3 916135 272188 794963
3 178997 502468 671251
1 122266
3 223511 996043 990104
3 319694 335208 200789
2 867809 273793
1 925358
1 71140
1 862238
1 994309
6 0
5 0
5 0
7 0
5 0
6 0
3 2
3 1
0 0
9 0
The initial value of my matrix changes from the sixth iteration, I would like to know what I did wrong but so far I haven’t found anything (and I would also like to know why this initial value changes from "nothing").
On my machine, it worked. Slackware Current, GCC 7.2.0 and Clang 5.0.0.
– lemoce
Whereas you even have the
#include <vector>
at the top would be much easier usingvectors
, unless it is a constraint of the problem. Still from what I ran and saw in the code it seemed correct. The platform gives you some error in the submission ?– Isac
If it is C++, why not use
new int[]
?– Jefferson Quesado
@lemoce, when running on my machine appears this print that I put in the question.
– Matheus
@Isac I do not use the vector for two reasons, I am new in c++ and do not understand and why I wanted to understand the pq of not working the way I wrote and I did not see any error in logic. And on submission, when no error gives exceeded time limit.
– Matheus
@Jeffersonquesado because I do not understand how the same works.
– Matheus