Posts

Showing posts from December, 2020

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 ++) {

Hackerrank's Problem Solving solutions( Viral Advertising, Save the Prisoner!, Circular Array Rotation ) :

Problem 31: Viral Advertising 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 , i , r = 5 , likes , ct = 0 ; //ct=count , r=initial no. of people cin >> n ; for ( i = 1 ; i <= n ; i ++) { likes = floor ( r / 2 ); //as said in the question ct = ct + likes ; r = likes * 3 ; //updating r } cout << ct ; } This code is simple. There's no need for any explanation.               Problem 32: Save the Prisoner! 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 t , n , m , s ; cin >> t ; while ( t --) { cin >> n >> m >> s ;

Hackerrank's Problem Solving solutions( Utopian Tree, Angry Professor, Beautiful Days at the Movies ) :

Problem 28: Utopian Tree 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 t ; //t=no. of test cases cin >> t ; while ( t --) { int n , h = 1 , i ; //h=height cin >> n ; if ( n == 0 ) { cout << "1" << endl ; } else { for ( i = 1 ; i <= n ; i ++) { if ( i % 2 == 0 ) { h = h + 1 ; } else { h = h * 2 ; } } cout << h << endl ; } } } This code is simple. There's no need for any explanation.             Problem 29: Angry Professor Solution: (in c++) ( please guys before moving to the solution try it yourself at least 3-4 times , if

Hackerrank's Problem Solving solutions( Picking Numbers, The Hurdle Race, Designer PDF Viewer ) :

Problem 25: Picking Numbers 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 , i , max = 0 , c , d ; cin >> n ; vector < int > v ( n ); for ( i = 0 ; i < n ; i ++) { cin >> v [ i ]; } for ( i = 0 ; i < n ; i ++) { c = count ( v . begin (), v . end (), v [ i ]); d = count ( v . begin (), v . end (), v [ i ]- 1 ); c = c + d ; if ( c > max ) //to find out the maximum value of c { max = c ; } } cout << max ; } In this question we have to find the length of the subarray from the given array which includes elements such that absolute difference between any two elements is less or equal to 1 i.e abs(x-y)<=1 .  What i have done here is we will count the occurrence of every element of the vect

Hackerrank's Problem Solving solutions( Counting Valleys, Electronics Shop, Cats and a Mouse ) :

Problem 22: Counting Valleys 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 , i , v = 0 , lvl = 0 ; //lvl=level , v=valleys cin >> n ; string s ; cin >> s ; for ( i = 0 ; i < s . length (); i ++) { if ( s [ i ]== 'U' ) { lvl ++; } if ( s [ i ]== 'D' ) { lvl --; } if ( lvl == 0 && s [ i ]== 'U' ) //see note below { v ++; } } cout << v ; } It means that "we just came out of a valley" because only then the final level will be zero and s[i]='U' means we just came out of the valley to make level = 0 . By counting how many number of time we came out of a valley , we can count the number of valleys.