Posts

Find the Leader C++

  #include<bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; int arr[n]; int i; for (i = 0; i < n; i++) { cin >> arr[i]; } int max = arr[n - 1]; cout << arr[n - 1] << " "; for (i = n - 2; i >= 0; i--) { if (arr[i] >= max) { max = arr[i]; cout << max << " "; } } cout << '\n'; } return 0; }

Luck Balance Hackerrank

Question Link   Click Here C++ SOLUTION:  int luckBalance ( int k , vector < vector < int >> contests) { int i , j ; sort(contests.begin() , contests.end() , greater<>()) ; int max = 0 ; for (i = 0 ; i < contests.size() ; i++) { if (k == 0 && contests [ i ][ j + 1 ] == 1 ){ max = max - contests [ i ][ j ] ; } else if (contests [ i ][ j + 1 ] == 1 ) { k = k - 1 ; max = max + contests [ i ][ j ] ; } if (contests [ i ][ j + 1 ] == 0 ){ max = max = max + contests [ i ][ j ] ; } } return max ; }  

New Year Chaos - Hackerrank C++ Sol

 Question Link :  https://www.hackerrank.com/challenges/new-year-chaos/problem?isFullScreen=true&h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=arrays void minimumBribes (vector< int > A) { int n = A.size() ; int swaps = 0 ; for ( int i = n - 1 ; i >= 0 ; i--) { if (A[i] != (i + 1 )) { if (((i - 1 ) >= 0 ) && A[i - 1 ] == (i + 1 )) { swaps++ ; swap(A[i] , A[i- 1 ]) ; } else if (((i - 2 ) >= 0 ) && A[i - 2 ] == (i + 1 )) { swaps += 2 ; A[i - 2 ] = A[i - 1 ] ; A[i - 1 ] = A[i] ; A[i] = i + 1 ; } else { printf( "Too chaotic \n " ) ; return; } } } printf( "%d \n " , swaps) ; return; }

Unique Birthday Gift - INFOSYS - SAMPLE QUESTION - 2

Image
 Unique Birthday Gift - INFOSYS - SAMPLE QUESTION - 2 (SOLUTION ) #include <iostream> #include <vector> #include <utility> using namespace std ; int main () { int i ; int c = 0 ; int n , k ; cin >> n ; cin >> k ; vector < pair < int , int >>v ; int j ; if ( n == 1 || k == 1 ) { int temp ; temp = n>k?n:k; cout << temp<< endl ; } else { for (i = 1 ; i <= k+ 1 ; i++) { for (j = 1 ; j <= n; j++) { pair < int , int > p = make_pair (i, j); v. push_back (p); } } for ( auto it : v) { if ( it. second % it. first == 0 ) { c++ ; } } } if (c) { cout << c << endl ; } return 0 ; }

Infosys - Monster RPG GAME Sample Question Solution

Image
Monster RPG GAME - Solution #Infosys SOLUTION    #include <iostream> #include <map> #include <algorithm> using namespace std ; void game ( map < int , int > & g , int energy ) { int c = 0 ; for ( auto & itr : g ) { if ( energy >= itr. first ) { c++; energy = energy + itr. second ; } else { continue ; } } cout << c << endl ; } void mapping ( int p [], int b [], int n , int energy ) { int count ; map < int , int > m ; // mapping the key value pair from the array int i ; for ( i = 0 ; i < n ; i++) { m. insert ({ p [i], b [i]}); } for ( auto & itr : m) { cout << itr. first << " " << itr. second << endl ; } game (m, energy ) ; } int main () { int n ; cin >> n ; int i ; int p[n] ; int b[n] ; int e ; cin >> e ; // energy i have for (i = 0 ; i < n ; i++ ) { cin >> p[i] ...

Hackerrank -- C++ - Variable Sized Array Solution

#include   < cmath > #include   < cstdio > #include   < vector > #include   < iostream > #include   < algorithm > using   namespace   std ; int   main ()   {      /* Enter your code here. Read input from STDIN. Print output to STDOUT */              int   n ,   q ;      cin   >>   n   >>   q ;      int  ** arr   =   new   int *[ n ];      for   (   int   i   =   0 ,   k   ;   i   <   n   ;   ++   i   )      {          cin   >>   k ;              ...

Hackerrank C++ -------- Array Introduction

#include   < iostream > using   namespace   std ;    int   main (){      int   n   ,   arr [ 1000 ]   ,   i   ,   j   ,   g   ;      cin >>   n   ;         g   =   n    ;             for   (   i   =   0   ;   i < n   ;   i ++)   {          cin   >>   arr [ i ]   ;        }      for   (   j   =   g - 1   ;   j   >=   0   ;   j --   ){          cout   <<   arr [ j ]   <<   " "   ;        }        return   0 ; }