Variable Sized Arrays

Sort by

recency

|

1457 Discussions

|

  • + 0 comments
    int main() {
        int n, q;
        int k_length, k_member;
        vector<vector<int>> a;
        vector<int> k; 
        int x,y; 
        
        cin >> n >> q;
        for(int i = 0; i < n; i++)
        {
            cin >> k_length;
            for(int j = 0; j < k_length; j++)
            {
                cin >> k_member;
                k.push_back(k_member);
            }
            a.emplace_back(k);
            k.clear();
        }
        
        for(int i = 0; i < q; i++)
        {
            cin >> x >> y;
            cout << a.at(x).at(y) << endl;   
        }
        
        return 0;
    }
    
  • + 0 comments
    int main() {
        unsigned int arrCount, queryCount;
        cin >> arrCount >> queryCount;
        
        vector<vector<unsigned int>> numArrs(arrCount);
        
        unsigned int arrSize = 0;
        unsigned int arrVal = 0;
        
        // Input all number arrays
        for (int i = 0; i < arrCount; i++) {
            cin >> arrSize;
            for (int j = 0; j < arrSize; j++) {
                cin >> arrVal; 
                numArrs[i].push_back(arrVal);
            }
        }
        unsigned int whichArr, indexArr;
        // Query the arrays
        // Query = [arr_num, arr_idx]
        for (int i = 0; i < queryCount; i++) {
            cin >> whichArr >> indexArr;
            cout << numArrs[whichArr].at(indexArr) << '\n';
        }
          
        return 0;
    }
    
    // TIME COMPLEXITY
    // Input phase: O(N)
    // Query phase: O(Q) as .at() is constant
    
    // SPACE COMPLEXITY
    // O(N) - took no location of array in memory 
    
  • + 0 comments
    int main() {
        
        int array_size, number_of_queries;
        
        cin >> array_size >> number_of_queries;
        
        vector<int> array_of_vectors[array_size];
        
        for (int i = 0; i < array_size; i++) {
            int vector_size;
            
            cin >> vector_size;
    
            array_of_vectors[i] = vector<int>(vector_size);
    
            for (int j = 0; j < vector_size; j++) {
                cin >> array_of_vectors[i][j];
            }
        }
        
        for (int i = 0; i < number_of_queries; i++) {
            int n_vector, n_element;
            
            cin >> n_vector >> n_element;
            cout << array_of_vectors[n_vector][n_element] << endl;
        }
        
        return 0;
    }
    
  • + 0 comments

    Let me explain the question for you.

    1. what we have here is an array of array sometimes called a jagged array.
    2. think of it as vector of vectors.
    3. each element in arr[i] has an array of integers.
    4. the size of each inner array can vary.

    INPUT:

    first line has two integers n, q then you have n lines describing each array: k a[ i ][ 0 ] a[ i ][ 1 ] ... a[ i ][ k-1 ] where k is the length of the array followed by k integers for array i.

    then you have q lines of queries i, j i = index of the array in the outer vector j = index of the inner array in i

    Task: for each query, print the value at arr[ i ] [ j ].

    Hence the problem known as variable sized array.

    I would recommend to understand the code but not copy paste it in your editor. once you understand the code then dry run it and then write the algorithm of it in your copy and then try yourself to solve the question here. that is the best way to do it.

    #include<bits/stdc++.h>
    using namespace std;
    
    int main(){
    	int n, q;
    	cin >> n>> q;
    	vector<vector<int>> arr(n);
    
    	for(int i=0; i<n ;i++){
    		int k;
    		cin >> k;
    		arr[i].resize(k);
    		for(int j=0; j<k; j++){
    			cin >> arr[i][j];
    		}
    	}
    	for(int a=0; a<q; a++){
    		int i, j;
    		cin >> i >> j;
    		cout << arr[i][j] << endl;
    	}
    	return 0;
    }
    

    If you like it then upvote . It is better for the coding community

  • + 0 comments
    int main() {
        int n, q;
        cin >> n >> q;  
        vector<vector<int>> arrays(n);
        
        for (int i = 0; i < n; i++) {
            int k; cin >> k; 
            vector<int> inner(k);  
            for (int j = 0; j < k; j++) {
                cin >> inner[j];  
            }
            arrays[i] = inner; 
        }    
        
        while(q--) {
            int i, j; cin >> i >> j; 
            cout << arrays[i][j] << endl; 
        }
        return 0;
    }