HACKERRANK C++ SOLUTIONs.


Home C++ Java Python Contact

Loading...
Hackerank C++ solution.
Say "Hello, World!" With C++
    using namespace std;
    int main() {
        cout<<"Hello, World!";
        return 0;
    }
Input and Output
    using namespace std;
    int main() {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 
        int a,b,c,sum=0;
        cin>> a>>b>>c;
        sum=a+b+c;
        cout<< sum;
        return 0;
    }
Basic Data Types
    using namespace std;
    int main() {
        int a;
        long b;
        char c;
        float d;
        double e;
        // Complete the code.
        cin>>a>>b>>c>>d>>e;
        cout<< a<< endl;  
        cout<< b<< endl;  
        cout<< c<< endl; 
        printf("%.2f\n",d);  
        std::cout<< std::setprecision(9)<< e;
        return 0;
    } 
Conditional Statements
    using namespace std;
    int main()
    {
    int n;
    cin >> n;
    cin.ignore(numeric_limits< streamsize>::max(), '\n');
    if(n==1) cout<<"one"; 
    else if(n==2) cout<<"two"; 
    else if(n==3) cout<<"three"; 
    else if(n==4) cout<<"four"; 
    else if(n==5) cout<<"five"; 
    else if(n==6) cout<<"six"; 
    else if(n==7) cout<<"seven"; 
    else if(n==8) cout<<"eight"; 
    else if(n==9) cout<<"nine"; 
    else cout<<"Greater than 9"; 
    // Write Your Code Here 
    return 0; 
}
For Loop
    using namespace std; 
    int main() 
    { 
        int a,b,n; 
        cin >> a>>b; 
        if(a<=b) 
        { 
        int s=a; 
        for(int i=0;i< 10;i++) 
            { 
            if(a==1) cout<<"one"<< endl; 
            else if(a==2) cout<<"two"<< endl; 
            else if(a==3) cout<<"three"<< endl; 
            else if(a==4) cout<<"four"<< endl; 
            else if(a==5) cout<<"five"<< endl; 
            else if(a==6) cout<<"six"<< endl; 
            else if(a==7) cout<<"seven"<< endl; 
            else if(a==8) cout<<"eight"<< endl; 
            else if(a==9 ) cout<<"nine"<< endl; 
            a=a+1; 
            if (a==10 || a==b+1) break; 
            } 
            for(int i=0;i< b-s;i++) 
            { 
                if(a%2==0 && a>9 ) cout<<"even"<< endl; 
                else if(a>9) cout<<"odd"<< endl; 
                a++; 
                if (a==b+1) break; 
            } 
        } 
        else 
        // Complete the code. 
        return 0; 
    } 
Function
    using namespace std; 
    /* 
    Add `int max_of_four(int a, int b, int c, int d)` here. 
    */ 
    int max_of_four(int a,int b,int c,int d) 
    { 
        if(a>b && a>c && a>d) return a; 
        else if(b>a && b>c && b>d) return b; 
        else if(c>a && c>b && c>d) return c; 
        else return d; 
    } 
    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
    void update(int *a,int *b) { 
        int temp=*a;  
        (*a)=(*a)+(*b); 
        (*b)=temp-(*b);
        if((*b)< 0) *b= (*b)-2*(*b); 
        // Complete this function     
    } 
    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
    using namespace std; 
    int main() 
    { 
        /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
        int n=0; 
        cin>>n; 
        int a[n],r[n]; 
        for(int i=0;i< n;i++) 
        { 
            cin>>a[i]; 
        } 
        int s=n-1; 
        for(int i=0;i< n+1;i++) 
        { 
                r[i]=a[s]; 
                s=s-1; 
        } 
        for(int i=0;i< n;i++) 
        { 
            cout<< r[i]<< " "; 
        } 
        return 0; 
    } 
Strings
    using namespace std; 
    int main() { 
        // Complete the program 
        string a,b; 
        int l1,l2; 
        cin>>a; 
        cin>>b; 
        cout<< a.size()<< " "<< b.size()<< endl;    
          cout<< a+b<< endl; 
        char t=a[0]; 
        a[0]=b[0]; 
        b[0]=t; 
        cout<< a<< " "<<  b; 
        return 0; 
    }
Structs
    using namespace std; 
    /*
        add code for struct here.
    */
    int main() { 
        Student st; 
        cin >> st.age >> st.first_name >> st.last_name >> st.standard;
        cout << st.age << " " << st.first_name << " " << st.last_name << " " << st.standard;
        return 0; 
    } 
