Hackerrank's Problem Solving solutions( Sequence Equation, Jumping on the Clouds: Revisited, Find Digits ) :

Problem 34: Sequence Equation

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;
vector<int>v(n+1);
for(int i=1;i<=n;i++)
{
cin>>k;
v[k]=i;
}
for(int i=1;i<=n;i++)
{
cout<<v[v[i]]<<endl;
}
}

This code is simple. There's no need for any explanation.

 

 

 

 

 

 

 

 

 

 

Problem 35: Jumping on the Clouds: Revisited

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,i,ct=100;
cin>>n>>k;
vector<int>v(n);
for(i=0;i<n;i++)
{
cin>>v[i];
}
i=0;
while(1) //infinite loop
{
i=(i+k)%n; //jumping on next cloud
if(v[i]==0) //doing what is asked in the question
{
ct-=1;
}
else 
  {
ct-=3;
}
if(i==0) //she reached start again
{
break;
}
}
cout<<ct<<endl;
}

The loop will run till i = 0 (she reaches the starting position).

 

 

 

 

 

 

 

 

 

Problem 36: Find Digits

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()
{
string s;
long long t,i,n,ct;
cin>>t;
while(t--)
{
ct=0;
cin>>s;
n=stoi(s); //see note below
for(i=0;i<s.length();i++)
{
if((s[i]!='0') && (n%(s[i]-'0')==0))
{
ct++;
}
}
cout<<ct<<endl;
}
}

I know the input is integer but this question can be solved quickly using string. The stoi() function converts the string of digits into the number the digits are forming (i.e into integer) . Now we have to iterate the string and see whether each of it's digit satisfies the given condition.







Guys , if you have any queries or need more explanation for something , comment below! 

Comments

Popular posts from this blog

Coursera's Algorithmic toolbox assignment solutions( Sum of Two Digits, Maximum Pairwise Product ) :

HackerEarth's basic programming solutions( Seating Arrangement, Zoos, Anagrams ) :

HackerEarth's basic programming solutions( Minimize Cost, Magical Word, Best Index ) :