Hackerrank's Problem Solving solutions( Divisible Sum Pairs, Migratory Birds, Day of the Programmer ) :
Problem 16: Divisible Sum Pairs
Solution: (in c++)
( please guys before moving to the solution try it yourself at least 3-4 times , if you really wanna become a good coder)
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,k;
    cin>>n>>k;
    int a[n];
    for(int p=0;p<n;p++)
    {
        cin>>a[p];
    }
    int count=0;
     for(int i=0; i<n; i++)
       {       
        for(int j=i+1; j<n; j++)
        {    
            if((a[i]+a[j])%k==0)
            {    
                count++;           
            }    
        }        
    }
   cout<<count; 
}
This code is simple. There's no need for any explanation. 
Problem 17: Migratory Birds
Solution: (in c++)
( please guys before moving to the solution try it yourself at least 3-4 times , if you really wanna become a good coder)
#include<bits/stdc++.h>
using namespace std;
int main()
{
   map<long long ,long long>m;  //declaring a map
   long long n,i,max=0,p;
   cin>>n;
   long long a[n];
   for(i=0;i<n;i++)
   {
       cin>>a[i];
   }
   for(i=0;i<n;i++)
   {
       m[a[i]]++;    //you will understand this once you have done map STL
   }
  for(auto &e : m)   //map traversal
  {
      if(max<e.second)
      {
          max=e.second;
          p=e.first;
      }
  }
  cout<<p;
}
This code is simple. There's no need for any explanation.
Problem 18: Day of the Programmer
Solution: (in c++)
( please guys before moving to the solution try it yourself at least 3-4 times , if you really wanna become a good coder)
#include<bits/stdc++.h>
using namespace std;
 int leap(int n)
{
    if(n==1918)
     {
       return 2;
     }
    else if(n>=1919)              //---leap year program----
     {
       if(n%100==0 && n%400==0)
         {
           return 1;
         }
       else
         {
           if(n%4==0 && n%100!=0)
           return 1;
           else
           return 0;
         }
     }                              //----------------------
    else      //i.e (n<1918)
     {
       if(n%4==0)
       {
          return 1;
       }
       else
       {
         return 0;
       }
     }
}
int main()
{
    int y,p;
    cin>>y;
    p=leap(y);
    if(p==1)
    {
        cout<<"12.09."<<y;
    }
    else if(p==0) 
    {
        cout<<"13.09."<<y;
    }
    else
    {
        cout<<"26.09."<<y;    
    }
}
This code is simple. There's no need for any explanation.
Guys , if you have any queries or need more explanation for something , comment below!  
Comments
Post a Comment