Showing posts with label pointers. Show all posts
Showing posts with label pointers. Show all posts

Tuesday, December 29, 2020

Variable Sized Arrays | Hackerrank | Solution | C++

 

                                                    


Question : https://www.hackerrank.com/challenges/variable-sized-arrays/problem

Solution : #include <cmath>

#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 
    int size , q;
    cin>>size >>q;
      
    int ** A = new int*[size];
    for(int i =0;i<size;i++){
        int s;
        cin>>s;
        A[i] = new int[s];
        for(int j = 0;j<s;j++){
            cin>>A[i][j];
        }
    }
    
    for(int i =0;i<q;i++){
        int r , c;
        cin>>r>>c;
        cout<<A[r][c]<<endl;
    }
    
/* Free your memory using this code
for(int i = 0; i < size; ++i)
{
    delete[] A[i];
}
delete[] A;

*/
    return 0;
}

Monday, December 28, 2020

Pointers : Hackerrank | Easy | Solution | C++


Problem : https://www.hackerrank.com/challenges/c-tutorial-pointer/problem

Solution : 

 #include <stdio.h>

#include<math.h>

void update(int *pa,int *pb) {

    // Complete this function 

    int temp = *pa;

    *pa = *pa + *pb;

    *pb = abs(temp - *pb);

}


int main() {

    int a, b;

    int *pa = &a, *pb = &b;

    

    scanf("%d %d", &a, &b);

    update(pa, pb);

    printf("%d\n%d", a, b);


    return 0;

}