Variable Sized Arrays

  • + 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