Posts

Hackerrank's Problem Solving solutions( Solve Me First, Simple Array Sum, Compare the Triplets ) :

Problem 1: Solve Me First 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 solveMeFirst ( int a , int b ) { return a + b ; } int main () { int num1 , num2 ; cin >> num1 >> num2 ; int sum ; sum = solveMeFirst ( num1 , num2 ); cout << sum ; } This code is simple. There's no need for any explanation.         Problem 2: Simple Array 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 , i , j , 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 ; } Thi

Coursera's Algorithmic toolbox assignment solutions( Collecting Signatures, Maximum Number of Prizes, Maximum Salary) :

Problem 5: Collecting Signatures 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) Still working on this problem...           Problem 6: Maximum Number of Prizes 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 , d ;     cin >> n ;      if ( n == 1 ) { cout << "1" << endl << "1" ; } else { d = n ; vector < long long > v ; for ( i = 1 ; i < n ; i ++) { if ( d > 2 * i ) { v . push_back ( i ); d -= i ; } else { v . push_back ( d ); break ; } } cout <<

Coursera's Algorithmic toolbox assignment solutions( Car Fueling, Maximum Advertisement Revenue ) :

Problem 3: Car Fueling 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 d , m , n , i , nr = 0 , cr = 0 , lr ; //d=distance between the cities , cr=currentrefill, cin >> d >> m >> n ; // m=max. distance by the car in a full tank long long a [ n + 1 ]; //lr=lastrefill , nr=no. of refills a [ 0 ]= 0 ; // array a represents gas stations positions for ( i = 1 ; i <= n ; i ++) { cin >> a [ i ]; } a [ n + 1 ]= d ; while ( cr <= n ) //this algorithm is explained perfectly { //in the lecture lr = cr ; while ( cr <= n && a [ cr + 1 ]- a [ lr ]<= m ) { cr = cr + 1 ; }

Coursera's Algorithmic toolbox assignment solutions( Money Change, Maximum Value of the Loot ) :

Problem 1: Money Change 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 , count = 0 ;   cin >> n ;    while ( n != 0 ) { if ( n >= 10 ) { n -= 10 ; count ++; } else if ( n < 10 && n >= 5 ) { n -= 5 ; count ++; } else { n -= 1 ; count ++; } } cout << count ; } This code is simple. There's no need for any explanation. or  you can also use this code below if denominations are given as input in descending order . #include < bits/stdc++.h > using namespace std ; int main () { int n ; //n=no. of denominations cin >> n ; int d [ n ], s , coins [ n ], count = 0 ; //d[n] is array of denominations ,

Coursera's Algorithmic toolbox assignment solutions( Last Digit of the Sum of Fibonacci Numbers Again, Last Digit of the Sum of Squares of Fibonacci Numbers ) :

Problem 7:  Last Digit of the Sum of Fibonacci Numbers Again 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 ; long long unsigned fib ( long long unsigned p ) //function to calculate nth fib. number { long long unsigned f [ p ], i ; f [ 0 ]= 0 ; f [ 1 ]= 1 ; for ( i = 2 ; i <= p ; i ++) { f [ i ]=( f [ i - 1 ]+ f [ i - 2 ])% 10 ; } return f [ p ]; } int main () { long long unsigned n , i , sum = 0 , m ; cin >> n >> m ; for ( i = n ; i <= m ; i ++) { sum += fib ( i ); } cout << sum % 10 ; } This code is simple. There's no need for any explanation.         Problem 8: Last Digit of the Sum of Squares of Fibonacci Numbers Solution: (in c++) ( please guys before moving to the solution try it yourself at least 3-4 times , if you really

Coursera's Algorithmic toolbox assignment solutions( Fibonacci Number Again, Find Product, Last Digit of the Sum of Fibonacci Numbers ) :

Problem 5: Fibonacci Number Again 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 ; long long unsigned get_p ( long long unsigned m ) //function to get the pisano period { long long unsigned a = 0 , b = 1 , c = a + b ; for ( long long unsigned int i = 0 ; i < m * m ; i ++) { c =( a + b )% m ; a = b ; b = c ; if ( a == 0 && b == 1 ) return i + 1 ; } } int main () { long long unsigned n , i , m , p ; cin >> n >> m ; p = get_p ( m ); n = n % p ; long long unsigned f [ n ]; f [ 0 ]= 0 ; f [ 1 ]= 1 ; for ( i = 2 ; i <= n ; i ++) { f [ i ]=( f [ i - 1 ]+ f [ i - 2 ])% m ; } cout << f [ n ]<< endl ; } In the pdf from coursera resources section where the question of this

Coursera's Algorithmic toolbox assignment solutions( Greatest Common Divisor, Least Common Multiple ) :

Problem 3: Greatest Common Divisor 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 gcd ( int a , int b ) { if ( b == 0 ) { return a ; } else { return gcd ( b , a % b ); } } int main () { int x , y ; cin >> x >> y ; cout << gcd ( x , y ); } This code is simple. There's no need for any explanation.         Problem 4: Least Common Multiple 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 ; long long gcd ( long long a , long long b ) { if ( b == 0 ) { return a ; } else { return gcd ( b , a % b ); } } int main () { long long x , y ; cin >> x >> y ; cout