Hackerrank's Problem Solving solutions( Number Line Jumps, Breaking the Records, Sub-array Division ) :
Problem 13: Number Line Jumps
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()
{
    long long x1,v1,x2,v2;
    cin>>x1>>v1>>x2>>v2;
    if(v1!=v2)
    {
    long long n=((x2-x1)/(v1-v2));
    if(n>0 && (n*v1+x1==n*v2+x2))   //i calculated this formula using maths
    {
        cout<<"YES";
    }
    else
    {
       cout<<"NO";
    }
    }
      if(x1==x2 || v1==v2) 
       {
           cout<<"NO";
       }      
}  
This code is simple. There's no need for any explanation.
 
Problem 14: Breaking the Records
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()
{
    long long n,a[10000];
    cin>>n;
    for(int i=0;i<n;i++)
    {
      cin>>a[i];
    }
  long long max=a[0];
  int p=0;
  for(int j=0;j<n;j++)
  {
    if(a[j]>max)
    {
        max=a[j];
        p++;
    }
  }
   long long min=a[0];
  int q=0;
  for(int k=0;k<n;k++)
  {
    if(a[k]<min)
    {
        min=a[k];
        q++;
    }
  }
  cout<<p<<" "<<q;
}
This code is simple. There's no need for any explanation.
 
Problem 15: Sub-array Division
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,d,m;
    cin>>n;
    int s[n];
    for(int i=0;i<n;i++)
    {
        cin>>s[i];
    }
    cin>>d>>m;
    int sum;
    int count=0;
    for(int i=0;i<n-m+1;i++)
   {
       sum=0;
       for(int j=i;j<i+m;j++)
       {
           sum+=s[j];
       }
       if(sum==d)
       count++;
   }
   cout<<count;
}
This code is simple. There's no need for any explanation.
Guys , if you have any queries related to a question or need more explanation for a question , comment below!
 
Comments
Post a Comment