This is the second to last challenge in the Introduction section of C++ at the HackerRank site: https://www.hackerrank.com/challenges/variable-sized-arrays
If interested please take a look at the challenge definition.
Following is a screen capture of the Windows console using the sample data provided in the challenge:
2 2
3 1 5 4
5 1 2 8 9 3
0 1
5 <== first answer
1 3
9 <== second answer
My solution written in C++ using Visual Studio 2013 Professional IDE which passed the 11 test cases follows:
#include <iostream>
#include <string>
using namespace std;
/*
*/
int main () {
int N,
Q;
int *arr[100000];
int i,
j,
k;
// **** ****
cin >> N;
cin >> Q;
// cout << “N: ” << N << ” Q: ” << Q << endl;
// **** load the array ****
for (int n = 0; n < N; n++) {
cin >> k;
// cout << “k: ” << k << endl;
arr[n] = (int *)calloc(k, sizeof(int));
for (int i = 0; i < k; i++) {
cin >> arr[n][i];
}
}
// **** perform queries ****
for (int q = 0; q < Q; q++) {
cin >> i;
cin >> j;
// cout << “i: ” << i << ” j: ” << j << endl;
cout << arr[i][j] << endl;
}
// **** ****
return 0;
}
If you have comments or questions regarding this or any other post in this blog or if you have a topic which you would like me to address, please do not hesitate and send me a message via email. I will not disclose your name unless you explicitly all me to do so.
John
john.canessa@gmail.com
Follow me on Twitter: @john_canessa