Variable Sized Arrays

Sort by

recency

|

1452 Discussions

|

  • + 0 comments

    The following include two solutions

    1. Using Vectors
    int main() {
        int rows, q;
        cin >> rows >> q;
        
        vector<vector<int>> matrix(rows);
        for(int i=0; i<rows; i++) {
           int cols;
           cin >> cols;
           matrix[i].resize(cols);
           
           for(int j=0; j<cols; j++) cin >> matrix[i][j];
        }
        
        // for query part
        for(int i=0; i<q; i++) {
            int row, col;
            cin >> row >> col;
            cout << matrix[row][col] << endl;
        }
        return 0;
    }
    
    1. Using normal Arrays
    int main() {
        int rows, q;
        cin >> rows >> q;
        
        int ** matrix = new int*[rows];
        
        // taking inputs
        for(int i=0; i<rows; i++) {
           int cols;
           cin >> cols;
           matrix[i] = new int[cols];
           
           for(int j=0; j<cols; j++) cin >> matrix[i][j];
        }
        
        // for query part
        for(int i=0; i<q; i++) {
            int row, col;
            cin >> row >> col;
            cout << matrix[row][col] << endl;
        }
        
        // delete dynamically allocated memory
        for(int i=0; i<rows; i++) {
            delete[] matrix[i];
        }
        delete[] matrix;
        return 0;
    }
    

    The logic is same TC -> O(n * m) where n is rows and m is columns

  • + 0 comments
    #include <cmath>
    #include <cstdio>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    
    int main(){
            int a,b,x;
            cin>>a>>b;
            int* arr[a];
            int c[b][2],d[a];
            vector<int> v;
            for(int i=0;i<a;i++){
                    int k;
                    cin>>k;
                    for(int j=0;j<k;j++){
                            cin>>x;
                            v.push_back(x);
                    }
                    d[i]=k;
                    }
            for(int k=0;k<b;k++){
                    cin>>(c[k][0])>>(c[k][1]);
            }
            int* p = &v[0];
            for(int i=0;i<a;i++){
                arr[i]=p;
                p += d[i]; 
            }
            for(int i=0;i<b;i++){
                cout<<*(arr[c[i][0]] + c[i][1])<<endl;
            }
    
                                }
    
  • + 0 comments

    include

    include

    include

    include

    include

    using namespace std;

    int main() { int n , q ; cin >> n >> q ; vector> 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 i = 0 ; i < q ; i++){
        int x,y;
        cin >>x>> y ;
        cout <<arr[x][y] << endl;
    
    }
    
    
    return 0;
    

    }

  • + 0 comments

    Here is Variable sized arrays solution in c++ - https://programmingoneonone.com/hackerrank-variable-sized-arrays-solution-in-cpp.html

  • + 1 comment

    I didn't really did a proper research about how to define the size of the vectors so I ended by making this code

    int main() {
        vector<vector<int>> matrix, querys;
        int n, q, lenght, x, y;
        
        cin >> n >> q;
        
        for(int i=0; i < n; i++){
            cin >> lenght;
            matrix.push_back(vector<int> ());
            
            for (int j=0; j < lenght; j++) {
                cin >> x;
                matrix[i].push_back(x);
            }
        }
        
        for (int i=0; i < q; i++) {
            cin >> x >> y;
            querys.push_back(vector<int> ());
            querys[i].push_back(x);
            querys[i].push_back(y);
        }
        
        for (int i=0; i < q; i++) {
            cout << matrix[querys[i][0]][querys[i][1]] << endl;
        }
         
        return 0;
    }