top of page

Keep Reading !

You can also checkout more topics like...

peep-101_edited.png
EXPLORE COURSES (6).png
EXPLORE COURSES (9).png
EXPLORE COURSES (7)_edited.jpg

Hacker Rank C++ Solutions - Introduction


Functions


Solution:


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


int max_of_four(int a, int b, int c, int d)
{
   int arr[4];
   arr[0]=a;
   arr[1]=b;
   
   arr[2]=c;
   
   arr[3]=d;
   
    int max=arr[0];
    for(int i=0;i<4;i++)
    {
    if(arr[i]>max)
    {
        max=arr[i];
    }
    }
return max;

}


int main() {
    int a, b, c, d;
    scanf("%d %d %d %d", &a, &b, &c, &d);
    int ans = max_of_four(a, b, c, d);
    printf("%d", ans);
    
    return 0;
}

Pointer


Solution:



#include <stdio.h>

void update(int *a,int *b) {
   // if(*a>*b)
   //  (*b = *a - *b);
   //  else
   //  (*b = *b - *a);
   //  *a = *a + *q;
   *a    = *a +*b;
   if(*b<=0)
   {*b= *a + *b + *b;
   if(*b<0)
       {
           *b= -*b;
       }
   }
   else
   {
       *b= *a - *b - *b;
       if(*b<0)
       {
           *b= -*b;
       }
   } 
}

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;
}





Arrays Introduction


Solution:



#include <iostream>
using namespace std;


int main() {
  int n,i,j,temp;
  cin>>n;
  int arr[n];
for (i=0;i<n;i++)
{
    cin>>arr[i];
}

int left=0;
int right=n-1;

while(left<right)
{
    swap(arr[left],arr[right]);
    left++;
    right--;
}


 for (i=0;i<n;i++)
{
    
    cout<<arr[i]<<" ";

}
    
    
      
    return 0;
}




Variable Sized Arrays


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 n;
    int q;
    cin >> n >> q;
    vector<int> a[n];
    for(int i = 0; i < n; i++){
        int m;
        cin >> m;
        int o;
        for(int j = 0; j < m; j++){
            cin >> o;
            a[i].push_back(o);
        }
    }
    
    int r, s;
    for(int k = 1; k <= q; k++){
        cin >> r >> s;
        cout << a[r][s] << endl;
    }
    return 0;
}




102 views0 comments

Recent Posts

See All
bottom of page