Variable Sized Arrays

  • + 0 comments

    Explaination above , Solution below :-

    This question involves working with arrays of varying lengths and performing queries on them. where n is the number of variable-length arrays, and q is the number of queries Each array is given in the format k a[i]0 a[i]1 … a[i]k-1, where k is the length of the array and a[i]0 a[i]1 … a[i]k-1 are the elements of the array also,there are q lines, each containing two integers, i and j, which are indices to query. in example 2 2 3 1 5 4 5 1 2 8 9 3 0 1 1 3 there are 2 variable-length arrays and 2 queries. The first array has length 3 and contains the elements 1, 5, and 4. The second array has length 5 and contains the elements 1, 2, 8, 9, and 3

    The first query is (0, 1)which means you need to find the array located at index 0 (first array) and print the element at index 1. So, for the first query, you need to print the value 5.

    The second query is (1, 3), which means you need to find the array located at index 1 (second array) and print the element at index 3. So, for the second query, you need to print the value 9. 5 9 here's approach with vector :-

    include

    include

    using namespace std; int main() { int n, q; cin >> n >> q; vector> arrays; for (int i = 0; i < n; ++i) { int k; cin >> k; vector array(k); for (int j = 0; j < k; ++j) { cin >> array[j]; } arrays.push_back(array); } for (int i = 0; i < q; ++i) { int x, y; cin >> x >> y; cout << arrays[x][y] << endl; }

    return 0;
    

    } -- arrays.push_back(array);: Adds the array vector to the arrays vector. This adds the current array to the list of variable-length arrays. -- cout << arrays[x][y] << endl;: Prints the value of the element at index y of the array located at index x from the arrays vector