Posts

LOCKER - Magic of the locker

#include <bits/stdc++.h> #define int long long #define pb push_back #define fi first #define se second #define pii pair < int,int > #define vi vector < int > using namespace std ; int m = 1e9 + 7 ; int modexp ( int x , int n ){     if ( n == 0 ) return 1 ;     if ( n % 2 == 0 ) return ( modexp ((( x % m )*( x % m ))% m , n / 2 ))% m ;     return (( x % m )*( modexp ((( x % m )*( x % m ))% m ,( n - 1 )/ 2 ))% m )% m ; } int32_t main () {     ios_base :: sync_with_stdio ( false );     cin . tie ( NULL );     cout . tie ( NULL );     int t ;     cin >> t ;     while ( t --){         int x ;         cin >> x ;         if ( x == 1 ){             cout << 1 << endl ;             continue ;         }         if ( x == 2 ){             cout << 2 << endl ;             continue ;         }         if ( x % 3 == 0 ) cout << modexp ( 3 , x / 3 ) << ' \n ' ;         else if ( x % 3 == 2 ) cout <<

Solution to the problem 'A' from 'Hello 2022' Contest (Codeforces)

Here's my solution to problem  A from the contest 'Hello 2022' of Codeforces .   Question Link:  https://codeforces.com/contest/1621/problem/A #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 , m = 0 ;         cin >> n >> k ;         if ( n % 2 == 0 ){             if ( k <= n / 2 ){                 for ( int i = 1 ; i <= n ; i ++){                     for ( int j = 1 ; j <= n ; j ++){                         if ( k > 0 && i == 2 * m + 1 && j == 2 * m + 1 ){                             cout << 'R' ;                             k --;                             m ++;                         }                    

Solution to the problem 'Electronics Shop' (HackerRank)

Hi all! Here's my solution to the problem 'Electronics Shop' from HackerRank. Question link:  https://www.hackerrank.com/challenges/electronics-shop/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 b , n , m ;     cin >> b >> n >> m ;     vector < int > keyboard , usb ;     for ( int i = 0 ; i < n ; i ++){         int x ;         cin >> x ;         keyboard . pb ( x );     }     for ( int i = 0 ; i < m ; i ++){         int x ;         cin >> x ;         usb . pb ( x );     }     sort ( keyboard . begin (), keyboard . end ());     sort ( usb . begin (), usb . end ());     int ans = 0 , flg = 0 ;     if ( keyboard [ 0 ] + usb [ 0 ] > b ) cout << - 1 ;     else {      

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