Posts

Showing posts from September, 2020

How learning STL in c++ can help you code faster , shorter and simple?

All you need to know about C++ STL is here! After you have finished c++ basics and wondering what to do next then the best choice from my point of view is to start learning c++ STL .  Are you tired of writing big codes and just want to use functions directly like you use to do in strings in c ?  Along with a STL you also get many direct functions. Using STL for different data storage containers or data structures like arrays, stacks, queues, lists (both singly linked list and doubly linked list) etc, you do not have to write big and lengthy codes for creating them, inserting elements into them and a whole lot number of other operations. STLs are also really really helpful in competitive programming . NOTE : Prior to doing STL, you should learn about templates in c++ which comes under objective c++. But since you have to use direct functions, that's why not that much deep knowledge is required. If you can remember the syntax of a given function, that's fine also. What is STL (n

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/

Hackerrank's Problem Solving solutions( Time Conversion, Grading Students, Apple and Orange ) :

Problem 10: Time Conversion 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 , r ; cin >> s ; if ( s [ 8 ]== 'A' ) //Time in AM { if (( s [ 0 ] - '0' )== 1 && ( s [ 1 ] - '0' )== 2 ) //12 AM means hour = 00 { r = s . substr ( 2 , 6 ); //this substring r contains minute and second cout << "00" << r << endl ; } else // if not 12 AM everything as it is printed { r = s . substr ( 0 , 8 ); cout << r << endl ; } } else //time in PM { if (( s [ 0 ] - '0' )== 1 && ( s [ 1 ] - '0' )== 2 ) //if 12 PM means no change in hours { r = s . substr ( 2 , 6 ); //

Hackerrank's Problem Solving solutions( Staircase, Mini-Max Sum, Birthday Cake Candles ) :

Problem 7: Staircase 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 i , space , rows , k = 0 ; cin >> rows ; for ( i = 1 ; i <= rows ; ++ i , k = 0 ) { for ( space = 1 ; space <= rows - i ; ++ space ) { cout << " " ; } while ( k != i ) { cout << "#" ; ++ k ; } cout << endl ; } } This code is simple. There's no need for any explanation.         Problem 8: Mini-Max Sum 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 a [ 5 ], i , sum = 0 ; for ( i = 0 ; i < 5 ; i ++) {

Hackerrank's Problem Solving solutions( A Very Big Sum, Diagonal Difference, Plus Minus ) :

Problem 4: A Very Big Sum 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 , j , i ; long long sum ; cin >> n ; int a [ n ]; for ( i = 0 ; i < n ; i ++) cin >> a [ i ]; sum = 0 ; for ( j = 0 ; j < n ; j ++) { sum = sum + a [ j ]; } cout << sum ; } This code is simple. There's no need for any explanation.         Problem 5: Diagonal Difference 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 , a [ 100 ][ 100 ], sum , summ , i , d , j ; //summ = sum of '/' diagonal cin >> n ; for ( i = 0 ; i < n ; i ++) for ( j = 0 ; j < n ; j ++) {