variable sized arrays hackerrank solution in c 2b 2b

Solutions on MaxInterview for variable sized arrays hackerrank solution in c 2b 2b by the best coders in the world

showing results for - "variable sized arrays hackerrank solution in c 2b 2b"
Jessica
06 Feb 2017
1#include <iostream>
2#include <vector>
3
4using namespace std;
5
6int main() {
7	// get length of array 'a' and number of queries
8	int n, q;
9	cin >> n >> q;
10
11	// create vector of vectors
12	vector<vector<int>> a(n);
13
14	// fill each 2D vector i with k_i values
15	for (int i = 0; i < n; i++) {
16		// get the length k of the vector at a[i]
17		int k;
18		cin >> k;
19
20		// fill the vector with k values
21		a[i].resize(k);
22		for (int j = 0; j < k; j++) {
23			cin >> a[i][j];
24		}
25	}
26
27	// run queries on a
28	for (int q_num = 0; q_num < q; q_num++) {
29		// get i, j as the 'query' to get a value from a
30		int i, j;
31		cin >> i >> j;
32		cout << a[i][j] << endl;
33	}
34
35	return 0;
36}
Kimberley
03 Jun 2020
1int n,q;
2cin >> n >> q;
3vector< vector<int> > a(n);
4
5// input each array
6for (int i=0;i<n;i++) {
7    int k;
8    cin >> k;
9
10    for (int j=0;j<k;j++) {
11        int data;
12        cin >> data;
13        a[i].push_back(data);
14    }
15}
16
17// do the queries
18for (int i=0;i<q;i++) {
19    int x,y;
20    cin >> x >> y;
21    cout << a[x][y] << endl;
22}
23