Class
    using namespace std;
    
    /*
    Enter code for class Student here.
    Read statement for specification.
    */
    class Student
    {
    //private:
        
        public:
        int a,s;
        string l,f;
        void set_age(int age)
        {   a=age;
        }
        void set_standard(int standard)
        {   s=standard;
        }
        void set_first_name(string first_name)
        {f=first_name;
        }
        void set_last_name(string last_name)
        {   l=last_name;
        }
        int get_age()
        {return a;}
        string get_last_name()
        {return l;}
        string get_first_name()
        {return f;}
        int get_standard()
        {return s;}
        string to_string()
        {
            string x;
            x=f+","+l;
            return a+x+s;
        }
    };
    int main() {
        int age, standard;
        string first_name, last_name;
        cin >> age >> first_name >> last_name >> standard;
        Student st;
        st.set_age(age);
        st.set_standard(standard);
        st.set_first_name(first_name);
        st.set_last_name(last_name);    
        cout << st.get_age() << "\n";
        cout << st.get_last_name() << ", " << st.get_first_name() << "\n";
        cout << st.get_standard()<< "\n";
        cout << "\n";
        cout << st.to_string();    
        return 0;
    }
Classes and objects
// Write your Student class here
class Student
{
    int sum=0;int scores[5];
    public:
    void input()
    {
        for(int i=0;i< 5;i++)
        {
            cin>>scores[i];
            sum=sum+scores[i];
        }
    }
    int calculateTotalScore()
    {
        return sum;
    }
};
Box it!
//Implement the class Box  
class Box
{
    //l,b,h are integers representing the dimensions of the box
    private:
    int l,b,h;
    public:
    // The class should have the following functions : 
    // Constructors: 
    // Box();
    Box()
    {
        l=0;b=0;h=0;
    }
    // Box(int,int,int);
    Box(int length, int breadth, int height)
    {   
        l=length;b=breadth;h=height;
    }
    // Box(Box);
    Box(const Box &B)
    {
        l=B.l;b=B.b;h=B.h;
    }
    // int getLength(); // Return box's length
    int getLength()
    {return l;}
    // int getBreadth (); // Return box's breadth
    int getBreadth()     
    {return b;}
    // int getHeight ();  //Return box's height
    int getHeight()
    {return h;}
    // long long CalculateVolume(); // Return the volume of the box
    long long CalculateVolume()
    {return (long long)l*b*h;}
    //Overload operator < as specified
    //bool operator<(Box& b)
    bool operator<(Box& B)
    {
        int ll=B.getLength();
        int bb=B.getBreadth();
        int hh=B.getHeight();
        if(h==hh && b==bb && l==ll)
            return false;
        else if (l< ll || (b< bb && l==ll) || (h< hh && b==bb && l==ll))
            return true;
        else 
            return false;
    }    
};
//Overload operator << as specified
//ostream& operator<<(ostream& out, Box& B)
ostream& operator<<(ostream& out, Box& B)
    {
        int l=B.getLength();
        int b=B.getBreadth();
        int h=B.getHeight();
        return out<< l<<" "<< b<<" "<< h;
    }
Virtual Function
    using namespace std;
    class Person
    {
        public:
        string name;
        int age;
        virtual void getdata()
        {}
        virtual void putdata(){}
    };
    static int c1=0;
    static int c2=0;
    class Professor : public Person
    {
        public:
        int publications,cur_id;
        void getdata()
        {
            cin>>name>>age>>publications;
        }
        void putdata()
        {
            c1=c1+1;
            cur_id=c1;
            cout<< name<<" "<< age<<" "<< publications<<" "<< cur_id<< endl;       
        }
    };
    class Student : public Person
    {
        public:
        int marks[6],cur_id;
        void getdata()
        {
            cin>>name>>age;
            for(int i=0;i< 6;i++)
            {
                cin>>marks[i];
            }
        }
        void putdata()
        {
            int sum=0;
            c2=c2+1;
            cur_id=c2;
            for(int i=0;i< 6;i++)
            {
                sum=sum+marks[i];
            }
            cout<< name<<" "<< page<<" "<< sum<<" "<< cur_id<< endl;         
        }
    };     
    int main(){
        int n, val;
    cin>>n; //The number of objects that is going to be created.
    Person *per[n];
    for(int i = 0;i < n;i++){
        cin>>val;
        if(val == 1){
            // If val is 1 current object is of type Professor
            per[i] = new Professor;
        }
        else per[i] = new Student; // Else the current object is of type Student

        per[i]->getdata(); // Get the data from the user.
    }
    for(int i=0;i< n;i++)
        per[i]->putdata(); // Print the required output for each object.
    return 0;
}
Inheritance Introduction
    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;
    }
Rectangle Area
/*
 * Create classes Rectangle and RectangleArea
 */
 class Rectangle
{   
    public:
    int width,height;
    void display()
    {
        cout<< width<<" "<< height"\n";
    }
}; 
class RectangleArea : public Rectangle
{
    public:
    void read_input()
    {
        cin>>width>>height;
    }
    void display()
    {
        cout<< width*height;    
    }
}; 
Multi Level Inheritance
using namespace std;
    class Triangle{
        public:
            void triangle(){
                cout<<"I am a triangle\n";
            }
             void description()
            {
                cout<<"In an isosceles triangle two sides are equal";
            }
    };
    class Isosceles : public Triangle{
          public:
              void isosceles(){
                cout<<"I am an isosceles triangle\n";
              }
    };
    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;
    }