Posts

Showing posts from 2022

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; }