Posts

Showing posts from December, 2021

Solution to 'Drawing Book' (HackerRank)

Hi all! Here's, my solution to the problem 'Drawing Book' from HackerRank. Question link:  https://www.hackerrank.com/challenges/drawing-book/problem #include <bits/stdc++.h> #define int long long #define pb push_back #define fi first #define se second #define pii pair < int,int > using namespace std ; int32_t main () {     ios_base :: sync_with_stdio ( false );     cin . tie ( NULL );     cout . tie ( NULL );     int n , p ;     cin >> n ;     cin >> p ;     int ans1 = p / 2 ;     if ( n % 2 == 0 ) n ++;     int ans2 =( n - p )/ 2 ;     int ans = min ( ans1 , ans2 );     cout << ans ;     return 0 ; }

Solution to the problem 'Counting Valleys' from HackerRank

 question link:  https://www.hackerrank.com/challenges/counting-valleys/problem #include <bits/stdc++.h> #define int long long #define pb push_back #define fi first #define se second #define pii pair < int,int > using namespace std ; int32_t main () {     ios_base :: sync_with_stdio ( false );     cin . tie ( NULL );     cout . tie ( NULL );     int n , ans = 0 ;     cin >> n ;     vector < int > hike ;     int prefix [ n ];     for ( int i = 0 ; i < n ; i ++){         char a ;         cin >> a ;         if ( a == 'U' ) hike . pb ( 1 );         else hike . pb (- 1 );     }     prefix [ 0 ]= hike [ 0 ] ;     for ( int i = 1 ; i < n ; i ++) prefix [ i ]= prefix [ i - 1 ]+ hike [ i ] ;     for ( int i = 1 ; i < n ; i ++){         if ( prefix [ i ]== 0 && prefix [ i - 1 ]==- 1 ) ans ++;     }     cout << ans ;     return 0 ; }

Solution to the problem 'Red Light, Green Light' from Codechef

Question Link:  https://www.codechef.com/INFI21C/problems/DOLL #include <bits/stdc++.h> #define int long long #define pb push_back #define fi first #define se second #define pii pair < int,int > using namespace std ; int32_t main () {     ios_base :: sync_with_stdio ( false );     cin . tie ( NULL );     cout . tie ( NULL );     int t ;     cin >> t ;     while ( t --){         int n , k , ans = 0 ;         cin >> n >> k ;         for ( int i = 0 ; i < n ; i ++){             int x ;             cin >> x ;             if ( x > k ) ans ++;         }         cout << ans << ' \n ' ;     }     return 0 ; }

Solution to the problem 'The Squid Game' form Codechef

Hola! Here's my solution to the problem 'The Squid Game' from Codechef Infinity 2K21 Division 3. Question link:  https://www.codechef.com/INFI21C/problems/SQUIDRULE #include <bits/stdc++.h> #define int long long #define pb push_back #define fi first #define se second #define pii pair < int,int > using namespace std ; int32_t main () {     ios_base :: sync_with_stdio ( false );     cin . tie ( NULL );     cout . tie ( NULL );     int t ;     cin >> t ;     while ( t --){         int n , ans = 0 , m = 1e4 + 1 ;         cin >> n ;         for ( int i = 0 ; i < n ; i ++){             int x ;             cin >> x ;             ans += x ;             if ( x < m ) m = x ;         }         ans = ans - m ;         cout << ans << ' \n ' ;     }     return 0 ; }

Solution of 'Sales by Match' (Hackerrank)

Hi all! Here's my solution to the problem 'Sales by Match' from HackerRank. Question link:  https://www.hackerrank.com/challenges/sock-merchant/problem #include <bits/stdc++.h> #define int long long #define pb push_back #define fi first #define se second #define pii pair < int,int > using namespace std ; int32_t main () {     ios_base :: sync_with_stdio ( false );     cin . tie ( NULL );     cout . tie ( NULL );     int n , ans = 0 ;     cin >> n ;     vector < int > arr ;     vector < int > c ;     for ( int i = 0 ; i < n ; i ++){         int x ;         cin >> x ;         arr . pb ( x );     }     sort ( arr . begin (), arr . end ());     for ( int i = 0 ; i < n ; i ++){         int a , cnt = 0 ;         a = arr [ i ] ;         while ( a == arr [ i ] ){             ++ cnt ;             i ++;         }         c . pb ( cnt );         i --;     }     for ( int i = 0 ; i < c . size (); i ++){         c [ i ] =

Solution of 'Divisible Sum Pairs' (HackerRank)

Hi all! Here's my solution to the problem 'Divisible Sum Pairs' from HackerRank. #include <bits/stdc++.h> #define int long long #define pb push_back #define fi first #define se second #define pii pair < int,int > using namespace std ; int divisibleSumPairs ( int n , int k , vector < int > ar ) {     int cnt = 0 ;     for ( int i = 0 ; i < n ; i ++){         for ( int j = i + 1 ; j < n ; j ++){             if (( ar [ i ] + ar [ j ] )% k == 0 ) cnt ++;         }     }     return cnt ; } int32_t main () {     int n , k ;     cin >> n >> k ;     vector < int > num ;     for ( int i = 0 ; i < n ; i ++){         int x ;         cin >> x ;         num . pb ( x );     }     int res = divisibleSumPairs ( n , k , num );     cout << res ; }

Solution of 'Breaking the Records' (HackerRank)

Hi all! Here's my solution to 'Breaking the Records' of HackerRank. #include <bits/stdc++.h> #define int long long #define pb push_back #define fi first #define se second #define pii pair < int,int > using namespace std ; vector < int > breakingRecords ( vector < int > scores ) {     int M = scores [ 0 ] , m = scores [ 0 ] , M_cnt = 0 , m_cnt = 0 ;     vector < int > temp ;     for ( int i = 0 ; i < scores . size (); i ++){         if ( scores [ i ] < m ){             m = scores [ i ] ;             m_cnt ++;         }         if ( scores [ i ] > M ){             M = scores [ i ] ;             M_cnt ++;         }     }     temp . pb ( M_cnt );     temp . pb ( m_cnt );     return temp ; } int32_t main () {     int n ;     cin >> n ;     vector < int > scores ;     for ( int i = 0 ; i < n ; i ++){         int x ;         cin >> x ;         scores . pb ( x );     }     vector < int > res = bre

Solution of Time Conversion (Hackerrank)

Hi all!! Here's my solution to the problem 'Time Conversion' on Hackerrank. Problem link: https://www.hackerrank.com/challenges/time-conversion/ #include <bits/stdc++.h> #define int long long #define pb push_back #define fi first #define se second #define pii pair < int,int > using namespace std ; string timeconversion ( string s ){     if ( s [ 8 ] == 'A' ){         s . erase ( 8 , 2 );         if ( s [ 0 ] == '1' && s [ 1 ] == '2' ){             s [ 0 ] = '0' ;             s [ 1 ] = '0' ;         }         return s ;     }     else {         s . erase ( 8 , 2 );         s [ 0 ] += 1 ;         s [ 1 ] += 2 ;         if ( s [ 0 ] == '2' && s [ 1 ] == '4' ){             s [ 0 ] = '1' ;             s [ 1 ] = '2' ;         }         return s ;     } } int32_t main () {     string s ;     cin >> s ;     string t = timeconversion ( s );     cout <<