Friday, January 1, 2021

Java Loops I | HackerRank | JAVA | Solution



Problem : https://www.hackerrank.com/challenges/java-loops-i/problem

Solution :

Java Output Formatting | HackerRank | JAVA | Solution | Color The Code



Problem : https://www.hackerrank.com/challenges/java-output-formatting/problem

Solution :

Java Stdin and Stdout II | HackerRank | Java | Solution | code the color



Problem : https://www.hackerrank.com/challenges/java-stdin-stdout/problem

Solution :

Overload Operators | HackerRank | C++ | Solution



Problem : https://www.hackerrank.com/challenges/overload-operators/problem

Solution : 

Preprocessor Solution | HackerRank | C++ | Solution | Color The Code



Problem : https://www.hackerrank.com/challenges/preprocessor-solution/problem

Solution : 

C++ Class Templates | HackerRank | C++ | Solution | Color The Code



Problem : https://www.hackerrank.com/challenges/c-class-templates/problem

Solution : 

Multi Level Inheritance | HackerRank | C++ | Solution | Color The Code



Problem : https://www.hackerrank.com/challenges/multi-level-inheritance-cpp/problem

Solution :


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

class Triangle{
    public:
        void triangle(){
            cout<<"I am a triangle\n";
        }
};

class Isosceles : public Triangle{
      public:
          void isosceles(){
            cout<<"I am an isosceles triangle\n";
          }
};

//Write your code here.
class Equilateral : public Isosceles{
public:
void equilateral(){
    cout<<"I am an equilateral triangle\n";
}
}; 

int main(){
  
    Equilateral eqr;
    eqr.equilateral();
    eqr.isosceles();
    eqr.triangle();
    return 0;
}

Rectangle Area | HackerRank | C++ | Solution | Color The Code



Problem : https://www.hackerrank.com/challenges/rectangle-area/problem

Solution : 

class Rectangle{

    public:

    int width;

    int height;

    void display(){

        cout<<width<<" "<<height<<endl;

    }

};

class RectangleArea : public Rectangle{

    public:

    void read_input(){

        cin>>width>>height;

    }

    void display(){

        cout<<(width*height);

    }

};

Inheritance Introduction | HackerRank | C++ | Inheritance | Solution | Color The Code



Problem : https://www.hackerrank.com/challenges/inheritance-introduction/problem

Solution : 

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


class Triangle{
    public:
        void triangle(){
            cout<<"I am a triangle\n";
        }
};

class Isosceles : public Triangle{
    public:
        void isosceles(){
            cout<<"I am an isosceles triangle\n";
        }
        //Write your code here.
        void description(){
            cout<<"In an isosceles triangle two sides are equal\n";
        }
};

int main(){
    Isosceles isc;
    isc.isosceles();
    isc.description();
    isc.triangle();
    return 0;
}

Print Pretty | HackerRank | C++ | STL | Solution | Color The Code



Problem : https://www.hackerrank.com/challenges/prettyprint/problem

Solution :

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

int main() {
    int T; cin >> T;
    cout << setiosflags(ios::uppercase);
    cout << setw(0xf) << internal;
    while(T--) {
        double A; cin >> A;
        double B; cin >> B;
        double C; cin >> C;

        /* Enter your code here */
        long long int D = (long)A;
        cout<<hex<<showbase<<left<<nouppercase<<D<<endl;
        cout<<fixed<<dec<<setw(15)<<setprecision(2)<<showpos<<right<<setfill('_');
        cout<<B<<endl;
        cout<<setprecision(9)<<dec<<left<<noshowpos<<uppercase<<scientific;
        cout<<C<<endl;
    }

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




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



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

Solution : 

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


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */  
    int q;
    cin>>q;
    set<int>s;
    while(q--) {
        int y , x;
        cin>>y>>x;
        if( y == 1){
            s.insert(x);
        }else if ( y == 2){
            s.erase(x);
        }else if ( y == 3){
            set<int>::iterator itr = s.find(x);
            if(itr == s.end()){
                cout<<"No\n";
            }else{
                cout<<"Yes\n";
            }
        }
    }
    return 0;
}

Lower Bound-STL | HackerRank | C++ | Solution | Color The Code



Problem : https://www.hackerrank.com/challenges/cpp-lower-bound/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 n ;
     cin>>n;
     vector<int>v;
     for(int i =0;i<n;i++){
         int temp;
         cin>>temp;
         v.push_back(temp);
     }
     
     int q;
     cin>>q;
     int num;
     while(q--){
         cin>>num;
         int r = lower_bound(v.begin(),v.end(),num) - v.begin();
         if(v[r] == num)cout<<"Yes "<<r+1<<endl;
         else cout<<"No "<<r+1<<endl;
     }
    return 0;
}