Showing posts with label cpp. Show all posts
Showing posts with label cpp. Show all posts

Friday, January 1, 2021

Maps-STL | HackerRank | C++ | Solution | Color The Code



Problem : https://www.hackerrank.com/challenges/cpp-maps/problem

Solution : 

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


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    int Q;
    cin>>Q;
    map<string,int>mp;
    while(Q--){
        int type;
        cin>>type;
        if(type == 1){
            string name;
            int marks;
            cin>>name>>marks;
            if(mp.find(name)!=mp.end()){
                mp[name] = mp[name] + marks;
            }else{
                mp[name] = marks;
            }
            
        }else if(type == 2){
            
            string name;
            cin>>name;
            mp[name] = 0;
        }else if(type == 3){
            string name;
            cin>>name;
            cout<<mp[name]<<endl;
        }
    }
    return 0;
  
}




Tuesday, December 29, 2020

StringStream | HackerRank | Solution | C++ | Easy



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

Solution :  

#include <sstream>

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

vector<int> parseInts(string str) {
    // Complete this function
    vector<int>v;
    
    stringstream s(str);
    int i;
    char c;
    
    while(s >> i){
        v.push_back(i);
        s>>c;
    }
    
    return v;
}

int main() {
    string str;
    cin >> str;
    vector<int> integers = parseInts(str);
    for(int i = 0; i < integers.size(); i++) {
        cout << integers[i] << "\n";
    }
    
    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;

}


Wednesday, November 4, 2020

Queue Using Stack | Code | C++

#include<iostream>

#include<stack>

using namespace std;

stack<int>s1;

stack<int>s2;



void push(int element){

s1.push(element);

}


int pop(){

    if(!s2.empty()){

        int r = s2.top();

        s2.pop();

        return r;

    }else{

        while(!s1.empty()){

            int t = s1.top();

            s1.pop();

            s2.push(t);

        }


        int r = s2.top();

        s2.pop();

        return r;

    }

}


int main(){


// s1 and s2

 //push : 1 2 3 4

 //pop :  ->  1

 //push : 5    -> 2 3 4 5

 //pop :   -> 2


 push(1);

 push(2);

 push(3);

 push(4);

 cout<<pop();

 push(5);

 cout<<'\n'<<pop();



return 0;}


NOTE : Write code for queue properties and even do check for empty before popping the element from the queue ...