├── 2D KADANE.cpp ├── BIT - EASY - VISA CODESPRINT.cpp ├── BITMASKING - MEDIUM- HACKERRANK HIRING CONTEST- WINNING LOTTERY TICKET.cpp ├── BITSET(MEDIUM) - CODEFORCES ER37.cpp ├── BOX STACKING PROBLEM - DP(MEDIUM) - HACKERBLOCKS.cpp ├── BUYING FRUITS - DP(1) - CODING BLOCKS.cpp ├── Boredom.cpp ├── CARDS GAME- MST - ADVANCED - GOOGLE KICKSTART.cpp ├── CATCH THEM ALL - GRAPH+PROBABILITY (TO DO) - KICKSTART.cpp ├── COMBINATORIAL PALINDROME - MEDIUM - HACKERRANK HOURRANK.cpp ├── CONSTRUCT UNIQUE NXN MATRIX WITH 1s PLACED AT ALL POSSIBLE DISTANCES FROM THE CENTRE.cpp ├── Classic Algos ├── Advanced STL.cpp ├── BRIDGE IN THE GRAPH - CODEFIGHTS.cpp ├── GRAPH COLORING - BACKTRACKING - CODEFIGHTS.cpp ├── LINKED LIST (ADVANCED) - ADD TWO HUGE NUMBERS. - CODEFIGHTS.cpp ├── LRU CACHE.cpp ├── LU_DECOMPOSITION - ADVANCED.cpp ├── LU_DECOMPOSITION - ADVANCED.exe ├── MO's ALGORITHM - DQUERY - SPOJ.cpp ├── Number Of Possible MSTs - TREES(ADVANCED) - CODEFIGHTS.cpp ├── PRIMS MST - EASY - CODEFIGHTS.cpp ├── SEGMENT TREE (MEDIUM) - HACEKREARTH - ADOBE CODIVA - MINI-MAXI QUERIES.cpp ├── TREE DECOMPOSITION - HLD(ADVANCED) - HACKERBLOCKS.cpp ├── TREE DIAMETER - MEDIUM - CODEFIGHTS.cpp ├── TREE TRAVERSAL (ADVANCED) - RESTORE TREE USING INORDER AND PREORDER WITHOUT RECURSION - CODEFIGHTS.cpp ├── TRIE (ADVANCED) + BACKTRACKING - CODEFIGHTS(wordBoggle).cpp ├── TRIE (ADVANCED) - CODEFIGHTS(findSubstrings).cpp ├── ZAlgorithm.cpp ├── input.txt ├── testing.cpp └── testing.exe ├── Coupon Collector Problem- CODING BLOCKS - COMBINATORICS (2).cpp ├── Crossing Blocks Game - HACKERBLOCKS.cpp ├── DANCE BATTLE - ADHOC(MEDIUM) - KICKSTART.cpp ├── DIJIKSTRA(ADVANCED) - CODEFIGHTS.cpp ├── DISCOUNT AT CODING BLOCKS.cpp ├── DP - MEDIUM - ADOBE - COST OF MERGING STRING.cpp ├── DP - MEDIUM - VISA CHECKSUM PROBLEM.cpp ├── DP Medium codemonk v2.cpp ├── DP(EASY) - MATRIX CHAINING - GEEKS.cpp ├── DSUBasic.cpp ├── DUPLICATE PERMUTATIONS - BACKTRACKING - CODING BLOCKS.cpp ├── EAT CAKE - DP(EASY) - KICKSTART.cpp ├── Editorials ├── EDITORIAL - BOX STACKING PROBLEM.md ├── EDITORIAL - BUYING FRUITS.md ├── EDITORIAL - COUPON COLLECTOR PROBLEM.md ├── EDITORIAL - DISCOUNT AT CODING BLOCKS.md ├── EDITORIAL - EXPAND THE POND.md ├── EDITORIAL - FRIENDS AT CODING BLOCKS.md ├── EDITORIAL - GYM IN CODING BLOCKS.md ├── EDITORIAL - HOLI .md ├── EDITORIAL - HOODIES AT CODING BLOCKS.md ├── EDITORIAL - KILL THE BIRDS.md ├── EDITORIAL - LIMITED BUDGET PARTY.md ├── EDITORIAL - LITTLE VIPUL AND HIS QUERIES.md ├── EDITORIAL - MATH DAY.md ├── EDITORIAL - MATRIX FLIP.md ├── EDITORIAL - MATRIX SEARCH.md ├── EDITORIAL - MAXIMUM SUM SUBARRAY.md ├── EDITORIAL - MIN STRING.md ├── EDITORIAL - MODULO SUM.md ├── EDITORIAL - MONEY CHANGE.md ├── EDITORIAL - MURTHAL PARANTHA.md ├── EDITORIAL - NEIGHBOURING ENEMY.md ├── EDITORIAL - PAINTER'S PARTITION.md ├── EDITORIAL - PAIR OF ROSES.md ├── EDITORIAL - PARTNERS.md ├── EDITORIAL - PIRATES AND THEIR CHESTS.md ├── EDITORIAL - PLACING THE TILES.md ├── EDITORIAL - QUEST FOR CODING BLOCKS TSHIRTS.md ├── EDITORIAL - RANDOM QUERY.md ├── EDITORIAL - REGEX MATCHING.md ├── EDITORIAL - ROTATE THE MATRIX.md ├── EDITORIAL - SCRABBLING THE BITS.md ├── EDITORIAL - SEQ - RECURSIVE SEQUENCE.md ├── EDITORIAL - SINGHAM AND HIS LOVE FOR LADDUS.md ├── EDITORIAL - SINGHAM AND HIS NEED FOR MONEY.md ├── EDITORIAL - SINGHAM AND MAZE.md ├── EDITORIAL - SITTING ARRANGEMENT AT CODING BLOCKS.md ├── EDITORIAL - STRING SUBSEQUENCES.md ├── EDITORIAL - SUM IT UP.md ├── EDITORIAL - SWAP SWAP.md ├── EDITORIAL - THE ABSOLUTE SUM FUNCTION.md ├── EDITORIAL - THE BEST NUMBER.md ├── EDITORIAL - THE CODING BLOCKS TRIO.md ├── EDITORIAL - TILLING PROBLEM - II.md ├── EDITORIAL - TREE DECOMPOSITION.md ├── EDITORIAL - TRICKY PERMUTATIONS.md ├── EDITORIAL - VIPUL AND HIS LOVE FOR STRINGS.md ├── EDITORIAL - VIPUL'S REDMI 4.md ├── EDITORIAL - WILDCARD PATTERN MATCHING.md ├── EDITORIAL - WINTER VACATIONS.md └── EDITORIAL - XORed ARRANGEMENT.md ├── Expand the pond.cpp ├── Expectation - RANDOM QUERY - CODING BLOCKS .cpp ├── FRIENDS AT CODING BLOCKS - BITMASKING - DP - HACKERBLOCKS.cpp ├── GRAPHS(MEDIUM) - CODEAGON - COUNTING PRINCESS PRESENTS.cpp ├── GYM IN CODING BLOCKS - DP(MEDIUM-HARD) - HACKERBLOCKS.cpp ├── HASHMAP - ADVANCED - HACKER EARTH HIRING - ARRAYS AND QUERIES.cpp ├── HOLI - SPOJ-HACKERBLOCKS - GRAPHS - MEDIUM.cpp ├── HOODIES AT CODING BLOCKS - QUEUES - HACKERBLOCKS.cpp ├── KICKSORT - ADHO(MEDIUM) - KICKSTART.cpp ├── KICKSTART - PRACTICE - 2018 - 1.CPP ├── KICKSTART - PRACTICE - 2018 - 2.CPP ├── KICKSTART - PRACTICE - 2018 - 3.CPP ├── KILL THE BIRDS - DP(2) - CODING BLOCKS.cpp ├── KMP.cpp ├── LIMITED BUDGET PARTY - ARRAY - TWO POINTERS - HACKERBLOCKS.cpp ├── LINKED LIST (ADVANCED) - ADD TWO HUGE NUMBERS. - CODEFIGHTS.cpp ├── LITTLE VIPUL AND HIS QUERIES.cpp ├── MATH - MEDIUM - HACKERRANK HIRING - THE SIMPLEST SUM.cpp ├── MATH DAY - MODULAR EXPONENTIATION - HACKERBLOCKS.cpp ├── MATRIX CUTTING - DP(EASY)- KICKSTART.cpp ├── MATRIX FLIP - ADHOC - HACKERBLOCKS.cpp ├── MATRIX SEARCH - LINEAR SEARCH - CODING BLOCKS.cpp ├── MAXIMUM SUM SUBARRAY - 2D KADANE - HACKERBLOCKS.cpp ├── MIN STRING - DP(HARD) - CODING BLOCKS.CPP ├── MO's ALGORITHM - DQUERY - SPOJ.cpp ├── MODULO SUM- CODING BLOCKS - DP(1).cpp ├── MONEY CHANGE - DP (EASY) - CODING BLOCKS.cpp ├── MURTHAL PARANTHA - BINARY SEARCH - CODING BLOCKS.cpp ├── Neighbouring Enemy -DP(Easy) -CODING BLOCKS.cpp ├── PAINTER'S PARTITION PROBLEM - BINARY SEARCH - CODEBLOCKS.cpp ├── PAIR OF ROSES - BINARY SEARCH - CODING BLOCKS.cpp ├── PARTNERS - STACKS - EASY - HACKERBLOCKS.cpp ├── PIRATES AND THEIR CHESTS.cpp ├── PLACING THE TILES - DP - CODING BLOCKS.cpp ├── QUEST FOR CODING BLOCK TSHIRTS.cpp ├── README.md ├── REGEX MATCHING - DP(MEDIUM) - HACKERBLOCKS.cpp ├── ROTATE THE MATRIX - ADHOC - HACKERBLOCKS.cpp ├── SCRABBLING THE BITS - BIT MANIPULATION - HACKERBLOCKS - EASY.cpp ├── SEGMENT TREE (MEDIUM) - CHEF AND SUBARRAY QUERIES.cpp ├── SEGMENT TREE (MEDIUM) - HACEKREARTH - ADOBE CODIVA - MINI-MAXI QUERIES.cpp ├── SEGMENT TREE + MATHS (MEDIUM) - SUM AND REPLACE - CODEFORCES 920F.cpp ├── SEGMENT TREE + MATHS (MEDIUM) - SUM AND REPLACE - CODEFORCES ER37F.cpp ├── SEQ - CODING BLOCKS - MATRIX EXPONENTIATION(1).cpp ├── SIMPLE ENOUGH - DnC-Adhoc - HACKERBLOCKS.cpp ├── SINGHAM AND HIS LOVE FOR LADDUS.cpp ├── SINGHAM AND HIS NEED FOR MONEY.cpp ├── SINGHAM AND MAZE - DP - HACKERBLOCKS.cpp ├── SITTING ARRANGEMENT AT CODING BLOCKS.cpp ├── STACK - EASY - VISA CODESPRINT.cpp ├── SUM IT UP- RECURSION - HACKERBLOCKS.cpp ├── SWAP SWAP - SEGMENT TREES - HACKERBLOCKS(HARD).cpp ├── String Subsequences - CODING BLOCKS - DP(3) - PROBLEM NOT SUBMITTED.cpp ├── String Subsequences - CODING BLOCKS - DP(3).cpp ├── String Subsequences.cpp ├── THE ABSOLUTE SUM FUNCTION - DP(MEDIUM) - HACKERBLOCKS.cpp ├── THE BEST NUMBER - BIT MANIPULATION - EASY- CODING BLOCKS.cpp ├── THE CODING BLOCKS TRIO - BITMASKING - DP(MEDIUM).cpp ├── THE PIRATES AND THEIR CHESTS.cpp ├── TILLING PROBLEM - II.cpp ├── TREE DECOMPOSITION - HLD(ADVANCED) - HACKERBLOCKS.cpp ├── TREE TRAVERSAL (ADVANCED) - RESTORE TREE USING INORDER AND PREORDER WITHOUT RECURSION - CODEFIGHTS.cpp ├── TRIE (ADVANCED) - CODEFIGHTS(findSubstrings).cpp ├── Test Case Generator.cpp ├── VIPUL AND HIS LOVE FOR STRINGS - DP(HARD) - CODING BLOCKS.cpp ├── VIPULS REDMI 4 - DP(ADVNACED) - HACKERBLOCKS.cpp ├── WILDCARD PATTERN MATCHING - DP(MEDIUM) - HACKERBLOCKS.cpp ├── WILDCARD PATTERN MATCHING - DP(MEDIUM).cpp ├── WINTER VACATIONS - HACKERBLOCKS - DP ├── WINTER VACATIONS - HACKERBLOCKS - DP(EASY).cpp ├── XORED ARRANGEMENT.cpp ├── count Permutations of BST.cpp ├── graph medium codeagon 2.cpp ├── graph medium codeagon.cpp ├── input.txt ├── output.txt ├── priority queue advanced.cpp ├── testing.cpp ├── testing.exe ├── testing1.cpp └── testing3.cpp /2D KADANE.cpp: -------------------------------------------------------------------------------- 1 | // http://www.geeksforgeeks.org/dynamic-programming-set-27-max-sum-rectangle-in-a-2d-matrix/ 2 | 3 | #include 4 | #include 5 | using namespace std; 6 | #define fio ios_base::sync_with_stdio(false) 7 | 8 | #define ll int 9 | 10 | #define s(x) scanf("%d",&x) 11 | #define s2(x,y) s(x)+s(y) 12 | #define s3(x,y,z) s(x)+s(y)+s(z) 13 | 14 | #define p(x) printf("%d\n",x) 15 | #define p2(x,y) p(x)+p(y) 16 | #define p3(x,y,z) p(x)+p(y)+p(z) 17 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 18 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 19 | 20 | #define ff first 21 | #define ss second 22 | #define mp(x,y) make_pair(x,y) 23 | #define pll pair 24 | #define pb push_back 25 | 26 | ll inf = 1e18; 27 | ll mod = 1e9 + 7 ; 28 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 29 | 30 | ll maxsum(INT_MIN),maxlef(-1),maxryt(-1),maxup(-1),maxdn(-1),cursum(0); 31 | 32 | void init(){ 33 | maxsum=INT_MIN; 34 | maxlef=-1; 35 | maxryt=-1; 36 | maxup=-1; 37 | maxdn=-1; 38 | cursum=0; 39 | } 40 | 41 | void solve(vector &kadane, ll lf, ll rt){ 42 | ll n = kadane.size(); 43 | if(n==0)return; 44 | ll mxsum=kadane[0]; 45 | ll cursum=kadane[0]; 46 | ll stlf=0; 47 | ll mxlf=0; 48 | ll mxrt=0; 49 | 50 | for(ll i=1;imxsum){ 59 | mxsum=cursum; 60 | mxlf=stlf; 61 | mxrt=i; 62 | } 63 | } 64 | if(mxsum>maxsum){ 65 | maxsum=mxsum; 66 | maxlef=lf; 67 | maxryt=rt; 68 | maxup=mxlf; 69 | maxdn=mxrt; 70 | } 71 | // cout<> mat; 85 | mat.resize(r); 86 | for(ll i=0;i>mat[i][j]; 90 | } 91 | } 92 | 93 | vector kadane; 94 | for(ll lf=0;lf 3 | #include 4 | using namespace std; 5 | #define fio ios_base::sync_with_stdio(false) 6 | 7 | #define ll long long int 8 | 9 | #define s(x) scanf("%lld",&x) 10 | #define s2(x,y) s(x)+s(y) 11 | #define s3(x,y,z) s(x)+s(y)+s(z) 12 | 13 | #define p(x) printf("%lld\n",x) 14 | #define p2(x,y) p(x)+p(y) 15 | #define p3(x,y,z) p(x)+p(y)+p(z) 16 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 17 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 18 | 19 | #define ff first 20 | #define ss second 21 | #define mp(x,y) make_pair(x,y) 22 | #define pll pair 23 | #define pb push_back 24 | 25 | ll inf = 1e18; 26 | ll mod = 1e9 + 7 ; 27 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 28 | 29 | /****************************************************************************/ 30 | 31 | vector ar; 32 | vector BIT; 33 | 34 | void updateBit(ll ind,ll val){ 35 | while(ind<5000001){ 36 | BIT[ind]+=val; 37 | ind+=ind&-ind; 38 | } 39 | } 40 | 41 | ll RMQ(ll ind){ 42 | ll ans=0ll; 43 | while(ind>0){ 44 | ans+=BIT[ind]; 45 | ind-=ind&-ind; 46 | } 47 | return ans; 48 | } 49 | 50 | int main() 51 | { 52 | freopen("input.txt","r",stdin); 53 | // freopen("output.txt","w",stdout); 54 | ll t=1; 55 | // s(t); 56 | while(t--){ 57 | ll n,q; 58 | s2(n,q); 59 | ar.resize(n+5); 60 | BIT.resize(5000000+5,0); 61 | F(i,0,n-1){ 62 | cin>>ar[i]; 63 | updateBit(ar[i],1ll); 64 | } 65 | 66 | while(q--){ 67 | string query; 68 | cin>>query; 69 | if(query=="count"){ 70 | ll x; 71 | s(x); 72 | cout<< RMQ(5000000)-RMQ(x-1)< 4 | #include 5 | using namespace std; 6 | #define fio ios_base::sync_with_stdio(false) 7 | 8 | #define ll int 9 | 10 | #define s(x) scanf("%d",&x) 11 | #define s2(x,y) s(x)+s(y) 12 | #define s3(x,y,z) s(x)+s(y)+s(z) 13 | 14 | #define p(x) printf("%d\n",x) 15 | #define p2(x,y) p(x)+p(y) 16 | #define p3(x,y,z) p(x)+p(y)+p(z) 17 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 18 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 19 | 20 | #define ff first 21 | #define ss second 22 | #define mp(x,y) make_pair(x,y) 23 | #define pll pair 24 | #define pb push_back 25 | 26 | // ll inf = 1e18; 27 | ll mod = 1e9 + 7 ; 28 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 29 | 30 | /****************************************************************************/ 31 | 32 | int main() 33 | { 34 | freopen("input.txt","r",stdin); 35 | // freopen("output.txt","w",stdout); 36 | ll t=1; 37 | // s(t); 38 | while(t--){ 39 | ll n; 40 | s(n); 41 | vector maskVector; 42 | map countMask; 43 | F(i,1,n){ 44 | string str; 45 | cin>>str; 46 | int mask=0; 47 | ll sz=str.size(); 48 | for(ll j=0;j0ll)ans+=(((countMask[1023]-1ll)*countMask[1023]))/2LL; 58 | 59 | n = maskVector.size(); 60 | F(i,0,n-1){ 61 | F(j,i+1,n-1){ 62 | if((maskVector[i]|maskVector[j]) == (1023)){ 63 | ans+=(long long int)countMask[maskVector[i]]*(long long int)countMask[maskVector[j]]; 64 | } 65 | } 66 | } 67 | 68 | cout< 4 | #include 5 | using namespace std; 6 | #define fio ios_base::sync_with_stdio(false) 7 | 8 | #define ll long long int 9 | 10 | #define s(x) scanf("%lld",&x) 11 | #define s2(x,y) s(x)+s(y) 12 | #define s3(x,y,z) s(x)+s(y)+s(z) 13 | 14 | #define p(x) printf("%lld\n",x) 15 | #define p2(x,y) p(x)+p(y) 16 | #define p3(x,y,z) p(x)+p(y)+p(z) 17 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 18 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 19 | 20 | #define ff first 21 | #define ss second 22 | #define mp(x,y) make_pair(x,y) 23 | #define pll pair 24 | #define pb push_back 25 | 26 | const ll maxi = 2e5 + 5; 27 | ll inf = 1e18; 28 | ll mod = 1e9 + 7 ; 29 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 30 | 31 | /**************************CODE GOES HERE*********************************************/ 32 | 33 | unordered_map edge[maxi]; 34 | vectorans; 35 | bitset notVis; 36 | ll k; 37 | void dfs(ll src){ 38 | notVis[src]=0; 39 | ans[k]++; 40 | for(int i=notVis._Find_first();i 3 | // #include 4 | using namespace std; 5 | #define fio ios_base::sync_with_stdio(false) 6 | 7 | #define ll long long int 8 | 9 | #define s(x) scanf("%lld",&x) 10 | #define s2(x,y) s(x)+s(y) 11 | #define s3(x,y,z) s(x)+s(y)+s(z) 12 | 13 | #define p(x) printf("%lld\n",x) 14 | #define p2(x,y) p(x)+p(y) 15 | #define p3(x,y,z) p(x)+p(y)+p(z) 16 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 17 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 18 | 19 | #define ff first 20 | #define ss second 21 | #define mp(x,y) make_pair(x,y) 22 | #define pll pair 23 | #define pb push_back 24 | 25 | ll inf = 1e18; 26 | ll mod = 1e9 + 7 ; 27 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 28 | 29 | int main() 30 | { 31 | // freopen("input.txt","r",stdin); 32 | // freopen("output.txt","w",stdout); 33 | ll t=1; 34 | s(t); 35 | while(t--){ 36 | ll n; 37 | s(n); 38 | vectorar[n]; 39 | F(i,0,n-1){ 40 | ar[i]=vector(3,0); 41 | F(j,0,2){ 42 | cin>>ar[i][j]; 43 | } 44 | } 45 | 46 | // dp[i][j] indicates the minimum cost of shopping if the jth fruit is purchased 47 | // from shop i (considering only till ith shop). 48 | ll dp[n][3]={0}; 49 | dp[0][0]=ar[0][0]; dp[0][1]=ar[0][1]; dp[0][2]=ar[0][2]; 50 | F(i,1,n-1){ 51 | 52 | // If you are purchasing apples from ith shop, you can only purchase mangoes 53 | // or oranges from the previous shop. Choose the one which gives minimum cost. 54 | dp[i][0]=ar[i][0]+min(dp[i-1][1],dp[i-1][2]); 55 | 56 | // Similar as the above case. 57 | dp[i][1]=ar[i][1]+min(dp[i-1][0],dp[i-1][2]); 58 | 59 | // Similar as the above case. 60 | dp[i][2]=ar[i][2]+min(dp[i-1][0],dp[i-1][1]); 61 | } 62 | 63 | // Now since we have considered all n shops, print the one which gives the minimum 64 | // cost. 65 | cout< 7 | #include 8 | using namespace std; 9 | #define fio ios_base::sync_with_stdio(false) 10 | 11 | #define ll long long int 12 | 13 | #define s(x) scanf("%lld",&x) 14 | #define s2(x,y) s(x)+s(y) 15 | #define s3(x,y,z) s(x)+s(y)+s(z) 16 | 17 | #define p(x) printf("%lld\n",x) 18 | #define p2(x,y) p(x)+p(y) 19 | #define p3(x,y,z) p(x)+p(y)+p(z) 20 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 21 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 22 | 23 | #define ff first 24 | #define ss second 25 | #define mp(x,y) make_pair(x,y) 26 | #define pll pair 27 | #define pb push_back 28 | 29 | ll inf = 1e18; 30 | ll mod = 1e9 + 7 ; 31 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 32 | 33 | int main() 34 | { 35 | // freopen("input.txt","r",stdin); 36 | // freopen("output.txt","w",stdout); 37 | ll t=1; 38 | // s(t); 39 | while(t--){ 40 | ll n; 41 | s(n); 42 | vectorb; 43 | mapmp; 44 | ll num; 45 | F(i,0,n-1){cin>>num; mp[num]++;} 46 | for(auto ele:mp){ 47 | b.push_back(ele.first); 48 | } 49 | 50 | n=b.size(); 51 | vectordp(n,0); 52 | dp[0]=b[0]*mp[b[0]]; 53 | 54 | for(ll i=1;i=2) temp+=dp[i-2]; 58 | dp[i]=max(dp[i-1],temp); 59 | }else dp[i]=dp[i-1]+b[i]*mp[b[i]]; 60 | } 61 | cout< 4 | #include 5 | using namespace std; 6 | #define fio ios_base::sync_with_stdio(false) 7 | 8 | #define ll long long int 9 | 10 | #define s(x) scanf("%lld",&x) 11 | #define s2(x,y) s(x)+s(y) 12 | #define s3(x,y,z) s(x)+s(y)+s(z) 13 | 14 | #define p(x) printf("%lld\n",x) 15 | #define p2(x,y) p(x)+p(y) 16 | #define p3(x,y,z) p(x)+p(y)+p(z) 17 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 18 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 19 | 20 | #define ff first 21 | #define ss second 22 | #define mp(x,y) make_pair(x,y) 23 | #define pll pair 24 | #define pb push_back 25 | 26 | ll inf = 1e18; 27 | ll mod = 1e9 + 7 ; 28 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 29 | 30 | /****************************************************************************/ 31 | ll n; 32 | vector> adj; 33 | 34 | ll prims(){ 35 | priority_queue,greater> Q; 36 | 37 | vector vis(n+5,false); 38 | 39 | Q.push({0,1}); 40 | ll ans=0ll; 41 | 42 | while(!Q.empty()){ 43 | pll top = Q.top(); 44 | Q.pop(); 45 | ll node = top.second; 46 | ll cost = top.first; 47 | if(vis[node])continue; 48 | vis[node]=true; 49 | ans+=cost; 50 | F(i,0,adj[node].size()-1){ 51 | ll ch = adj[node][i].first; 52 | ll wt = adj[node][i].second; 53 | if(vis[ch])continue; 54 | Q.push({wt,ch}); 55 | } 56 | } 57 | return ans; 58 | } 59 | 60 | int main() 61 | { 62 | freopen("input.txt","r",stdin); 63 | freopen("output.txt","w",stdout); 64 | ll t=1; 65 | ll tc=1; 66 | s(t); 67 | while(t--){ 68 | s(n); 69 | vectorred(n+1),blue(n+1); 70 | adj.clear(); 71 | adj.resize(n+4); 72 | F(i,1,n) cin>>red[i]; 73 | F(i,1,n) cin>>blue[i]; 74 | F(i,1,n){ 75 | F(j,i+1,n){ 76 | ll cost = min(red[i]^blue[j],red[j]^blue[i]); 77 | adj[i].pb({j,cost}); 78 | adj[j].pb({i,cost}); 79 | } 80 | } 81 | ll minCost = prims(); 82 | cout<<"Case #"< 3 | #include 4 | using namespace std; 5 | #define fio ios_base::sync_with_stdio(false) 6 | 7 | #define ll long long int 8 | 9 | #define s(x) scanf("%lld",&x) 10 | #define s2(x,y) s(x)+s(y) 11 | #define s3(x,y,z) s(x)+s(y)+s(z) 12 | 13 | #define p(x) printf("%lld\n",x) 14 | #define p2(x,y) p(x)+p(y) 15 | #define p3(x,y,z) p(x)+p(y)+p(z) 16 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 17 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 18 | 19 | #define ff first 20 | #define ss second 21 | #define mp(x,y) make_pair(x,y) 22 | #define pll pair 23 | #define pb push_back 24 | 25 | const ll inf = 1e18; 26 | const ll mod = 1e9 + 7 ; 27 | const ll maxi = 1e5 + 5; 28 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 29 | 30 | /**************************CODE GOES HERE*********************************************/ 31 | 32 | ll modExpo(ll a,ll e){ 33 | if(a<=1)return a; 34 | ll ans=1ll; 35 | while(e>0){ 36 | if(e&1)ans=(ans*a)%mod; 37 | e>>=1; 38 | a=(a*a)%mod; 39 | } 40 | return ans; 41 | } 42 | 43 | ll fact[maxi]; 44 | ll rfact[maxi]; 45 | ll freq[30][maxi]; 46 | 47 | void init(string str){ 48 | 49 | memset(freq,0,sizeof(freq)); 50 | 51 | fact[0]=1ll; 52 | rfact[0]=1ll; 53 | for(ll i=1;i<=maxi;i++){ 54 | fact[i]=(fact[i-1]*i)%mod; 55 | rfact[i]=modExpo(fact[i],mod-2); 56 | } 57 | 58 | str="."+str; 59 | ll sz=str.size(); 60 | for(ll i=1;i>str; 79 | init(str); 80 | ll q; 81 | s(q); 82 | while(q--){ 83 | ll l,r; 84 | s2(l,r); 85 | ll ans=1ll; 86 | ll sum=0ll; 87 | ll odd=0ll; 88 | for(char ch='a';ch<='z';ch++){ 89 | ll cur = freq[ch-'a'][r]-freq[ch-'a'][l-1]; 90 | if((cur%2) == 1)odd++; 91 | ans=(ans*rfact[(cur/2)])%mod; 92 | sum+=(cur/2ll); 93 | } 94 | ans=(ans*max(1ll,odd))%mod; 95 | ans=(ans*fact[sum])%mod; 96 | cout< 4 | #include 5 | using namespace std; 6 | #define fio ios_base::sync_with_stdio(false) 7 | 8 | #define ll long long int 9 | 10 | #define s(x) scanf("%lld",&x) 11 | #define s2(x,y) s(x)+s(y) 12 | #define s3(x,y,z) s(x)+s(y)+s(z) 13 | 14 | #define p(x) printf("%lld\n",x) 15 | #define p2(x,y) p(x)+p(y) 16 | #define p3(x,y,z) p(x)+p(y)+p(z) 17 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 18 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 19 | 20 | #define ff first 21 | #define ss second 22 | #define mp(x,y) make_pair(x,y) 23 | #define pll pair 24 | #define pb push_back 25 | 26 | ll inf = 1e18; 27 | ll mod = 1e9 + 7 ; 28 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 29 | 30 | /****************************************************************************/ 31 | 32 | class SquareMatrix{ 33 | vector> mat; 34 | ll sz; 35 | 36 | void fillMatrix(ll i,ll j){ 37 | ll toFill = 1; 38 | F(in,i,sz-1)mat[in][j]=toFill++; 39 | F(in,0,i-1)mat[in][j]=toFill++; 40 | } 41 | 42 | public: 43 | SquareMatrix(ll n=0):sz(n){ 44 | assert(n%2 != 0); 45 | mat = vector>(n,vector(n,0)); 46 | } 47 | 48 | void printMatrix(){ 49 | F(i,0,sz-1){ 50 | F(j,0,sz-1){ 51 | cout< sortedSquaredArray(std::vector array) { 4 | int sz=array.size(); 5 | if(sz==0)return array; 6 | int posElemIndex=find_if(array.begin(),array.end(),[](int i)->bool{ 7 | return i>=0; 8 | })-array.begin(); 9 | cout<<"Positive Element Index: "<negSquare(array.begin(),array.begin()+posElemIndex); 11 | vectorposSquare(array.begin()+posElemIndex,array.end()); 12 | transform(negSquare.begin(),negSquare.end(),negSquare.begin(),[](int i)->int{ 13 | return i*i; 14 | }); 15 | for_each(negSquare.begin(),negSquare.end(),[](int i)->int{ 16 | cout<int{ 20 | return i*i; 21 | }); 22 | for_each(posSquare.begin(),posSquare.end(),[](int i)->int{ 23 | cout< vis; // visited status of the nodes 6 | vector par; // parent of the nodes 7 | vector low; // low time of the nodes 8 | vector disc; // discovery time of the nodes 9 | 10 | void findBridges(int node,int parent,vector> &connections){ 11 | int independentChildren=0; 12 | vis[node]=1; 13 | par[node]=parent; 14 | low[node]=time; 15 | disc[node]=time; 16 | time++; 17 | for(int i=0;i> connections) { 30 | int sz=connections.size(); 31 | ans=0; 32 | time=0; 33 | vis=vector(sz,false); 34 | par=vector(sz,-1); 35 | low=vector(sz,INT_MAX); 36 | disc=vector(sz,INT_MAX); 37 | for(int i=0;i>adj; 4 | vectorcolor; 5 | int totColors; 6 | const int MAX_COLORS_ALLOWED = 5; 7 | 8 | bool isSafe(int node,int colr){ 9 | for(int i=0;i(sz,0); 32 | if(colorGraphUtil(0,maxAllowedColors))return true; 33 | return false; 34 | } 35 | 36 | int feedingTime(std::vector> classes) { 37 | int sz=classes.size(); 38 | adj=vector>(sz); 39 | for(int i=0;i ustA; 41 | for(int j=0;j 4 | #include 5 | unordered_map::iterator> ump; 6 | list dll; 7 | int cap; 8 | LRUCache::LRUCache(int capacity) { 9 | ump.clear(); 10 | dll.clear(); 11 | cap=capacity; 12 | } 13 | 14 | int LRUCache::get(int key) { 15 | if(ump.find(key)==ump.end())return -1; 16 | int val = (*(ump[key])).second; 17 | set(key,val); 18 | return (*(ump[key])).second; 19 | } 20 | 21 | void LRUCache::set(int key, int value) { 22 | if(cap==0)return ; 23 | 24 | if(ump.find(key)==ump.end()){ 25 | int sz=dll.size(); 26 | if(sz==cap){ 27 | int firstEleKey=(dll.begin())->first; 28 | dll.erase(dll.begin()); 29 | ump.erase(firstEleKey); 30 | } 31 | }else{ 32 | list::iterator nodeToDelete = ump[key]; 33 | dll.erase(nodeToDelete); 34 | } 35 | dll.push_back({key,value}); 36 | list::iterator it=dll.end(); 37 | it--; 38 | ump[key]=it; 39 | return; 40 | } 41 | -------------------------------------------------------------------------------- /Classic Algos/LU_DECOMPOSITION - ADVANCED.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iam-ss/Competitive-Programming-Problems/d07e00c8d81e9d4ad8fd90aebf0edb3e39af90a6/Classic Algos/LU_DECOMPOSITION - ADVANCED.exe -------------------------------------------------------------------------------- /Classic Algos/MO's ALGORITHM - DQUERY - SPOJ.cpp: -------------------------------------------------------------------------------- 1 | // http://www.spoj.com/problems/DQUERY/ 2 | 3 | #include 4 | #include 5 | using namespace std; 6 | #define fio ios_base::sync_with_stdio(false) 7 | 8 | #define ll long long int 9 | 10 | #define s(x) scanf("%lld",&x) 11 | #define s2(x,y) s(x)+s(y) 12 | #define s3(x,y,z) s(x)+s(y)+s(z) 13 | 14 | #define p(x) printf("%lld\n",x) 15 | #define p2(x,y) p(x)+p(y) 16 | #define p3(x,y,z) p(x)+p(y)+p(z) 17 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 18 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 19 | 20 | #define ff first 21 | #define ss second 22 | #define mp(x,y) make_pair(x,y) 23 | #define pll pair 24 | #define pb push_back 25 | 26 | ll inf = 1e18; 27 | ll mod = 1e9 + 7 ; 28 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 29 | 30 | /**************************CODE GOES HERE*********************************************/ 31 | vectorar; 32 | unordered_map resmp; 33 | vector> queries; 34 | ll freq[1000000 + 5]; 35 | ll n; 36 | ll sqrtn; 37 | 38 | bool comp(pair a,pair b){ 39 | if(a.first.first/sqrtn == b.first.first/sqrtn)return a.first.secondlf){ 85 | start--; 86 | freq[ar[start]]++; 87 | if(freq[ar[start]]==1)ct++; 88 | } 89 | 90 | while(startrt){ 103 | freq[ar[end]]--; 104 | if(freq[ar[end]]==0)ct--; 105 | end--; 106 | } 107 | resmp[ind]=ct; 108 | } 109 | F(i,1,q)p(resmp[i]); 110 | } 111 | return 0; 112 | } -------------------------------------------------------------------------------- /Classic Algos/TREE DIAMETER - MEDIUM - CODEFIGHTS.cpp: -------------------------------------------------------------------------------- 1 | // https://codefights.com/interview-practice/task/Sby2j4SHqDncwyQjh/description 2 | 3 | vector>adj; 4 | int maxDisVer; 5 | int maxDis; 6 | void treeDiaHelper(int node){ 7 | maxDis=0; 8 | maxDisVer=0; 9 | queue q; 10 | unordered_set vis; 11 | vis.insert(node); 12 | q.push(node); 13 | unordered_map disFromNode; 14 | disFromNode[node]=0; 15 | while(!q.empty()){ 16 | int par = q.front(); 17 | q.pop(); 18 | for(int i=0;imaxDis){ 25 | maxDis=disFromNode[child]; 26 | maxDisVer=child; 27 | } 28 | } 29 | } 30 | } 31 | 32 | 33 | int treeDiameter(int n, std::vector> tree) { 34 | if(n<=2)return n-1; 35 | adj.resize(n); 36 | for(int i=0;i 5 | // struct Tree { 6 | // Tree(const T &v) : value(v), left(nullptr), right(nullptr) {} 7 | // T value; 8 | // Tree *left; 9 | // Tree *right; 10 | // }; 11 | 12 | //------------------------------------ ITERATIVE SOLUTION ----------------------------------------------// 13 | 14 | #define nodeptr Tree* 15 | Tree * restoreBinaryTree(std::vector inorder, std::vector preorder) { 16 | if(preorder.size()==0)return nullptr; 17 | stack stk; 18 | int preptr=0; 19 | int inptr=0; 20 | nodeptr root; 21 | nodeptr cur=new Tree(preorder[0]); 22 | stk.push(cur); 23 | root=cur; 24 | preptr++; 25 | bool isRight=false; 26 | while(inptrvalue){ 28 | if(isRight){ 29 | cur->right = new Tree(preorder[preptr]); 30 | cur=cur->right; 31 | isRight=false; 32 | }else{ 33 | cur->left = new Tree(preorder[preptr]); 34 | cur=cur->left; 35 | } 36 | preptr++; 37 | stk.push(cur); 38 | }else{ 39 | isRight=true; 40 | cur=stk.top(); 41 | stk.pop(); 42 | inptr++; 43 | } 44 | } 45 | return root; 46 | } 47 | 48 | //------------------------------------------- RECURSI1VE SOLUTION -------------------------------------// 49 | 50 | // #define nodeptr Tree* 51 | // int indx=0; 52 | // nodeptr helper(vector &inorder,int lf,int rt,vector &preorder){ 53 | // int id = -1; 54 | // for(int i=lf;i<=rt;i++) 55 | // if(inorder[i]==preorder[indx]){ 56 | // id=i; 57 | // break; 58 | // } 59 | 60 | // if(id==-1)return nullptr; 61 | // nodeptr root = new Tree(preorder[indx]); 62 | // indx++; 63 | // root->left = helper(inorder,lf,id-1,preorder); 64 | // root->right = helper(inorder,id+1,rt,preorder); 65 | // return root; 66 | // } 67 | 68 | // Tree * restoreBinaryTree(std::vector inorder, std::vector preorder) { 69 | // return helper(inorder,0,inorder.size()-1,preorder); 70 | // } 71 | -------------------------------------------------------------------------------- /Classic Algos/ZAlgorithm.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | #define fio ios_base::sync_with_stdio(false) 5 | 6 | #define ll long long int 7 | 8 | #define s(x) scanf("%lld",&x) 9 | #define s2(x,y) s(x)+s(y) 10 | #define s3(x,y,z) s(x)+s(y)+s(z) 11 | 12 | #define p(x) printf("%lld\n",x) 13 | #define p2(x,y) p(x)+p(y) 14 | #define p3(x,y,z) p(x)+p(y)+p(z) 15 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 16 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 17 | 18 | #define ff first 19 | #define ss second 20 | #define mp(x,y) make_pair(x,y) 21 | #define pll pair 22 | #define pb push_back 23 | 24 | ll inf = 1e18; 25 | ll mod = 1e9 + 7 ; 26 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 27 | 28 | /**************************CODE GOES HERE*********************************************/ 29 | 30 | vector computeZArray(string str){ 31 | int lf=0; 32 | int rt=0; 33 | vector Z; 34 | int sz=str.size(); 35 | Z.resize(sz); 36 | Z[0]=0; 37 | for(int i=1;irt){ 39 | lf=rt=i; 40 | while(rt ZAlgo(string str,string pat){ 57 | int n=str.size(); 58 | int m=pat.size(); 59 | vector ret; 60 | if(m>n)return ret; 61 | str=pat+"$"+str; 62 | 63 | vector ZArray = computeZArray(str); 64 | for(int i=m+1;i>str>>pat; 79 | vector matchedIndicesList = ZAlgo(str,pat); 80 | cout<<"The string contains the pattern at the following indices(0-based): \n"; 81 | int sz=matchedIndicesList.size(); 82 | for(int i=0;i 2 | #include 3 | using namespace std; 4 | #define fio ios_base::sync_with_stdio(false) 5 | 6 | #define ll long long int 7 | 8 | #define s(x) scanf("%lld",&x) 9 | #define s2(x,y) s(x)+s(y) 10 | #define s3(x,y,z) s(x)+s(y)+s(z) 11 | 12 | #define p(x) printf("%lld\n",x) 13 | #define p2(x,y) p(x)+p(y) 14 | #define p3(x,y,z) p(x)+p(y)+p(z) 15 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 16 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 17 | 18 | #define ff first 19 | #define ss second 20 | #define mp(x,y) make_pair(x,y) 21 | #define pll pair 22 | #define pb push_back 23 | 24 | ll inf = 1e18; 25 | ll mod = 1e9 + 7 ; 26 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 27 | 28 | /**************************CODE GOES HERE*********************************************/ 29 | 30 | 31 | int steps(int node, int moves, int des){ 32 | if(node==des or node==-des)return moves; 33 | if(abs(node)>abs(des))return INT_MAX; 34 | 35 | int pos = steps(node+(moves+1),moves+1,des); 36 | int neg = steps(node-(moves+1),moves+1,des); 37 | return min(pos,neg); 38 | } 39 | 40 | int main() 41 | { 42 | freopen("input.txt","r",stdin); 43 | // freopen("output.txt","w",stdout); 44 | ll t=1; 45 | s(t); 46 | while(t--){ 47 | ll des; 48 | s(des); 49 | cout<>q; 56 | // q.push({0,0}); 57 | // while(!q.empty()){ 58 | // pair pii = q.front(); 59 | // q.pop(); 60 | // int num=pii.first; 61 | // int moves=pii.second; 62 | // if(num==des or num==-des){ 63 | // cout< 3 | #include 4 | using namespace std; 5 | #define fio ios_base::sync_with_stdio(false) 6 | 7 | #define ll long long int 8 | 9 | #define s(x) scanf("%lld",&x) 10 | #define s2(x,y) s(x)+s(y) 11 | #define s3(x,y,z) s(x)+s(y)+s(z) 12 | 13 | #define p(x) printf("%lld\n",x) 14 | #define p2(x,y) p(x)+p(y) 15 | #define p3(x,y,z) p(x)+p(y)+p(z) 16 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 17 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 18 | 19 | #define ff first 20 | #define ss second 21 | #define mp(x,y) make_pair(x,y) 22 | #define pll pair 23 | #define pb push_back 24 | 25 | ll inf = 1e18; 26 | ll mod = 1e9 + 7 ; 27 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 28 | 29 | int main() 30 | { 31 | // freopen("input.txt","r",stdin); 32 | // freopen("output.txt","w",stdout); 33 | ll t=1; 34 | s(t); 35 | while(t--){ 36 | ll n; 37 | s(n); 38 | double ans=(double)n; 39 | double sum=0.0; 40 | for(ll i=1;i<=n;i++){ 41 | sum+= (double)(1/(double)i) ; 42 | } 43 | ans=ans*sum; 44 | printf("%.2lf\n",ans); 45 | } 46 | } 47 | 48 | // Refer this for editorial https://www.youtube.com/watch?v=3mu47FWEuqA 49 | -------------------------------------------------------------------------------- /Crossing Blocks Game - HACKERBLOCKS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | #define fio ios_base::sync_with_stdio(false) 5 | 6 | #define ll long long int 7 | 8 | #define s(x) scanf("%lld",&x) 9 | #define s2(x,y) s(x)+s(y) 10 | #define s3(x,y,z) s(x)+s(y)+s(z) 11 | 12 | #define p(x) printf("%lld\n",x) 13 | #define p2(x,y) p(x)+p(y) 14 | #define p3(x,y,z) p(x)+p(y)+p(z) 15 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 16 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 17 | 18 | #define ff first 19 | #define ss second 20 | #define mp(x,y) make_pair(x,y) 21 | #define pll pair 22 | #define pb push_back 23 | 24 | ll inf = 1e18; 25 | ll mod = 1e9 + 7 ; 26 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 27 | 28 | vector fibo(101,-1); 29 | ll dp[102][102][2]; 30 | 31 | ll fib(ll n){ 32 | if(n<0)return 0ll; 33 | if(n<=1)return 1ll; 34 | if(fibo[n]!=-1)return fibo[n]; 35 | return fibo[n]=fib(n-1)+fib(n-2); 36 | } 37 | 38 | ll solve(ll lf,ll rt,vector &ar,ll player){ 39 | if(lf==rt){ 40 | if(player == 1) return fib(ar[lf]); 41 | else return 0LL; 42 | } 43 | // if(dp[lf][rt][player]!=-1)return dp[lf][rt][player]; 44 | 45 | if(player==1){ 46 | ll choice1 = fib(ar[lf])+solve(lf+1,rt,ar,0); 47 | ll choice2 = fib(ar[rt])+solve(lf,rt-1,ar,0); 48 | return dp[lf][rt][player] = max(choice1,choice2); 49 | } 50 | else { 51 | ll choice1 = solve(lf+1,rt,ar,1); 52 | ll choice2 = solve(lf,rt-1,ar,1); 53 | return dp[lf][rt][player] = min(choice1,choice2); 54 | } 55 | } 56 | 57 | int main() 58 | { 59 | freopen("input.txt","r",stdin); 60 | // freopen("output.txt","w",stdout); 61 | ll t=1; 62 | s(t); 63 | while(t--){ 64 | memset(dp,-1,sizeof(dp)); 65 | ll n; 66 | s(n); 67 | ll sum=0ll; 68 | vectorar(n+1); 69 | F(i,1,n){ 70 | cin>>ar[i]; 71 | sum+=fib(ar[i]); 72 | } 73 | ll player1 = solve(1,n,ar,1); 74 | ll player2 = sum-player1; 75 | cout<player2)cout<<"First\n"; 78 | else cout<<"Second\n"; 79 | } 80 | } -------------------------------------------------------------------------------- /DANCE BATTLE - ADHOC(MEDIUM) - KICKSTART.cpp: -------------------------------------------------------------------------------- 1 | // https://code.google.com/codejam/contest/7254486/dashboard#s=p1&a=0 2 | 3 | #include 4 | #include 5 | using namespace std; 6 | #define fio ios_base::sync_with_stdio(false) 7 | 8 | #define ll long long int 9 | 10 | #define s(x) scanf("%lld",&x) 11 | #define s2(x,y) s(x)+s(y) 12 | #define s3(x,y,z) s(x)+s(y)+s(z) 13 | 14 | #define p(x) printf("%lld\n",x) 15 | #define p2(x,y) p(x)+p(y) 16 | #define p3(x,y,z) p(x)+p(y)+p(z) 17 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 18 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 19 | 20 | #define ff first 21 | #define ss second 22 | #define mp(x,y) make_pair(x,y) 23 | #define pll pair 24 | #define pb push_back 25 | 26 | ll inf = 1e18; 27 | ll mod = 1e9 + 7 ; 28 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 29 | 30 | /****************************************************************************/ 31 | 32 | int main() 33 | { 34 | freopen("input.txt","r",stdin); 35 | freopen("output.txt","w",stdout); 36 | ll t=1; 37 | ll tc=1; 38 | s(t); 39 | while(t--){ 40 | ll E,n; 41 | s2(E,n); 42 | vector dance(n); 43 | F(i,0,n-1)cin>>dance[i]; 44 | sort(dance.begin(),dance.end()); 45 | ll lo=0; 46 | ll hi=n-1; 47 | ll hon=0; 48 | ll ene=E; 49 | ll maxHon=0; 50 | while(lo<=hi){ 51 | if(ene-dance[lo]>0){ene-=dance[lo++]; hon++; } 52 | else if(hon>0){ ene+=dance[hi--]; hon--; } 53 | else break; 54 | maxHon=max(maxHon,hon); 55 | } 56 | cout<<"Case #"< 4 | #include 5 | using namespace std; 6 | #define fio ios_base::sync_with_stdio(false) 7 | 8 | #define ll long long int 9 | 10 | #define s(x) scanf("%lld",&x) 11 | #define s2(x,y) s(x)+s(y) 12 | #define s3(x,y,z) s(x)+s(y)+s(z) 13 | 14 | #define p(x) printf("%lld\n",x) 15 | #define p2(x,y) p(x)+p(y) 16 | #define p3(x,y,z) p(x)+p(y)+p(z) 17 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 18 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 19 | 20 | #define ff first 21 | #define ss second 22 | #define mp(x,y) make_pair(x,y) 23 | #define pll pair 24 | #define pb push_back 25 | 26 | ll inf = 1e18; 27 | ll mod = 1e9 + 7 ; 28 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 29 | 30 | /****************************************************************************/ 31 | ll n,m,x,y; 32 | 33 | // Returns if it is possible to give 100% discount to 'stud' number of students 34 | bool solve(ll stud){ 35 | return (stud*x <= m+(n-stud)*y); 36 | } 37 | 38 | int main() 39 | { 40 | // freopen("input.txt","r",stdin); 41 | // freopen("output.txt","w",stdout); 42 | ll t=1; 43 | // s(t); 44 | while(t--){ 45 | s3(n,m,x); 46 | s(y); 47 | ll lo=0; 48 | ll hi=n; 49 | ll ans=0; 50 | // Just do the binary search over the number of students 51 | while(lo<=hi){ 52 | ll mid=lo+hi; 53 | mid>>=1; 54 | if(solve(mid)){ lo=mid+1; ans=mid; } 55 | else hi=mid-1; 56 | } 57 | p(ans); 58 | } 59 | return 0; 60 | } -------------------------------------------------------------------------------- /DP - MEDIUM - ADOBE - COST OF MERGING STRING.cpp: -------------------------------------------------------------------------------- 1 | // https://www.hackerrank.com/contests/adobe-codiva/challenges/cost-of-merging-strings/problem 2 | #include 3 | #include 4 | using namespace std; 5 | #define fio ios_base::sync_with_stdio(false) 6 | 7 | #define ll int 8 | 9 | #define s(x) scanf("%d",&x) 10 | #define s2(x,y) s(x)+s(y) 11 | #define s3(x,y,z) s(x)+s(y)+s(z) 12 | 13 | #define p(x) printf("%d\n",x) 14 | #define p2(x,y) p(x)+p(y) 15 | #define p3(x,y,z) p(x)+p(y)+p(z) 16 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 17 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 18 | 19 | #define ff first 20 | #define ss second 21 | #define mp(x,y) make_pair(x,y) 22 | #define pll pair 23 | #define pb push_back 24 | 25 | ll inf = 1e9; 26 | ll mod = 1e9 + 7 ; 27 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 28 | 29 | /****************************************************************************/ 30 | 31 | ll dp[400][400][400]; 32 | 33 | int main() 34 | { 35 | // freopen("input.txt","r",stdin); 36 | // freopen("output.txt","w",stdout); 37 | ll t=1; 38 | // s(t); 39 | while(t--){ 40 | ll n,m,k; 41 | s3(n,m,k); 42 | string str1,str2; 43 | cin>>str1>>str2; 44 | str1="."+str1; 45 | str2="."+str2; 46 | 47 | if(k>min(n,m)){cout<<"-1\n"; break; } 48 | 49 | F(i,0,n){ 50 | F(j,0,m){ 51 | F(len,0,k){ 52 | dp[i][j][len]=inf; 53 | if(len==0)dp[i][j][len]=0; 54 | else if(i==0 or j==0)continue; 55 | else{ 56 | dp[i][j][len]=dp[i-1][j-1][len-1]+((int)(str1[i]-'a')^(int)(str2[j]-'a')); 57 | dp[i][j][len]=min(dp[i][j][len],min(dp[i-1][j][len],dp[i][j-1][len])); 58 | } 59 | } 60 | } 61 | } 62 | cout< 4 | #include 5 | using namespace std; 6 | #define fio ios_base::sync_with_stdio(false) 7 | 8 | #define ll long long int 9 | 10 | #define s(x) scanf("%lld",&x) 11 | #define s2(x,y) s(x)+s(y) 12 | #define s3(x,y,z) s(x)+s(y)+s(z) 13 | 14 | #define p(x) printf("%lld\n",x) 15 | #define p2(x,y) p(x)+p(y) 16 | #define p3(x,y,z) p(x)+p(y)+p(z) 17 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 18 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 19 | 20 | #define ff first 21 | #define ss second 22 | #define mp(x,y) make_pair(x,y) 23 | #define pll pair 24 | #define pb push_back 25 | 26 | ll inf = 1e18; 27 | ll mod = 1e9 + 7 ; 28 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 29 | 30 | /****************************************************************************/ 31 | 32 | ll dp[5000][600]; 33 | ll mo; 34 | 35 | ll solve(ll sum,ll ind){ 36 | 37 | if(sum<0)return 0ll; 38 | 39 | if(ind==0){ 40 | if(sum==0)return 1ll; 41 | return 0ll; 42 | } 43 | 44 | if(dp[sum][ind]!=-1)return dp[sum][ind]; 45 | ll numOfCards=0ll; 46 | for(ll i=0;i<10;i++){ 47 | ll num=i; 48 | if(num%2 ==0){ 49 | num=2*num; 50 | if(num>9)num-=9; 51 | } 52 | 53 | numOfCards+=solve(sum-num,ind-1); 54 | numOfCards%=mod; 55 | } 56 | return dp[sum][ind]=numOfCards; 57 | } 58 | 59 | int main() 60 | { 61 | freopen("input.txt","r",stdin); 62 | // freopen("output.txt","w",stdout); 63 | ll t=1; 64 | s(t); 65 | memset(dp,-1,sizeof(dp)); 66 | while(t--){ 67 | 68 | ll x; 69 | s2(x,mo); 70 | ll ans=0ll; 71 | for(ll sum=0ll;sum<=x*9;sum+=mo){ 72 | ans+=solve(sum,x); 73 | ans%=mod; 74 | } 75 | p(ans); 76 | } 77 | return 0; 78 | } -------------------------------------------------------------------------------- /DP Medium codemonk v2.cpp: -------------------------------------------------------------------------------- 1 | // https://www.hackerearth.com/problem/algorithm/vaishu-and-queries/ 2 | 3 | #include 4 | #include 5 | using namespace std; 6 | #define fio ios_base::sync_with_stdio(false) 7 | 8 | #define ll long long int 9 | 10 | #define s(x) scanf("%lld",&x) 11 | #define s2(x,y) s(x)+s(y) 12 | #define s3(x,y,z) s(x)+s(y)+s(z) 13 | 14 | #define p(x) printf("%lld\n",x) 15 | #define p2(x,y) p(x)+p(y) 16 | #define p3(x,y,z) p(x)+p(y)+p(z) 17 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 18 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 19 | 20 | #define ff first 21 | #define ss second 22 | #define mp(x,y) make_pair(x,y) 23 | #define pll pair 24 | #define pb push_back 25 | 26 | ll inf = 1e18; 27 | ll mod = 1e9 + 7 ; 28 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 29 | 30 | ll dp[101][101][101]={0}; 31 | 32 | int main() 33 | { 34 | // freopen("input.txt","r",stdin); 35 | // freopen("output.txt","w",stdout); 36 | ll t=1; 37 | //s(t); 38 | while(t--){ 39 | ll n; 40 | s(n); 41 | ll rd[201]={0}; 42 | ll bl[201]={0}; 43 | 44 | F(i,1,n){ 45 | string str; 46 | cin>>str; 47 | for(char ch:str){ 48 | if(ch=='R')rd[i]++; 49 | else bl[i]++; 50 | } 51 | } 52 | 53 | for(ll i=1;i<=n;i++){ 54 | for(ll x=0;x<=100;x++){ 55 | for(ll y=0;y<=100;y++){ 56 | if(i==1){ 57 | if(x>=rd[i] and y>=bl[i])dp[i][x][y]=1; 58 | }else{ 59 | dp[i][x][y]=dp[i-1][x][y]; 60 | if(x>=rd[i] and y>=bl[i])dp[i][x][y]=max(dp[i][x][y],1+dp[i-1][x-rd[i]][y-bl[i]]); 61 | } 62 | } 63 | } 64 | } 65 | 66 | ll q; 67 | s(q); 68 | while(q--){ 69 | ll x,y; 70 | cin>>x>>y; 71 | cout< 4 | #include 5 | using namespace std; 6 | #define fio ios_base::sync_with_stdio(false) 7 | 8 | #define ll long long int 9 | 10 | #define s(x) scanf("%lld",&x) 11 | #define s2(x,y) s(x)+s(y) 12 | #define s3(x,y,z) s(x)+s(y)+s(z) 13 | 14 | #define p(x) printf("%lld\n",x) 15 | #define p2(x,y) p(x)+p(y) 16 | #define p3(x,y,z) p(x)+p(y)+p(z) 17 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 18 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 19 | 20 | #define ff first 21 | #define ss second 22 | #define mp(x,y) make_pair(x,y) 23 | #define pll pair 24 | #define pb push_back 25 | 26 | ll inf = 1e18; 27 | ll mod = 1e9 + 7 ; 28 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 29 | 30 | /****************************************************************************/ 31 | 32 | vector dim; 33 | ll n; 34 | 35 | ll dp[105][105]; 36 | 37 | ll solve(ll lf,ll rt){ 38 | if(dp[lf][rt]!=-1)return dp[lf][rt]; 39 | ll ans=(ll)INT_MAX; 40 | F(i,lf+1,rt-1){ 41 | ans=min(ans,solve(lf,i)+solve(i,rt)+dim[lf]*dim[i]*dim[rt]); 42 | } 43 | if(ans==INT_MAX) ans=0ll; 44 | return dp[lf][rt]=ans; 45 | } 46 | 47 | 48 | int main() 49 | { 50 | freopen("input.txt","r",stdin); 51 | // freopen("output.txt","w",stdout); 52 | ll t=1; 53 | s(t); 54 | while(t--){ 55 | memset(dp,-1,sizeof(dp)); 56 | s(n); 57 | dim.resize(n+5); 58 | F(i,1,n)s(dim[i]); 59 | p(solve(1,n)); 60 | 61 | } 62 | return 0; 63 | } -------------------------------------------------------------------------------- /DSUBasic.cpp: -------------------------------------------------------------------------------- 1 | // https://www.hackerrank.com/challenges/journey-to-the-moon 2 | 3 | #include 4 | #include 5 | using namespace std; 6 | #define fio ios_base::sync_with_stdio(false) 7 | 8 | #define ll long long int 9 | 10 | #define s(x) scanf("%lld",&x) 11 | #define s2(x,y) s(x)+s(y) 12 | #define s3(x,y,z) s(x)+s(y)+s(z) 13 | 14 | #define p(x) printf("%lld\n",x) 15 | #define p2(x,y) p(x)+p(y) 16 | #define p3(x,y,z) p(x)+p(y)+p(z) 17 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 18 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 19 | 20 | #define ff first 21 | #define ss second 22 | #define mp(x,y) make_pair(x,y) 23 | #define pll pair 24 | #define pb push_back 25 | 26 | ll inf = 1e18; 27 | ll mod = 1e9 + 7 ; 28 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 29 | 30 | vector par; 31 | vector lev; 32 | 33 | ll findPar(ll ele){ 34 | if(par[ele]==ele)return ele; 35 | return par[ele]=findPar(par[ele]); 36 | } 37 | 38 | void makeUnion(ll a, ll b){ 39 | ll parA = findPar(a); 40 | ll parB = findPar(b); 41 | 42 | if(parA==parB)return; 43 | if(lev[parA]>=lev[parB]){ 44 | par[parB]=parA; 45 | if(lev[parA]==lev[parB])lev[parA]++; 46 | return; 47 | } 48 | par[parA]=parB; 49 | } 50 | 51 | int main() 52 | { 53 | // freopen("input.txt","r",stdin); 54 | // freopen("output.txt","w",stdout); 55 | ll t=1; 56 | //s(t); 57 | while(t--){ 58 | ll n,p; 59 | s2(n,p); 60 | 61 | par = vector(n+1); 62 | for(ll i=0;i(n+1,0); 64 | 65 | F(i,1,p){ 66 | ll u,v; 67 | s2(u,v); 68 | makeUnion(u,v); 69 | } 70 | 71 | F(i,0,n-1)findPar(i); 72 | 73 | // F(i,0,n-1)cout<<"par[ "<= (ll)(b); i--) 17 | 18 | #define ff first 19 | #define ss second 20 | #define mp(x,y) make_pair(x,y) 21 | #define pll pair 22 | #define pb push_back 23 | 24 | ll inf = 1e18; 25 | ll mod = 1e9 + 7 ; 26 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 27 | 28 | vector res; 29 | 30 | void solve(string ar,int id){ 31 | 32 | // Base Condition: Just passed the string ar into the res vector. 33 | if(id==ar.size()){ 34 | res.push_back(ar); 35 | return; 36 | } 37 | 38 | for(int i=id;i permute(string &A) { 58 | res.clear(); 59 | // Sort the given string, then only the resultant permutations will be in 60 | // a lexicographic order 61 | sort(A.begin(), A.end()); 62 | 63 | // Call this recursive function with first parameter as string and second parameter 64 | // as the index to be focused. 65 | solve(A,0); 66 | 67 | // Now the res contains all the distinct permutations for the string A. Return it. 68 | return res; 69 | } 70 | 71 | 72 | int main() 73 | { 74 | // freopen("input.txt","r",stdin); 75 | // freopen("output.txt","w",stdout); 76 | ll t=1; 77 | //s(t); 78 | while(t--){ 79 | string str; 80 | cin>>str; 81 | vector res = permute(str); 82 | for(ll i=0;i 4 | #include 5 | using namespace std; 6 | #define fio ios_base::sync_with_stdio(false) 7 | 8 | #define ll long long int 9 | 10 | #define s(x) scanf("%lld",&x) 11 | #define s2(x,y) s(x)+s(y) 12 | #define s3(x,y,z) s(x)+s(y)+s(z) 13 | 14 | #define p(x) printf("%lld\n",x) 15 | #define p2(x,y) p(x)+p(y) 16 | #define p3(x,y,z) p(x)+p(y)+p(z) 17 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 18 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 19 | 20 | #define ff first 21 | #define ss second 22 | #define mp(x,y) make_pair(x,y) 23 | #define pll pair 24 | #define pb push_back 25 | 26 | ll inf = 1e18; 27 | ll mod = 1e9 + 7 ; 28 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 29 | 30 | /****************************************************************************/ 31 | 32 | ll dp[20000]; 33 | ll solve(ll n){ 34 | if(n<0)return (ll)INT_MAX; 35 | if(n==0)return 0; 36 | if(dp[n]!=-1)return dp[n]; 37 | ll up = floor(sqrt(n)); 38 | ll ans = (ll)INT_MAX; 39 | RF(i,up,1)ans=min(ans,1+solve(n-i*i)); 40 | return dp[n]=ans; 41 | } 42 | 43 | int main() 44 | { 45 | freopen("input.txt","r",stdin); 46 | freopen("output.txt","w",stdout); 47 | ll t=1,tc=1; 48 | s(t); 49 | memset(dp,-1,sizeof(dp)); 50 | while(t--){ 51 | ll n; 52 | s(n); 53 | ll ans = solve(n); 54 | cout<<"Case #"< 12 | #include 13 | using namespace std; 14 | #define fio ios_base::sync_with_stdio(false) 15 | 16 | #define ll long long int 17 | 18 | #define s(x) scanf("%lld",&x) 19 | #define s2(x,y) s(x)+s(y) 20 | #define s3(x,y,z) s(x)+s(y)+s(z) 21 | 22 | #define p(x) printf("%lld\n",x) 23 | #define p2(x,y) p(x)+p(y) 24 | #define p3(x,y,z) p(x)+p(y)+p(z) 25 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 26 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 27 | 28 | #define ff first 29 | #define ss second 30 | #define mp(x,y) make_pair(x,y) 31 | #define pll pair 32 | #define pb push_back 33 | 34 | ll inf = 1e18; 35 | ll mod = 1e9 + 7 ; 36 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 37 | 38 | int main() 39 | { 40 | // freopen("input.txt","r",stdin); 41 | // freopen("output.txt","w",stdout); 42 | ll t=1; 43 | s(t); 44 | while(t--){ 45 | ll n; 46 | s(n); 47 | double ans=(double)n; 48 | double sum=0.0; 49 | for(ll i=1;i<=n;i++){ 50 | sum+= (double)(1/(double)i) ; 51 | } 52 | ans=ans*sum; 53 | printf("%.2lf\n",ans); 54 | } 55 | } 56 | 57 | ``` -------------------------------------------------------------------------------- /Editorials/EDITORIAL - DISCOUNT AT CODING BLOCKS.md: -------------------------------------------------------------------------------- 1 | ## DISCOUNT AT CODING BLOCKS 2 | ##### Problem Link : [Discount At Coding Blocks](https://hack.codingblocks.com/contests/c/1001/1226) 3 | 4 | First let's check if we can give discount to a fixed number of students `A`. For this we need `A * X` discount coupons. Initially we have `M` discount coupons, but we can also ask the other N−A students to pay for additional `(N-A) * Y` coupons. So we should check if `M+(N−A) * Y ≥ A* X`. 5 | 6 | We can try out all the values of `A` and print the largest one that is possible. As there are `N` different values to check this algorithm runs in `O(N)`. 7 | 8 | Now notice that if we can discount `A` students then we are sure we can also give discount to any number of students smaller than `A`. This allows us to binary search the answer, using the previous formula. 9 | 10 | The binary search works in `O(logN)`. 11 | 12 | _**Time Complexity:** O(log N)_ 13 | 14 | Read below commented code for implementation details. 15 | ```C++ 16 | #include 17 | #include 18 | using namespace std; 19 | #define fio ios_base::sync_with_stdio(false) 20 | 21 | #define ll long long int 22 | 23 | #define s(x) scanf("%lld",&x) 24 | #define s2(x,y) s(x)+s(y) 25 | #define s3(x,y,z) s(x)+s(y)+s(z) 26 | 27 | #define p(x) printf("%lld\n",x) 28 | #define p2(x,y) p(x)+p(y) 29 | #define p3(x,y,z) p(x)+p(y)+p(z) 30 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 31 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 32 | 33 | #define ff first 34 | #define ss second 35 | #define mp(x,y) make_pair(x,y) 36 | #define pll pair 37 | #define pb push_back 38 | 39 | ll inf = 1e18; 40 | ll mod = 1e9 + 7 ; 41 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 42 | 43 | /****************************************************************************/ 44 | ll n,m,x,y; 45 | 46 | // Returns if it is possible to give 100% discount to 'stud' number of students 47 | bool solve(ll stud){ 48 | return (stud*x <= m+(n-stud)*y); 49 | } 50 | 51 | int main() 52 | { 53 | // freopen("input.txt","r",stdin); 54 | // freopen("output.txt","w",stdout); 55 | ll t=1; 56 | // s(t); 57 | while(t--){ 58 | s3(n,m,x); 59 | s(y); 60 | ll lo=0; 61 | ll hi=n; 62 | ll ans=0; 63 | // Just do the binary search over the number of students 64 | while(lo<=hi){ 65 | ll mid=lo+hi; 66 | mid>>=1; 67 | if(solve(mid)){ lo=mid+1; ans=mid; } 68 | else hi=mid-1; 69 | } 70 | p(ans); 71 | } 72 | return 0; 73 | } 74 | ``` -------------------------------------------------------------------------------- /Editorials/EDITORIAL - HOLI .md: -------------------------------------------------------------------------------- 1 | ## HOLI 2 | ##### Problem Link : [HOLI](https://hack.codingblocks.com/contests/c/1001/771) 3 | 4 | The problem can be solved if viewed from the `edges` point of view, rather than `nodes` point of view.One can easily observe that to obtain the maximum distance travelled by all the visitors, each edge should be travelled by maximum possible number of visitors. Since the maximum visitors which can travel the edge is minimum of the total number of nodes to the left and right of the edge, hence we compute it using DFS. If number of visitors one side of the edge is a,then the number of visitors on the other side of the edge automatically becomes n-a. 5 | 6 | For further analysis, refer [this](http://codeforces.com/blog/entry/8149?locale=en). 7 | _**Time Complexity:** O(E)_ where E = Number of edges in the given graph(tree). 8 | 9 | Read below commented code for implementation details. 10 | ```C++ 11 | 12 | #include 13 | #include 14 | using namespace std; 15 | #define fio ios_base::sync_with_stdio(false) 16 | 17 | #define ll long long int 18 | 19 | #define s(x) scanf("%lld",&x) 20 | #define s2(x,y) s(x)+s(y) 21 | #define s3(x,y,z) s(x)+s(y)+s(z) 22 | 23 | #define p(x) printf("%lld\n",x) 24 | #define p2(x,y) p(x)+p(y) 25 | #define p3(x,y,z) p(x)+p(y)+p(z) 26 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 27 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 28 | 29 | #define ff first 30 | #define ss second 31 | #define mp(x,y) make_pair(x,y) 32 | #define pll pair 33 | #define pb push_back 34 | 35 | ll inf = 1e18; 36 | ll mod = 1e9 + 7 ; 37 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 38 | 39 | 40 | 41 | vectoradj[100005]; 42 | 43 | // ret will contain the total travel distance (travelled one-sidedly) by the visitors , 44 | // that's why, at the end we are multiplying by 2, as people from both sides are crossing 45 | // a particular edge. 46 | ll ret=0; 47 | ll n; 48 | 49 | ll dfs(ll par, ll u){ 50 | ll ans=1; 51 | for(ll i=0;i 13 | // #include 14 | using namespace std; 15 | #define fio ios_base::sync_with_stdio(false) 16 | 17 | #define ll long long int 18 | 19 | #define s(x) scanf("%lld",&x) 20 | #define s2(x,y) s(x)+s(y) 21 | #define s3(x,y,z) s(x)+s(y)+s(z) 22 | 23 | #define p(x) printf("%lld\n",x) 24 | #define p2(x,y) p(x)+p(y) 25 | #define p3(x,y,z) p(x)+p(y)+p(z) 26 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 27 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 28 | 29 | #define ff first 30 | #define ss second 31 | #define mp(x,y) make_pair(x,y) 32 | #define pll pair 33 | #define pb push_back 34 | 35 | ll inf = 1e18; 36 | ll mod = 1e9 + 7 ; 37 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 38 | ll x,y,n,w; 39 | double p1,p2; 40 | double dp[1005][1005]; 41 | 42 | double solve(ll n, ll w){ 43 | // If w is less than 0 , then we need not shoot any bird and we have already won, so return 1.0 44 | if(w<=0)return 1.0; 45 | 46 | // If w is not less than or equal to 0 and we also don't have any coins left, we can't win the match, so return 0.0 47 | if(n<=0)return 0.0; 48 | 49 | if(dp[n][w]>=0.0)return dp[n][w]; 50 | 51 | // Either we kill the bird 1 with p1 percentage and try to obtain the remaining w-x points with n-1 coins 52 | // or we miss the shot with 1-p1 percentage and try to obtain the remaining w points with n-1 coins. 53 | double c1 = p1*solve(n-1,w-x) + (1-p1)*solve(n-1,w); 54 | 55 | // Same as above 56 | double c2 = p2*solve(n-1,w-y) + (1-p2)*solve(n-1,w); 57 | 58 | // Return the maximum of the two 59 | return dp[n][w]=max(c1,c2); 60 | } 61 | 62 | int main() 63 | { 64 | // freopen("input.txt","r",stdin); 65 | // freopen("output.txt","w",stdout); 66 | ll t=1; 67 | s(t); 68 | while(t--){ 69 | ll p1,p2; 70 | cin>>x>>y>>n>>w>>p1>>p2; 71 | ::p1=(double)p1/100.0; 72 | ::p2=(double)p2/100.0; 73 | memset(dp,-1.0,sizeof(dp)); 74 | double ans = solve(n,w)*100.0; 75 | printf("%.6f\n",ans); 76 | } 77 | } 78 | 79 | ``` -------------------------------------------------------------------------------- /Editorials/EDITORIAL - MATH DAY.md: -------------------------------------------------------------------------------- 1 | ## MATH DAY 2 | ##### Problem Link : [Math Day](https://hack.codingblocks.com/admin/preview/1251) 3 | 4 | We can take advantage of a basic property of exponents: ab * c can be rewritten as abc. 5 | 6 | So, we can write AN! as A123.... And, since multiplication modulo P is associative, we can maintain our answer modulo P at all times. 7 | 8 | We will be using [Modular exponentiation](https://www.geeksforgeeks.org/modular-exponentiation-power-in-modular-arithmetic/) to compute exponentiation in log E time where E is the power to be computed. 9 | 10 | _**Time Complexity:** O(N log N)_ where N is described in the question 11 | 12 | Read below commented code for implementation details. 13 | ```C++ 14 | #include 15 | #include 16 | using namespace std; 17 | #define fio ios_base::sync_with_stdio(false) 18 | 19 | #define ll long long int 20 | 21 | #define s(x) scanf("%lld",&x) 22 | #define s2(x,y) s(x)+s(y) 23 | #define s3(x,y,z) s(x)+s(y)+s(z) 24 | 25 | #define p(x) printf("%lld\n",x) 26 | #define p2(x,y) p(x)+p(y) 27 | #define p3(x,y,z) p(x)+p(y)+p(z) 28 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 29 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 30 | 31 | #define ff first 32 | #define ss second 33 | #define mp(x,y) make_pair(x,y) 34 | #define pll pair 35 | #define pb push_back 36 | 37 | ll inf = 1e18; 38 | ll mod = 1e9 + 7 ; 39 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 40 | 41 | /****************************************************************************/ 42 | 43 | // Modular Exponentiation 44 | ll modExp(ll a,ll e,ll mod){ 45 | if(e==0)return 1ll; 46 | if(a==0) return 0ll; 47 | 48 | ll pro=1; 49 | while(e>0){ 50 | if(e&1)pro=(pro*a)%mod; 51 | a=(a*a)%mod; 52 | e=e/2; 53 | } 54 | return pro; 55 | } 56 | 57 | int main() 58 | { 59 | //freopen("input.txt","r",stdin); 60 | //freopen("output1.txt","w",stdout); 61 | ll t=1; 62 | s(t); 63 | while(t--){ 64 | ll a,n,pp; 65 | s3(a,n,pp); 66 | ll ans = a; 67 | F(i,1,n)ans=modExp(ans,i,pp); 68 | cout< 11 | #include 12 | using namespace std; 13 | #define fio ios_base::sync_with_stdio(false) 14 | 15 | #define ll long long int 16 | 17 | #define s(x) scanf("%lld",&x) 18 | #define s2(x,y) s(x)+s(y) 19 | #define s3(x,y,z) s(x)+s(y)+s(z) 20 | 21 | #define p(x) printf("%lld\n",x) 22 | #define p2(x,y) p(x)+p(y) 23 | #define p3(x,y,z) p(x)+p(y)+p(z) 24 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 25 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 26 | 27 | #define ff first 28 | #define ss second 29 | #define mp(x,y) make_pair(x,y) 30 | #define pll pair 31 | #define pb push_back 32 | 33 | ll inf = 1e18; 34 | ll mod = 1e9 + 7 ; 35 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 36 | 37 | int main() 38 | { 39 | freopen("input.txt","r",stdin); 40 | // freopen("output.txt","w",stdout); 41 | ll t=1; 42 | // s(t); 43 | while(t--){ 44 | ll r,c; 45 | s2(r,c); 46 | 47 | // Accept the input in the matrix 48 | vector>mat; 49 | mat.resize(r); 50 | for(ll i=0;i>mat[i][j]; 54 | } 55 | } 56 | 57 | // Accept the input 58 | ll num; 59 | cin>>num; 60 | 61 | // Initially begin searching from right top corner 62 | ll row,col; 63 | row=0; 64 | col=c-1; 65 | 66 | // This flag indicates if the number has been found or not 67 | bool fg=0; 68 | 69 | // This loop will run untill we remain in the bounds of the matrix 70 | while(row=0){ 71 | // If the number is found, set the flag and break 72 | if(mat[row][col]==num){ 73 | fg=1; 74 | break; 75 | } 76 | // If the matrix element is greater than number, we must check in the previous column as that 77 | // will contain lower numbers. 78 | if(mat[row][col]>num)col--; 79 | else row++; // Else check in the next row which will contain higher numbers. 80 | } 81 | 82 | if(fg)cout<<"1"< 13 | // #include 14 | using namespace std; 15 | #define fio ios_base::sync_with_stdio(false) 16 | 17 | #define ll long long int 18 | 19 | #define s(x) scanf("%lld",&x) 20 | #define s2(x,y) s(x)+s(y) 21 | #define s3(x,y,z) s(x)+s(y)+s(z) 22 | 23 | #define p(x) printf("%lld\n",x) 24 | #define p2(x,y) p(x)+p(y) 25 | #define p3(x,y,z) p(x)+p(y)+p(z) 26 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 27 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 28 | 29 | #define ff first 30 | #define ss second 31 | #define mp(x,y) make_pair(x,y) 32 | #define pll pair 33 | #define pb push_back 34 | 35 | ll inf = 1e18; 36 | ll mod = 1e9 + 7 ; 37 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 38 | 39 | map,bool> dp; 40 | 41 | // Returns the result if there is a nonempty set of elements whose sum%m is zero 42 | bool isPossible(vector &vec, ll m, ll ind, ll modulo){ 43 | // base case : if we have reached the end, we will see if the modulo value is zero or not 44 | // Note that the modulo will contain the remainder obtained after dividing the sum of all 45 | // the elements considered so far by m. 46 | if(ind>=vec.size())return modulo==0; 47 | 48 | // Return the solution if already calculated 49 | if(dp.find({ind,modulo})!=dp.end())return dp[{ind,modulo}]; 50 | 51 | // If by adding the current element in the overall sum, we attain modulo zero , return success. 52 | if(vec[ind]%m == m-modulo)return dp[{ind,modulo}]=true; 53 | 54 | // Else consider the two options, either add this element in the overall sum or don't add. 55 | return dp[{ind,modulo}]= isPossible(vec,m,ind+1,modulo) or isPossible(vec,m,ind+1,(modulo+vec[ind])%m); 56 | } 57 | 58 | int main() 59 | { 60 | // freopen("input.txt","r",stdin); 61 | // freopen("output.txt","w",stdout); 62 | ll t=1; 63 | //s(t); 64 | while(t--){ 65 | ll n,m; 66 | s2(n,m); 67 | vectorvec; 68 | F(i,1,n){ 69 | ll num; 70 | cin>>num; 71 | vec.push_back(num); 72 | } 73 | if(isPossible(vec,m,0,m))cout<<"YES\n"; 74 | else cout<<"NO\n"; 75 | } 76 | } 77 | ``` -------------------------------------------------------------------------------- /Editorials/EDITORIAL - MONEY CHANGE.md: -------------------------------------------------------------------------------- 1 | ## MONEY CHANGE 2 | ##### Problem Link : [Money Change](https://hack.codingblocks.com/contests/c/141/1026) 3 | 4 | This is a very basic Dynamic Programming Approach based solely on the observation that either we take a denomination or not take a denomination. When we try to skip a denomination, we skip it completely but when we try to take a denomination, we can still take it another time as there are infinite amount of coins available for every destination. The following code is written in a bottom up manner. 5 | 6 | _**Time Complexity:** O(m*N)_ where **m** = Total Number of Denominations and **N** = Target Amount to be made 7 | 8 | Read below commented code for implementation details. 9 | ```C++ 10 | 11 | #include 12 | #include 13 | using namespace std; 14 | #define fio ios_base::sync_with_stdio(false) 15 | 16 | #define ll long long int 17 | 18 | #define s(x) scanf("%lld",&x) 19 | #define s2(x,y) s(x)+s(y) 20 | #define s3(x,y,z) s(x)+s(y)+s(z) 21 | 22 | #define p(x) printf("%lld\n",x) 23 | #define p2(x,y) p(x)+p(y) 24 | #define p3(x,y,z) p(x)+p(y)+p(z) 25 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 26 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 27 | 28 | #define ff first 29 | #define ss second 30 | #define mp(x,y) make_pair(x,y) 31 | #define pll pair 32 | #define pb push_back 33 | 34 | ll inf = 1e18; 35 | ll mod = 1e9 + 7 ; 36 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 37 | 38 | int main() 39 | { 40 | // freopen("input.txt","r",stdin); 41 | // freopen("output.txt","w",stdout); 42 | ll t=1; 43 | s(t); 44 | while(t--){ 45 | ll n; 46 | s(n); 47 | vector coins(n); 48 | F(i,1,n)cin>>coins[i-1]; 49 | ll N; 50 | s(N); 51 | 52 | // dp[i][j] represents the number of ways to make change for j cents using coins[0] to coins[i] denominations. 53 | vector> dp(n,vector(N+1,0)); 54 | F(i,0,n-1)dp[i][0]=1ll; 55 | F(j,1,N){ 56 | F(i,0,n-1){ 57 | // Option 1: You dont't take that denomination 58 | if(i>0)dp[i][j]=dp[i-1][j]; 59 | if(coins[i]<=j){ 60 | // Option 2: You take the denomination 61 | dp[i][j]+=dp[i][j-coins[i]]; // Total number of ways is the sum of both these options 62 | dp[i][j]%=mod; 63 | } 64 | } 65 | } 66 | p(dp[n-1][N]); 67 | } 68 | } 69 | 70 | 71 | ``` -------------------------------------------------------------------------------- /Editorials/EDITORIAL - NEIGHBOURING ENEMY.md: -------------------------------------------------------------------------------- 1 | ## NEIGHBOURING ENEMY 2 | ##### Problem Link : [Neighbouring Enemy](https://hack.codingblocks.com/practice/p/140/1036) 3 | 4 | This is a very basic problem. 5 | First, maintain the map of the numbers and their frequency in an ordered map. This takes `O(NxlogN)` time. Then make another array of distinct elements in the array and traverse through it. 6 | Let `dp[i] = maximum sum possible till the ith number of this new array`. 7 | Here, we have only two possibilities: 8 | 9 | >Either we take the ith digit and delete it, Or 10 | 11 | >We take another digit,1 less than the current digit, and delete that 12 | 13 | We pick the maximum of the two. 14 | 15 | _**Time Complexity: O(NxlogN)**_ where N = Number Of Elements in the Array 16 | 17 | Read below code for implementation details. 18 | ```C++ 19 | 20 | #include 21 | #include 22 | using namespace std; 23 | #define fio ios_base::sync_with_stdio(false) 24 | 25 | #define ll long long int 26 | 27 | #define s(x) scanf("%lld",&x) 28 | #define s2(x,y) s(x)+s(y) 29 | #define s3(x,y,z) s(x)+s(y)+s(z) 30 | 31 | #define p(x) printf("%lld\n",x) 32 | #define p2(x,y) p(x)+p(y) 33 | #define p3(x,y,z) p(x)+p(y)+p(z) 34 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 35 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 36 | 37 | #define ff first 38 | #define ss second 39 | #define mp(x,y) make_pair(x,y) 40 | #define pll pair 41 | #define pb push_back 42 | 43 | ll inf = 1e18; 44 | ll mod = 1e9 + 7 ; 45 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 46 | 47 | int main() 48 | { 49 | // freopen("input.txt","r",stdin); 50 | // freopen("output.txt","w",stdout); 51 | ll t=1; 52 | // s(t); 53 | while(t--){ 54 | ll n; 55 | s(n); 56 | vectorb; 57 | mapmp; 58 | ll num; 59 | F(i,0,n-1){cin>>num; mp[num]++;} 60 | for(auto ele:mp){ 61 | b.push_back(ele.first); 62 | } 63 | 64 | n=b.size(); 65 | vectordp(n,0); 66 | dp[0]=b[0]*mp[b[0]]; 67 | 68 | for(ll i=1;i=2) temp+=dp[i-2]; 72 | dp[i]=max(dp[i-1],temp); 73 | }else dp[i]=dp[i-1]+b[i]*mp[b[i]]; 74 | } 75 | cout< 14 | #include 15 | using namespace std; 16 | #define fio ios_base::sync_with_stdio(false) 17 | 18 | #define ll long long int 19 | 20 | #define s(x) scanf("%lld",&x) 21 | #define s2(x,y) s(x)+s(y) 22 | #define s3(x,y,z) s(x)+s(y)+s(z) 23 | 24 | #define p(x) printf("%lld\n",x) 25 | #define p2(x,y) p(x)+p(y) 26 | #define p3(x,y,z) p(x)+p(y)+p(z) 27 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 28 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 29 | 30 | #define ff first 31 | #define ss second 32 | #define mp(x,y) make_pair(x,y) 33 | #define pll pair 34 | #define pb push_back 35 | 36 | ll inf = 1e18; 37 | ll mod = 1e9 + 7 ; 38 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 39 | 40 | int main() 41 | { 42 | // freopen("input.txt","r",stdin); 43 | // freopen("output.txt","w",stdout); 44 | ll t=1; 45 | // s(t); 46 | while(t--){ 47 | ll n; 48 | cin>>n; 49 | ll a=1; 50 | ll b=2; 51 | // Note that if we place the tile vertically the problem reduces to finding the number of ways in 2 X n-1 board 52 | // If we place the tile horizontally , the problem reduces to finding the number of ways in 2 x n-2 board since 53 | // only 1 tile can only be placed in only horizontal manner above the horizontally placed tile. 54 | // So total number of ways : F(n)= F(n+1)+F(n+2). 55 | // Or if placed in the opposite manner : F(n) = F(n-1) + F(n-2) = nth fibonacci number 56 | 57 | if(n==1)cout< 13 | #include 14 | using namespace std; 15 | 16 | #define ll long long int 17 | 18 | #define s(x) scanf("%lld",&x) 19 | #define s2(x,y) s(x)+s(y) 20 | #define s3(x,y,z) s(x)+s(y)+s(z) 21 | 22 | #define p(x) printf("%lld\n",x) 23 | #define p2(x,y) p(x)+p(y) 24 | #define p3(x,y,z) p(x)+p(y)+p(z) 25 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 26 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 27 | 28 | #define ff first 29 | #define ss second 30 | #define mp(x,y) make_pair(x,y) 31 | #define pll pair 32 | #define pb push_back 33 | ll mod = 1e9 + 7 ; 34 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 35 | 36 | int main() 37 | { 38 | freopen("input.txt","r",stdin); 39 | freopen("output.txt","w",stdout); 40 | ll t; 41 | s(t); 42 | while(t--){ 43 | ll n,k; 44 | s2(n,k); 45 | vectorar(n); 46 | while(n--){ 47 | ll num; s(num); 48 | // ct counts the number of set bits in num 49 | ll ct=0; 50 | while(num){ 51 | // This formula simply clears the first set bit from right in num 52 | num=num&(num-1); 53 | ct++; 54 | } 55 | ar.pb(ct); 56 | } 57 | // sort to find the K largest number of set bits and sum them up 58 | sort(ar.begin(),ar.end()); 59 | ll ans=0; 60 | for(ll i=ar.size()-1,j=1;i>=0 and j<=k;j++,i--)ans+=ar[i]; 61 | p(ans); 62 | } 63 | } 64 | ``` 65 | -------------------------------------------------------------------------------- /Editorials/EDITORIAL - RANDOM QUERY.md: -------------------------------------------------------------------------------- 1 | ## RANDOM QUERY 2 | ##### Problem Link : [Random Query](https://hack.codingblocks.com/contests/c/126/834) 3 | 4 | The solution is very simple.Just change the way you look at the question! Rather than thinking in terms of segments, think what all segments each number could be a part of. And that will solve the question in O(N) time. 5 | 6 | Take a number, find its last occurence, then count the numbers between the number and its last occurence, then simply mulitply it with the total count of numbers on its right hand side. You are bascially adding 1 to all the ranges which consists of that number, each time you do that. 7 | 8 | Once we have computed the total number of unique elements in all the segments,we then divide it with the total number of possible segments which comes out to be N2. Note that a segment can also has L>R in which case we have to swap the L and R. So, the total number of segments comes out to be : 9 | >`N(when L==R) + (N*(N-1)/2)(for all segments where LR) = N`2. 10 | 11 | _**Time Complexity:** O(N)_ where N = Number of elements in the array. 12 | 13 | Read below commented code for implementation details. 14 | ```C++ 15 | 16 | #include 17 | #include 18 | using namespace std; 19 | #define fio ios_base::sync_with_stdio(false) 20 | 21 | #define ll long long int 22 | 23 | #define s(x) scanf("%lld",&x) 24 | #define s2(x,y) s(x)+s(y) 25 | #define s3(x,y,z) s(x)+s(y)+s(z) 26 | 27 | #define p(x) printf("%lld\n",x) 28 | #define p2(x,y) p(x)+p(y) 29 | #define p3(x,y,z) p(x)+p(y)+p(z) 30 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 31 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 32 | 33 | #define ff first 34 | #define ss second 35 | #define mp(x,y) make_pair(x,y) 36 | #define pll pair 37 | #define pb push_back 38 | 39 | ll inf = 1e18; 40 | ll mod = 1e9 + 7 ; 41 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 42 | 43 | int main() 44 | { 45 | // freopen("input.txt","r",stdin); 46 | // freopen("output.txt","w",stdout); 47 | ll t=1; 48 | //s(t); 49 | while(t--){ 50 | ll n; 51 | s(n); 52 | vectorar(1000000+5,0); 53 | F(i,1,n)cin>>ar[i]; 54 | ll ans=0ll; 55 | // occ[z] will contain the array index where the number z was last found 56 | vector occ(1000000+5,0); 57 | F(i,1,n){ 58 | ans+=(i-occ[ar[i]])*(n-i+1)*2 - 1ll; 59 | occ[ar[i]]=i; 60 | } 61 | double res = (double)(ans)/(double)(n*n); 62 | printf("%.6lf\n",res); 63 | } 64 | } 65 | 66 | ``` -------------------------------------------------------------------------------- /Editorials/EDITORIAL - ROTATE THE MATRIX.md: -------------------------------------------------------------------------------- 1 | ## ROTATE THE MATRIX 2 | ##### Problem Link : [Rotate The Matrix](https://hack.codingblocks.com/contests/c/1001/1229) 3 | 4 | If we find out how to rotate a matrix by 90°, we can just apply the same procedure on that rotated matrix to obtain 180° and further applying it to get 270°. In order to rotate a matrix by 90° we can use the following relation: 5 | 6 | > Rotated N−j−1,i = Originali,j,0≤i,jBi,j = Ai,j || AN−j−i,i || AN−i−1,N−j−1 || Aj,N−i−1 13 | ​​ 14 | 15 | _**Time Complexity:** O(N 2)_ where N is the dimension of the square matrix 16 | 17 | Read below commented code for implementation details. 18 | ```C++ 19 | #include 20 | #include 21 | using namespace std; 22 | #define fio ios_base::sync_with_stdio(false) 23 | 24 | #define ll long long int 25 | 26 | #define s(x) scanf("%lld",&x) 27 | #define s2(x,y) s(x)+s(y) 28 | #define s3(x,y,z) s(x)+s(y)+s(z) 29 | 30 | #define p(x) printf("%lld\n",x) 31 | #define p2(x,y) p(x)+p(y) 32 | #define p3(x,y,z) p(x)+p(y)+p(z) 33 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 34 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 35 | 36 | #define ff first 37 | #define ss second 38 | #define mp(x,y) make_pair(x,y) 39 | #define pll pair 40 | #define pb push_back 41 | 42 | ll inf = 1e18; 43 | ll mod = 1e9 + 7 ; 44 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 45 | 46 | int main() 47 | { 48 | // freopen("input.txt","r",stdin); 49 | // freopen("output.txt","w",stdout); 50 | ll t=1; 51 | //s(t); 52 | while(t--){ 53 | ll n; 54 | s(n); 55 | vector> a(n+1); 56 | F(i,1,n){ 57 | a[i].resize(n+1); 58 | F(j,1,n){ 59 | cin>>a[i][j]; 60 | } 61 | } 62 | 63 | F(i,1,n){ 64 | F(j,1,n){ 65 | // Basic Observation 66 | ll res = a[i][j]|a[j][n-i+1]|a[n-j+1][i]|a[n-i+1][n-j+1]; 67 | cout<2sub>P)_ where P = Required Amount Of Money 7 | 8 | Read below commented code for implementation details. 9 | ```C++ 10 | #include 11 | #include 12 | using namespace std; 13 | #define fio ios_base::sync_with_stdio(false) 14 | 15 | #define ll long long int 16 | 17 | #define s(x) scanf("%lld",&x) 18 | #define s2(x,y) s(x)+s(y) 19 | #define s3(x,y,z) s(x)+s(y)+s(z) 20 | 21 | #define p(x) printf("%lld\n",x) 22 | #define p2(x,y) p(x)+p(y) 23 | #define p3(x,y,z) p(x)+p(y)+p(z) 24 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 25 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 26 | 27 | #define ff first 28 | #define ss second 29 | #define mp(x,y) make_pair(x,y) 30 | #define pll pair 31 | #define pb push_back 32 | 33 | ll inf = 1e18; 34 | ll mod = 1e9 + 7 ; 35 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 36 | 37 | /****************************************************************************/ 38 | 39 | int main() 40 | { 41 | // freopen("input.txt","r",stdin); 42 | // freopen("output.txt","w",stdout); 43 | ll t=1; 44 | s(t); 45 | while(t--){ 46 | ll P; 47 | s(P); 48 | ll ans=0ll; 49 | while(P){ 50 | if(P&1){ ans++; P--; } 51 | else { P=P>>1; } 52 | } 53 | p(ans); 54 | } 55 | return 0; 56 | } 57 | ``` -------------------------------------------------------------------------------- /Editorials/EDITORIAL - THE ABSOLUTE SUM FUNCTION.md: -------------------------------------------------------------------------------- 1 | ## THE ABSOLUTE SUM FUNCTION 2 | ##### Problem Link : [The Absolute Sum Function](https://hack.codingblocks.com/contests/c/1001/1074) 3 | 4 | Just try every possibility out there. Either you place `1` or `B[i]` in the answer , because you have to maximise the sum of the absolute difference, hence only extreme values can contribute to the answer. So keep maintaining the answer in `dp[]` array. 5 | 6 | `dp[0][i]` means maximum achievable sum by placing `1` at index `i` using the first `i` elements only. 7 | `dp[1][i]` means maximum achievable sum by placing `B[i]` at index `i` using the first `i` elements only. 8 | 9 | Now for computing them, find the answer by taking the maximum of the two cases when you have place either `1` or `B[i-1]` at index `i-1`. 10 | 11 | _**Time Complexity:** O(N)_ where N is the size of the array 12 | 13 | Read below commented code for implementation details. 14 | ```C++ 15 | #include 16 | #include 17 | using namespace std; 18 | #define fio ios_base::sync_with_stdio(false) 19 | 20 | #define ll long long int 21 | 22 | #define s(x) scanf("%lld",&x) 23 | #define s2(x,y) s(x)+s(y) 24 | #define s3(x,y,z) s(x)+s(y)+s(z) 25 | 26 | #define p(x) printf("%lld\n",x) 27 | #define p2(x,y) p(x)+p(y) 28 | #define p3(x,y,z) p(x)+p(y)+p(z) 29 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 30 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 31 | 32 | #define ff first 33 | #define ss second 34 | #define mp(x,y) make_pair(x,y) 35 | #define pll pair 36 | #define pb push_back 37 | 38 | ll inf = 1e18; 39 | ll mod = 1e9 + 7 ; 40 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 41 | 42 | int main() 43 | { 44 | // freopen("input.txt","r",stdin); 45 | // freopen("output.txt","w",stdout); 46 | ll t=1; 47 | s(t); 48 | while(t--){ 49 | ll n; 50 | s(n); 51 | vectorar; 52 | F(i,0,n-1){ 53 | ll num; 54 | cin>>num; 55 | ar.pb(num); 56 | } 57 | 58 | if(n==1){ 59 | cout<<"0\n"; 60 | continue; 61 | } 62 | 63 | vectordp[2]; 64 | // dp[0][i] means maximum achievable sum by placing 1 at index i using the first `i` elements only. 65 | dp[0]=vector(n,0ll); 66 | // dp[1][i] means maximum achievable sum by placing ar[i] at index i using the first `i` elements only. 67 | dp[1]=vector(n,0ll); 68 | 69 | for(ll i=1;ith** bit is set. The bit having maximum count and is smallest is the required answer. 5 | For the above operation, for each integer, we can go through all the 32 bits and check whether the **ith** bit is set in the integer or not. If yes, increase the count **freq[i]** by **1**. 6 | 7 | _**Time Complexity:** O(T * N * log2num)_ where T = Number Of Test Cases, N = Total numbers for that test case , num = 32 bit Number 8 | 9 | Read below commented code for implementation details. 10 | ```C++ 11 | #include 12 | #include 13 | using namespace std; 14 | #define fio ios_base::sync_with_stdio(false) 15 | 16 | #define ll long long int 17 | 18 | #define s(x) scanf("%lld",&x) 19 | #define s2(x,y) s(x)+s(y) 20 | #define s3(x,y,z) s(x)+s(y)+s(z) 21 | 22 | #define p(x) printf("%lld\n",x) 23 | #define p2(x,y) p(x)+p(y) 24 | #define p3(x,y,z) p(x)+p(y)+p(z) 25 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 26 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 27 | 28 | #define ff first 29 | #define ss second 30 | #define mp(x,y) make_pair(x,y) 31 | #define pll pair 32 | #define pb push_back 33 | 34 | ll inf = 1e18; 35 | ll mod = 1e9 + 7 ; 36 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 37 | 38 | /****************************************************************************/ 39 | 40 | int main() 41 | { 42 | // freopen("input.txt","r",stdin); 43 | // freopen("output.txt","w",stdout); 44 | ll t=1; 45 | s(t); 46 | 47 | while(t--){ 48 | ll n; 49 | s(n); 50 | vector freq(32); 51 | while(n--){ 52 | ll num; 53 | s(num); 54 | // bt will keep track of which bit is currently checked. 55 | ll bt=0; 56 | 57 | while(num>0){ 58 | // if bt bit is set, freq for that bit is increased. 59 | if(num&1)freq[bt]++; 60 | 61 | // Move to the next bit; 62 | num>>=1; 63 | 64 | // Also increment the bit 65 | bt++; 66 | } 67 | } 68 | 69 | // Finally output that bit which has the maximum frequency 70 | ll ans=0; 71 | ll maxi=0; 72 | for(ll bt=0;bt<32;bt++){ 73 | if(freq[bt]>maxi){ 74 | maxi=freq[bt]; 75 | ans=bt; 76 | } 77 | } 78 | p(ans); 79 | } 80 | return 0; 81 | } 82 | ``` -------------------------------------------------------------------------------- /Editorials/EDITORIAL - TILLING PROBLEM - II.md: -------------------------------------------------------------------------------- 1 | ## TILLING PROBLEM - II 2 | ##### Problem Link : [Tilling Problem - II](https://hack.codingblocks.com/contests/c/1001/1045) 3 | 4 | Read the problem as matrix of size `M x N`, rather than `N x M` as it helps in easy visualisation. 5 | 6 | In this manner, 7 |
    8 |
  • A tile can be either place vertically,meaning it will only occupy a cell of width 1 and a complete hieght of M.
  • 9 | Or, 10 |
  • You can place M tiles horizontally one over the other if there is enough width left.
  • 11 |
12 | 13 | _**Time Complexity:** O(N)_ where N is the number of rows 14 | 15 | Read below commented code for implementation details. 16 | ```C++ 17 | #include 18 | #include 19 | using namespace std; 20 | #define fio ios_base::sync_with_stdio(false) 21 | 22 | #define ll long long int 23 | 24 | #define s(x) scanf("%lld",&x) 25 | #define s2(x,y) s(x)+s(y) 26 | #define s3(x,y,z) s(x)+s(y)+s(z) 27 | 28 | #define p(x) printf("%lld\n",x) 29 | #define p2(x,y) p(x)+p(y) 30 | #define p3(x,y,z) p(x)+p(y)+p(z) 31 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 32 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 33 | 34 | #define ff first 35 | #define ss second 36 | #define mp(x,y) make_pair(x,y) 37 | #define pll pair 38 | #define pb push_back 39 | 40 | ll inf = 1e18; 41 | ll mod = 1e9 + 7 ; 42 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 43 | 44 | int main() 45 | { 46 | // freopen("input.txt","r",stdin); 47 | // freopen("output.txt","w",stdout); 48 | ll t=1; 49 | s(t); 50 | while(t--){ 51 | ll n,m; 52 | s2(n,m); 53 | vectordp(n+1,0); 54 | dp[0]=1ll; 55 | for(ll i=1;i<=n;i++){ 56 | // Placing the tile vertically 57 | dp[i]=dp[i-1]; 58 | // Placint the tile horizontally if there is space 59 | dp[i]+=((i-m)>=0)?dp[i-m]:0; 60 | dp[i]%=mod; 61 | } 62 | p(dp[n]); 63 | } 64 | } 65 | 66 | ``` -------------------------------------------------------------------------------- /Editorials/EDITORIAL - WILDCARD PATTERN MATCHING.md: -------------------------------------------------------------------------------- 1 | ## WILDCARD PATTERN MATCHING 2 | ##### Problem Link : [Wildcard Pattern Matching](https://hack.codingblocks.com/contests/c/1001/1058) 3 | 4 | Very simple problem, just maintain a DP state for every index of the string and the pattern. 5 | 6 | _**Time Complexity:** O(N x M)_ where N is the size of the string, M is the size of the pattern 7 | 8 | Read below commented code for implementation details. 9 | ```C++ 10 | 11 | #include 12 | #include 13 | using namespace std; 14 | #define fio ios_base::sync_with_stdio(false) 15 | 16 | #define ll long long int 17 | 18 | #define s(x) scanf("%lld",&x) 19 | #define s2(x,y) s(x)+s(y) 20 | #define s3(x,y,z) s(x)+s(y)+s(z) 21 | 22 | #define p(x) printf("%lld\n",x) 23 | #define p2(x,y) p(x)+p(y) 24 | #define p3(x,y,z) p(x)+p(y)+p(z) 25 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 26 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 27 | 28 | #define ff first 29 | #define ss second 30 | #define mp(x,y) make_pair(x,y) 31 | #define pll pair 32 | #define pb push_back 33 | 34 | ll inf = 1e18; 35 | ll mod = 1e9 + 7 ; 36 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 37 | 38 | map dp; 39 | 40 | bool doesPatMatches(string &str, string &pat, ll i,ll j){ 41 | 42 | // Base Case : will only reach here if the pattern has matched the string 43 | if(i==str.size() and j==pat.size())return true; 44 | if(dp.find({i,j})!=dp.end())return dp[{i,j}]; 45 | 46 | if(i==str.size()){ 47 | if(pat[j]!='*')return false; // because * means 0 or more characters, hence if it is not `*`, there is no way to match the finished string with the remaining pattern 48 | return dp[{i,j}]=doesPatMatches(str,pat,i,j+1);// will only return true if pattern has only `*` ahead left 49 | } 50 | if(j==pat.size())return dp[{i,j}]=false; // pattern finished, but string is still left 51 | if(pat[j]=='*'){ 52 | // since '*' means 0 or more characters, the left part of 'or' considers 0 characters and the right part means 1 or more characters 53 | return dp[{i,j}] = doesPatMatches(str,pat,i,j+1) or doesPatMatches(str,pat,i+1,j); 54 | }else if(pat[j]=='?'){ 55 | return dp[{i,j}]= doesPatMatches(str,pat,i+1,j+1); 56 | }else{ 57 | return dp[{i,j}] = str[i]==pat[j] and doesPatMatches(str,pat,i+1,j+1); 58 | } 59 | } 60 | 61 | int main() 62 | { 63 | // freopen("input.txt","r",stdin); 64 | // freopen("output.txt","w",stdout); 65 | ll t=1; 66 | // s(t); 67 | while(t--){ 68 | dp.clear(); 69 | string str,pat; 70 | cin>>str>>pat; 71 | cout< 4 | #include 5 | using namespace std; 6 | #define fio ios_base::sync_with_stdio(false) 7 | 8 | #define ll long long int 9 | 10 | #define s(x) scanf("%lld",&x) 11 | #define s2(x,y) s(x)+s(y) 12 | #define s3(x,y,z) s(x)+s(y)+s(z) 13 | 14 | #define p(x) printf("%lld\n",x) 15 | #define p2(x,y) p(x)+p(y) 16 | #define p3(x,y,z) p(x)+p(y)+p(z) 17 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 18 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 19 | 20 | #define ff first 21 | #define ss second 22 | #define mp(x,y) make_pair(x,y) 23 | #define pll pair 24 | #define pb push_back 25 | 26 | // TEMPLATE FOR POLICY BASED DATA STRUCTURES // 27 | 28 | // #include 29 | // #include 30 | // using namespace __gnu_pbds; 31 | // typedef tree, rb_tree_tag, tree_order_statistics_node_update> OST; 32 | 33 | // TEMPLATE OVER // 34 | 35 | ll inf = 1e18; 36 | ll mod = 1e9 + 7 ; 37 | ll r,c; 38 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 39 | 40 | // Every pond is identified uniquely by a timer variable 41 | ll dfs(ll i,ll j,vector> &ar,ll timer){ 42 | ar[i][j]=timer; 43 | ll ct=1; 44 | if(i-1 > 0 and ar[i-1][j]==1)ct+=dfs(i-1,j,ar,timer); 45 | if(i+1 <= r and ar[i+1][j]==1)ct+=dfs(i+1,j,ar,timer); 46 | if(j-1 > 0 and ar[i][j-1]==1)ct+=dfs(i,j-1,ar,timer); 47 | if(j+1 <= c and ar[i][j+1]==1)ct+=dfs(i,j+1,ar,timer); 48 | return ct; 49 | } 50 | 51 | int main() 52 | { 53 | // freopen("input.txt","r",stdin); 54 | // freopen("output.txt","w",stdout); 55 | ll t=1; 56 | //s(t); 57 | while(t--){ 58 | 59 | s2(r,c); 60 | 61 | vector>ar(r+1); 62 | 63 | F(i,1,r){ 64 | ar[i].resize(c+1); 65 | F(j,1,c){ 66 | cin>>ar[i][j]; 67 | } 68 | } 69 | 70 | ll ans=-inf; 71 | // mp stores the size of each pond. Note that each pond is 72 | // identified uniquely by a timer. 73 | map mp; 74 | ll timer=2; 75 | 76 | F(i,1,r){ 77 | F(j,1,c){ 78 | if(ar[i][j]==1){ 79 | mp[timer]=dfs(i,j,ar,timer); 80 | ans=max(ans,mp[timer]); 81 | timer++; 82 | } 83 | } 84 | } 85 | 86 | // Finally for every dry cell, sum up the size of each pond it is 87 | // sorrounded by because the moment that dry cell is watered, it will 88 | // connect all adjacent ponds. 89 | F(i,1,r){ 90 | F(j,1,c){ 91 | if(ar[i][j]==0){ 92 | set st; 93 | if(i-1 > 0 and ar[i-1][j]>1)st.insert(ar[i-1][j]); 94 | if(i+1 <= r and ar[i+1][j]>1)st.insert(ar[i+1][j]); 95 | if(j-1 > 0 and ar[i][j-1]>1)st.insert(ar[i][j-1]); 96 | if(j+1 <= c and ar[i][j+1]>1)st.insert(ar[i][j+1]); 97 | 98 | ll sum=1; 99 | for(auto it:st){ 100 | sum+=mp[it]; 101 | } 102 | ans=max(ans,sum); 103 | } 104 | } 105 | } 106 | 107 | p(ans); 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /Expectation - RANDOM QUERY - CODING BLOCKS .cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | using namespace std; 5 | #define fio ios_base::sync_with_stdio(false) 6 | 7 | #define ll long long int 8 | 9 | #define s(x) scanf("%lld",&x) 10 | #define s2(x,y) s(x)+s(y) 11 | #define s3(x,y,z) s(x)+s(y)+s(z) 12 | 13 | #define p(x) printf("%lld\n",x) 14 | #define p2(x,y) p(x)+p(y) 15 | #define p3(x,y,z) p(x)+p(y)+p(z) 16 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 17 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 18 | 19 | #define ff first 20 | #define ss second 21 | #define mp(x,y) make_pair(x,y) 22 | #define pll pair 23 | #define pb push_back 24 | 25 | ll inf = 1e18; 26 | ll mod = 1e9 + 7 ; 27 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 28 | 29 | int main() 30 | { 31 | // The solution is very simple. 32 | // Just change the way you look at the question! 33 | // Rather than thinking in terms of segments, think what all segments each number could be 34 | // a part of. And that will solve the question in O(N) time. 35 | 36 | // Take a number, find its last occurence, then count the numbers between the number and its last 37 | // occurence, then simply mulitply it with the total count of numbers on its right hand side. 38 | // You are bascially adding 1 to all the ranges which consists of that number, each time you 39 | // do that. 40 | 41 | // freopen("input.txt","r",stdin); 42 | // freopen("output.txt","w",stdout); 43 | ll t=1; 44 | //s(t); 45 | while(t--){ 46 | ll n; 47 | s(n); 48 | vectorar(1000000+5,0); 49 | F(i,1,n)cin>>ar[i]; 50 | ll ans=0ll; 51 | // Occ[z] will contain the array index where the number z was last found 52 | vector occ(1000000+5,0); 53 | F(i,1,n){ 54 | ans+=(i-occ[ar[i]])*(n-i+1)*2 - 1ll; 55 | occ[ar[i]]=i; 56 | } 57 | double res = (double)(ans)/(double)(n*n); 58 | printf("%.6lf\n",res); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /FRIENDS AT CODING BLOCKS - BITMASKING - DP - HACKERBLOCKS.cpp: -------------------------------------------------------------------------------- 1 | // https://www.hackerearth.com/practice/algorithms/dynamic-programming/bit-masking/practice-problems/algorithm/monks-visit-to-amathville/editorial/ 2 | 3 | #include 4 | #include 5 | using namespace std; 6 | #define fio ios_base::sync_with_stdio(false) 7 | 8 | #define ll long long int 9 | 10 | #define s(x) scanf("%lld",&x) 11 | #define s2(x,y) s(x)+s(y) 12 | #define s3(x,y,z) s(x)+s(y)+s(z) 13 | 14 | #define p(x) printf("%lld\n",x) 15 | #define p2(x,y) p(x)+p(y) 16 | #define p3(x,y,z) p(x)+p(y)+p(z) 17 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 18 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 19 | 20 | #define ff first 21 | #define ss second 22 | #define mp(x,y) make_pair(x,y) 23 | #define pll pair 24 | #define pb push_back 25 | #define all(v) v.begin(), v.end() 26 | 27 | ll inf = 1e18; 28 | ll mod = 1e9 + 7 ; 29 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 30 | 31 | /****************************************************************************/ 32 | 33 | // primes[] contains all primes less than 50 34 | vector primes; 35 | 36 | void mySieve(){ 37 | primes.pb(1); 38 | primes.pb(2); 39 | vectorpr(50,true); 40 | for(ll i=3;i*i<=50;i+=2){ 41 | if(pr[i]){ 42 | for(ll j=i*i;j<=50;j+=2*i)pr[j]=false; 43 | } 44 | } 45 | for(ll i=3;i<=50;i+=2)if(pr[i])primes.pb(i); 46 | } 47 | 48 | // dp[prev][mask] contains the answer for a particular mask whose last bit set was 'prev' 49 | ll dp[20][65536]; 50 | ll minFriction(ll finalInd,ll prev,ll mask, ll P){ 51 | 52 | // if the number of set bits is equal to finalInd+1, this indicates that all friends 53 | // have been included 54 | if(__builtin_popcount(mask)==finalInd+1)return 0ll; 55 | 56 | // prev+1 is considered for every prev in order to counter the case when prev is -1 57 | if(dp[prev+1][mask]!=-1)return dp[prev+1][mask]; 58 | 59 | ll ret=inf; 60 | for(ll i=0;i<=finalInd;i++){ 61 | if((1< 4 | #include 5 | using namespace std; 6 | #define fio ios_base::sync_with_stdio(false) 7 | 8 | #define ll long long int 9 | 10 | #define s(x) scanf("%lld",&x) 11 | #define s2(x,y) s(x)+s(y) 12 | #define s3(x,y,z) s(x)+s(y)+s(z) 13 | 14 | #define p(x) printf("%lld\n",x) 15 | #define p2(x,y) p(x)+p(y) 16 | #define p3(x,y,z) p(x)+p(y)+p(z) 17 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 18 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 19 | 20 | #define ff first 21 | #define ss second 22 | #define mp(x,y) make_pair(x,y) 23 | #define pll pair 24 | #define pb push_back 25 | 26 | ll inf = 1e18; 27 | ll mod = 1e9 + 7 ; 28 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 29 | 30 | /****************************************************************************/ 31 | vector>adj; 32 | vectorisSpecial; 33 | 34 | bool dfs_markSpecial(ll node, ll par=-1){ 35 | bool ret=0; 36 | F(i,0,adj[node].size()-1){ 37 | ll child = adj[node][i].first; 38 | ll spec = adj[node][i].second; 39 | if(child==par)continue; 40 | bool res = dfs_markSpecial(child,node); 41 | if(spec==0)spec=res; 42 | isSpecial[child]=spec; 43 | ret = ret or spec; 44 | } 45 | return ret; 46 | } 47 | 48 | ll dfs(ll node,ll par=-1){ 49 | ll ret=1ll; 50 | F(i,0,adj[node].size()-1){ 51 | ll child = adj[node][i].first; 52 | ll spec = adj[node][i].second; 53 | if(child==par or isSpecial[child])continue; 54 | ret=(ret*(1+dfs(child,node)))%mod; 55 | } 56 | return ret; 57 | } 58 | 59 | int main() 60 | { 61 | freopen("input.txt","r",stdin); 62 | // freopen("output.txt","w",stdout); 63 | ll t=1; 64 | s(t); 65 | while(t--){ 66 | ll n; 67 | s(n); 68 | adj=vector>(n+5); 69 | isSpecial=vector(n+5,false); 70 | ll specNode=-1; 71 | F(i,1,n-1){ 72 | ll u,v,g; 73 | s3(u,v,g); 74 | adj[u].pb({v,g}); 75 | adj[v].pb({u,g}); 76 | if(isSpecial[v]==0)isSpecial[v]=g; 77 | if(isSpecial[u]==0)isSpecial[u]=g; 78 | if(g==1)specNode=u; 79 | } 80 | 81 | // make a connected component of all the special nodes and those that fall between them 82 | if(specNode!=-1)dfs_markSpecial(specNode); 83 | 84 | vector res; 85 | F(i,1,n){ 86 | if(isSpecial[i])res.pb(dfs(i)); 87 | } 88 | ll ans=1ll; 89 | ll sz=res.size(); 90 | 91 | F(i,0,sz-1)ans=(ans*res[i])%mod; 92 | p(ans); 93 | } 94 | return 0; 95 | } -------------------------------------------------------------------------------- /GYM IN CODING BLOCKS - DP(MEDIUM-HARD) - HACKERBLOCKS.cpp: -------------------------------------------------------------------------------- 1 | // http://codeforces.com/contest/429/problem/B 2 | 3 | #include 4 | #include 5 | using namespace std; 6 | #define fio ios_base::sync_with_stdio(false) 7 | 8 | #define ll long long int 9 | 10 | #define s(x) scanf("%lld",&x) 11 | #define s2(x,y) s(x)+s(y) 12 | #define s3(x,y,z) s(x)+s(y)+s(z) 13 | 14 | #define p(x) printf("%lld\n",x) 15 | #define p2(x,y) p(x)+p(y) 16 | #define p3(x,y,z) p(x)+p(y)+p(z) 17 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 18 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 19 | 20 | #define ff first 21 | #define ss second 22 | #define mp(x,y) make_pair(x,y) 23 | #define pll pair 24 | #define pb push_back 25 | 26 | ll inf = 1e18; 27 | ll mod = 1e9 + 7 ; 28 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 29 | 30 | /****************************************************************************/ 31 | 32 | ll dp[5][1005][1005]; 33 | 34 | int main() 35 | { 36 | freopen("input.txt","r",stdin); 37 | freopen("output.txt","w",stdout); 38 | ll t=1; 39 | // s(t); 40 | while(t--){ 41 | ll n,m; 42 | s2(n,m); 43 | vector> gym(n+5,vector(m+5,0ll)); 44 | memset(dp,0ll,sizeof(dp)); 45 | F(i,1,n){ 46 | F(j,1,m){ 47 | s(gym[i][j]); 48 | } 49 | } 50 | 51 | // From top left to (i,j) 52 | F(i,1,n){ 53 | F(j,1,m){ 54 | dp[1][i][j]=max(dp[1][i][j-1],dp[1][i-1][j])+gym[i][j]; 55 | } 56 | } 57 | 58 | // From (i,j) to bottom right 59 | RF(i,n,1){ 60 | RF(j,m,1){ 61 | dp[2][i][j]=max(dp[2][i][j+1],dp[2][i+1][j])+gym[i][j]; 62 | } 63 | } 64 | 65 | // From bottom left to (i,j) 66 | RF(i,n,1){ 67 | F(j,1,m){ 68 | dp[3][i][j]=max(dp[3][i][j-1],dp[3][i+1][j])+gym[i][j]; 69 | } 70 | } 71 | 72 | // From (i,j) to top right 73 | F(i,1,n){ 74 | RF(j,m,1){ 75 | dp[4][i][j]=max(dp[4][i][j+1],dp[4][i-1][j])+gym[i][j]; 76 | } 77 | } 78 | ll ans=0ll; 79 | 80 | F(i,2,n-1){ 81 | F(j,2,m-1){ 82 | // Path in which Prateek Bhaiya comes from top and Arnav Bhaiya comes from left to (i,j) 83 | ans=max(ans,dp[1][i][j-1]+dp[2][i][j+1]+dp[3][i+1][j]+dp[4][i-1][j]); 84 | 85 | // Path in which Prateek Bhaiya comes from left and Arnav Bhaiya comes from bottom to (i,j) 86 | ans=max(ans,dp[1][i-1][j]+dp[2][i+1][j]+dp[3][i][j-1]+dp[4][i][j+1]); 87 | } 88 | } 89 | p(ans); 90 | } 91 | return 0; 92 | } -------------------------------------------------------------------------------- /HOLI - SPOJ-HACKERBLOCKS - GRAPHS - MEDIUM.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | using namespace std; 5 | #define fio ios_base::sync_with_stdio(false) 6 | 7 | #define ll long long int 8 | 9 | #define s(x) scanf("%lld",&x) 10 | #define s2(x,y) s(x)+s(y) 11 | #define s3(x,y,z) s(x)+s(y)+s(z) 12 | 13 | #define p(x) printf("%lld\n",x) 14 | #define p2(x,y) p(x)+p(y) 15 | #define p3(x,y,z) p(x)+p(y)+p(z) 16 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 17 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 18 | 19 | #define ff first 20 | #define ss second 21 | #define mp(x,y) make_pair(x,y) 22 | #define pll pair 23 | #define pb push_back 24 | 25 | ll inf = 1e18; 26 | ll mod = 1e9 + 7 ; 27 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 28 | 29 | 30 | 31 | vectoradj[100005]; 32 | 33 | // ret will contain the total travel distance (travelled one-sidedly) by the visitors , 34 | // that's why at the end we are multiplying by 2 as people from both sides are crossing 35 | // a particular edge. 36 | ll ret=0; 37 | ll n; 38 | 39 | ll dfs(ll par, ll u){ 40 | ll ans=1; 41 | for(ll i=0;i 3 | #include 4 | using namespace std; 5 | 6 | #define ll long long int 7 | 8 | #define s(x) scanf("%lld",&x) 9 | #define s2(x,y) s(x)+s(y) 10 | #define s3(x,y,z) s(x)+s(y)+s(z) 11 | 12 | #define p(x) printf("%lld\n",x) 13 | #define p2(x,y) p(x)+p(y) 14 | #define p3(x,y,z) p(x)+p(y)+p(z) 15 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 16 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 17 | 18 | #define ff first 19 | #define ss second 20 | #define mp(x,y) make_pair(x,y) 21 | #define pll pair 22 | #define pb push_back 23 | ll mod = 1e9 + 7 ; 24 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 25 | 26 | int main() 27 | { 28 | // freopen("input.txt","r",stdin); 29 | // freopen("output.txt","w",stdout); 30 | ll q; 31 | s(q); 32 | // tot[0] will contain the overall timestamp 33 | // tot[i] will contain the timestamp for the ith course for i>0 34 | ll tot[5]; 35 | memset(tot,-1,sizeof(tot)); 36 | tot[0]=0; 37 | 38 | // qu[i] will contain the students of the ith course in the order of their entry 39 | queuequ[5]; 40 | 41 | while(q--){ 42 | char ch; 43 | cin>>ch; 44 | if(ch=='E'){ 45 | ll x,r; 46 | s2(x,r); 47 | // Increase the overall timestamp 48 | tot[0]++; 49 | // Push the child in the xth course's queue 50 | qu[x].push(r); 51 | 52 | // If the student is the first student in the course, set the timestamp of the xth course to that of the overall time stamp 53 | if(tot[x]==-1) 54 | tot[x]=tot[0]; 55 | } 56 | else{ 57 | ll id=0; 58 | ll mini=INT_MAX; 59 | // Simply compute the course with the minimum timestamp 60 | for(ll i=1;i<=4;i++) 61 | if(tot[i]!=-1 and tot[i] 2 | #include 3 | using namespace std; 4 | #define fio ios_base::sync_with_stdio(false) 5 | 6 | #define ll long long int 7 | 8 | #define s(x) scanf("%lld",&x) 9 | #define s2(x,y) s(x)+s(y) 10 | #define s3(x,y,z) s(x)+s(y)+s(z) 11 | 12 | #define p(x) printf("%lld\n",x) 13 | #define p2(x,y) p(x)+p(y) 14 | #define p3(x,y,z) p(x)+p(y)+p(z) 15 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 16 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 17 | 18 | #define ff first 19 | #define ss second 20 | #define mp(x,y) make_pair(x,y) 21 | #define pll pair 22 | #define pb push_back 23 | 24 | ll inf = 1e18; 25 | ll mod = 1e9 + 7 ; 26 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 27 | 28 | /****************************************************************************/ 29 | 30 | int main() 31 | { 32 | freopen("input.txt","r",stdin); 33 | freopen("output.txt","w",stdout); 34 | ll t=1; 35 | ll tc=1; 36 | s(t); 37 | while(t--){ 38 | ll n; 39 | s(n); 40 | vector ar(n); 41 | F(i,0,n-1)cin>>ar[i]; 42 | if(n==2)cout<<"Case #"<dupAr(ar); 48 | sort(dupAr.begin(),dupAr.end()); 49 | ll dupLo=0; 50 | ll dupHi=n-1; 51 | ll st=(n-1)/2; 52 | bool fg=0; 53 | 54 | if(ar[st]==dupAr[dupLo])dupLo++; 55 | else if (ar[st]==dupAr[dupHi])dupHi--; 56 | else {cout<<"Case #"<=0 and hi2){ 60 | if(n%2 == 0){ 61 | ll nex = ar[hi]; 62 | if(nex==dupAr[dupLo])dupLo++; 63 | else if(nex == dupAr[dupHi])dupHi--; 64 | else {fg=1; break; } 65 | hi++; 66 | 67 | if(dupHi-dupLo+1 <=2 ) break; 68 | 69 | nex=ar[lo]; 70 | if(nex==dupAr[dupLo])dupLo++; 71 | else if(nex == dupAr[dupHi])dupHi--; 72 | else {fg=1; break; } 73 | lo--; 74 | }else{ 75 | 76 | ll nex=ar[lo]; 77 | if(nex==dupAr[dupLo])dupLo++; 78 | else if(nex == dupAr[dupHi])dupHi--; 79 | else {fg=1; break; } 80 | lo--; 81 | 82 | if(dupHi-dupLo+1 <=2 ) break; 83 | 84 | nex = ar[hi]; 85 | if(nex==dupAr[dupLo])dupLo++; 86 | else if(nex == dupAr[dupHi])dupHi--; 87 | else {fg=1; break; } 88 | hi++; 89 | } 90 | } 91 | if((dupHi-dupLo+1)==2)cout<<"Case #"< 2 | #include 3 | using namespace std; 4 | #define fio ios_base::sync_with_stdio(false) 5 | 6 | #define ll long long int 7 | 8 | #define s(x) scanf("%lld",&x) 9 | #define s2(x,y) s(x)+s(y) 10 | #define s3(x,y,z) s(x)+s(y)+s(z) 11 | 12 | #define p(x) printf("%lld\n",x) 13 | #define p2(x,y) p(x)+p(y) 14 | #define p3(x,y,z) p(x)+p(y)+p(z) 15 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 16 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 17 | 18 | #define ff first 19 | #define ss second 20 | #define mp(x,y) make_pair(x,y) 21 | #define pll pair 22 | #define pb push_back 23 | 24 | ll inf = 1e18; 25 | ll mod = 1e9 + 7 ; 26 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 27 | 28 | /**************************CODE GOES HERE*********************************************/ 29 | 30 | const int UP_LIM=5000; 31 | int cum_sum[5050]; 32 | 33 | void updateBIT(int ind,int val){ 34 | while(ind<=UP_LIM){ 35 | cum_sum[ind]+=val; 36 | ind+=ind&-ind; 37 | } 38 | } 39 | 40 | int queryBIT(int ind){ 41 | int ans=0; 42 | while(ind>0){ 43 | ans+=cum_sum[ind]; 44 | ind-=ind&-ind; 45 | } 46 | return ans; 47 | } 48 | 49 | int main() 50 | { 51 | freopen("input.txt","r",stdin); 52 | freopen("output.txt","w",stdout); 53 | ll t=1; 54 | ll tc=1; 55 | s(t); 56 | while(t--){ 57 | ll n; 58 | memset(cum_sum,0,sizeof(cum_sum)); 59 | s(n); 60 | F(i,1,n){ 61 | ll st,en; 62 | s2(st,en); 63 | updateBIT(st,1); 64 | updateBIT(en+1,-1); 65 | } 66 | ll p; 67 | s(p); 68 | cout<<"Case #"< 2 | #include 3 | using namespace std; 4 | #define fio ios_base::sync_with_stdio(false) 5 | 6 | #define ll long long int 7 | 8 | #define s(x) scanf("%lld",&x) 9 | #define s2(x,y) s(x)+s(y) 10 | #define s3(x,y,z) s(x)+s(y)+s(z) 11 | 12 | #define p(x) printf("%lld\n",x) 13 | #define p2(x,y) p(x)+p(y) 14 | #define p3(x,y,z) p(x)+p(y)+p(z) 15 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 16 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 17 | 18 | #define ff first 19 | #define ss second 20 | #define mp(x,y) make_pair(x,y) 21 | #define pll pair 22 | #define pb push_back 23 | 24 | ll inf = 1e18; 25 | ll mod = 1e9 + 7 ; 26 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 27 | 28 | /**************************CODE GOES HERE*********************************************/ 29 | 30 | ll helper(ll lev,ll k){ 31 | if(lev==1)return 0ll; 32 | ll totChars=pow(2,lev)-1ll; 33 | if(k==(totChars+1ll)/2)return 0; 34 | if(k<=totChars/2) return helper(lev-1ll,k); 35 | ll posInSecondHalf=k-((totChars+1ll)/2); 36 | ll temp = (totChars/2) - posInSecondHalf + 1ll; 37 | return 1-helper(lev-1ll,temp); 38 | } 39 | 40 | ll findKthChar(ll k){ 41 | if(k==1)return 0ll; 42 | ll power; 43 | for(power=1;power<=70;power++){ 44 | if(k<=(pow(2,power)-1ll))break; 45 | } 46 | return helper(power,k); 47 | } 48 | 49 | int main() 50 | { 51 | freopen("input.txt","r",stdin); 52 | freopen("output.txt","w",stdout); 53 | ll t=1; 54 | s(t); 55 | ll tc=1; 56 | while(t--){ 57 | ll k; 58 | s(k); 59 | ll ans=findKthChar(k); 60 | cout<<"Case #"< 2 | #include 3 | using namespace std; 4 | #define fio ios_base::sync_with_stdio(false) 5 | 6 | #define ll long long int 7 | 8 | #define s(x) scanf("%lld",&x) 9 | #define s2(x,y) s(x)+s(y) 10 | #define s3(x,y,z) s(x)+s(y)+s(z) 11 | 12 | #define p(x) printf("%lld\n",x) 13 | #define p2(x,y) p(x)+p(y) 14 | #define p3(x,y,z) p(x)+p(y)+p(z) 15 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 16 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 17 | 18 | #define ff first 19 | #define ss second 20 | #define mp(x,y) make_pair(x,y) 21 | #define pll pair 22 | #define pb push_back 23 | 24 | ll inf = 1e18; 25 | ll mod = 1e9 + 7 ; 26 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 27 | 28 | /**************************CODE GOES HERE*********************************************/ 29 | 30 | int main() 31 | { 32 | freopen("input.txt","r",stdin); 33 | freopen("output.txt","w",stdout); 34 | ll t=1; 35 | ll tc=1; 36 | s(t); 37 | while(t--){ 38 | ll n; 39 | s(n); 40 | unordered_mapump_places; 41 | unordered_mapump_count; 42 | 43 | F(i,1,n){ 44 | string u,v; 45 | cin>>u>>v; 46 | ump_places[u]=v; 47 | ump_count[u]++; 48 | ump_count[v]++; 49 | } 50 | unordered_map::iterator it=ump_count.begin(); 51 | string source,dest; 52 | while(it!=ump_count.end()){ 53 | string city=it->first; 54 | ll count=it->second; 55 | if(count==1){ 56 | if(ump_places.find(city)==ump_places.end())dest=city; 57 | else source=city; 58 | } 59 | it++; 60 | } 61 | cout<<"Case #"< 3 | // #include 4 | using namespace std; 5 | #define fio ios_base::sync_with_stdio(false) 6 | 7 | #define ll long long int 8 | 9 | #define s(x) scanf("%lld",&x) 10 | #define s2(x,y) s(x)+s(y) 11 | #define s3(x,y,z) s(x)+s(y)+s(z) 12 | 13 | #define p(x) printf("%lld\n",x) 14 | #define p2(x,y) p(x)+p(y) 15 | #define p3(x,y,z) p(x)+p(y)+p(z) 16 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 17 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 18 | 19 | #define ff first 20 | #define ss second 21 | #define mp(x,y) make_pair(x,y) 22 | #define pll pair 23 | #define pb push_back 24 | 25 | ll inf = 1e18; 26 | ll mod = 1e9 + 7 ; 27 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 28 | ll x,y,n,w; 29 | double p1,p2; 30 | double dp[1005][1005]; 31 | 32 | double solve(ll n, ll w){ 33 | // If w is less than 0 , then we need not shoot any bird and we have already won, so return 1.0 34 | if(w<=0)return 1.0; 35 | 36 | // If w is not less than or equal to 0 and we also don't have any coins left, we can't win the match, so return 0.0 37 | if(n<=0)return 0.0; 38 | 39 | if(dp[n][w]>=0.0)return dp[n][w]; 40 | 41 | // Either we kill the bird 1 with p1 percentage and try to obtain the remaining w-x points with n-1 coins 42 | // or we miss the shot with 1-p1 percentage and try to obtain the remaining w points with n-1 coins. 43 | double c1 = p1*solve(n-1,w-x) + (1-p1)*solve(n-1,w); 44 | 45 | // Same as above 46 | double c2 = p2*solve(n-1,w-y) + (1-p2)*solve(n-1,w); 47 | 48 | // Return the maximum of the two 49 | return dp[n][w]=max(c1,c2); 50 | } 51 | 52 | int main() 53 | { 54 | // freopen("input.txt","r",stdin); 55 | // freopen("output.txt","w",stdout); 56 | ll t=1; 57 | s(t); 58 | while(t--){ 59 | ll p1,p2; 60 | cin>>x>>y>>n>>w>>p1>>p2; 61 | ::p1=(double)p1/100.0; 62 | ::p2=(double)p2/100.0; 63 | memset(dp,-1.0,sizeof(dp)); 64 | double ans = solve(n,w)*100.0; 65 | printf("%.6f\n",ans); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /KMP.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | using namespace std; 5 | #define fio ios_base::sync_with_stdio(false) 6 | 7 | #define ll long long int 8 | 9 | #define s(x) scanf("%lld",&x) 10 | #define s2(x,y) s(x)+s(y) 11 | #define s3(x,y,z) s(x)+s(y)+s(z) 12 | 13 | #define p(x) printf("%lld\n",x) 14 | #define p2(x,y) p(x)+p(y) 15 | #define p3(x,y,z) p(x)+p(y)+p(z) 16 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 17 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 18 | 19 | #define ff first 20 | #define ss second 21 | #define mp(x,y) make_pair(x,y) 22 | #define pll pair 23 | #define pb push_back 24 | 25 | ll inf = 1e18; 26 | ll mod = 1e9 + 7 ; 27 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 28 | 29 | vector preComputeLPS(string text){ 30 | int sz = text.size(); 31 | vector lps; 32 | if(sz==0)return lps; 33 | lps = vector(sz+1,0); 34 | 35 | int len=0; 36 | lps[0]=0; 37 | int i=1; 38 | while(i lps = preComputeLPS(pat); 57 | int i=0; 58 | int j=0; 59 | int sz = text.size(); 60 | int pat_sz = pat.size(); 61 | while(i0)j=lps[j-1]; 73 | else i++; 74 | } 75 | } 76 | cout<>text>>pat; 88 | findOccurenceByKMP(text,pat); 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /LIMITED BUDGET PARTY - ARRAY - TWO POINTERS - HACKERBLOCKS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | 26 | using namespace std; 27 | 28 | #define ll long long int 29 | 30 | #define s(x) scanf("%lld",&x) 31 | #define s2(x,y) s(x)+s(y) 32 | #define s3(x,y,z) s(x)+s(y)+s(z) 33 | 34 | #define p(x) printf("%lld\n",x) 35 | #define p2(x,y) p(x)+p(y) 36 | #define p3(x,y,z) p(x)+p(y)+p(z) 37 | #define fori(i,max) for(ll i=0;i<(ll)(max) ;(i)++) 38 | #define for2i(i,min,max) for(ll i=min;i<(ll)(max) ;(i)++) 39 | 40 | int main() 41 | { 42 | // freopen("input.txt","r",stdin); 43 | // freopen("output.txt","w",stdout); 44 | ll t; 45 | s(t); 46 | while(t--) 47 | { 48 | ll n,x; 49 | s2(n,x); 50 | ll *a=new ll[n+1]; 51 | 52 | fori(i,n) 53 | { 54 | s(a[i]); 55 | } 56 | bool flag=0; 57 | 58 | // left counter 59 | ll c1=0; 60 | 61 | // right counter 62 | ll c2=0; 63 | 64 | // sum contains the sum of all the array elements between left and right counter index 65 | ll sum=0; 66 | 67 | for(ll i=0;ix) 79 | { 80 | while(sum>=x && c1<=c2) 81 | { 82 | 83 | // All elements removed. Reset everything 84 | if(c1==c2) 85 | { 86 | sum=0; 87 | c1++; 88 | c2++; 89 | break; 90 | } 91 | 92 | // Remove the last element from the segment 93 | sum -= a[c1]; 94 | // Increment the last pointer 95 | c1++ ; 96 | 97 | if(sum==x) 98 | { 99 | flag=1;break; 100 | } 101 | } 102 | if(flag==1)break; 103 | } 104 | 105 | } 106 | if(sum 4 | #include 5 | using namespace std; 6 | #define fio ios_base::sync_with_stdio(false) 7 | 8 | #define ll long long int 9 | 10 | #define s(x) scanf("%lld",&x) 11 | #define s2(x,y) s(x)+s(y) 12 | #define s3(x,y,z) s(x)+s(y)+s(z) 13 | 14 | #define p(x) printf("%lld\n",x) 15 | #define p2(x,y) p(x)+p(y) 16 | #define p3(x,y,z) p(x)+p(y)+p(z) 17 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 18 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 19 | 20 | #define ff first 21 | #define ss second 22 | #define mp(x,y) make_pair(x,y) 23 | #define pll pair 24 | #define pb push_back 25 | 26 | ll inf = 1e18; 27 | ll mod = 1e9 + 7 ; 28 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 29 | 30 | ll dp[101][101][101]={0}; 31 | 32 | int main() 33 | { 34 | // freopen("input.txt","r",stdin); 35 | // freopen("output.txt","w",stdout); 36 | ll t=1; 37 | //s(t); 38 | while(t--){ 39 | ll n; 40 | s(n); 41 | ll rd[201]={0}; 42 | ll bl[201]={0}; 43 | 44 | F(i,1,n){ 45 | string str; 46 | cin>>str; 47 | for(char ch:str){ 48 | if(ch=='R')rd[i]++; 49 | else bl[i]++; 50 | } 51 | } 52 | 53 | 54 | // dp[i][j][k] = maximum rows upto the ith row which can be mimicked if the person has 55 | // j red and k blue balls. 56 | // Concept is very simple: Either we mimick a row or we don't mimick a row, take the maximum one. 57 | 58 | for(ll i=1;i<=n;i++){ 59 | for(ll x=0;x<=100;x++){ 60 | for(ll y=0;y<=100;y++){ 61 | if(i==1){ 62 | if(x>=rd[i] and y>=bl[i])dp[i][x][y]=1; 63 | }else{ 64 | dp[i][x][y]=dp[i-1][x][y]; 65 | if(x>=rd[i] and y>=bl[i])dp[i][x][y]=max(dp[i][x][y],1+dp[i-1][x-rd[i]][y-bl[i]]); 66 | } 67 | } 68 | } 69 | } 70 | 71 | ll q; 72 | s(q); 73 | while(q--){ 74 | ll x,y; 75 | cin>>x>>y; 76 | cout< 4 | #include 5 | using namespace std; 6 | #define fio ios_base::sync_with_stdio(false) 7 | 8 | #define ll long long int 9 | 10 | #define s(x) scanf("%lld",&x) 11 | #define s2(x,y) s(x)+s(y) 12 | #define s3(x,y,z) s(x)+s(y)+s(z) 13 | 14 | #define p(x) printf("%lld\n",x) 15 | #define p2(x,y) p(x)+p(y) 16 | #define p3(x,y,z) p(x)+p(y)+p(z) 17 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 18 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 19 | 20 | #define ff first 21 | #define ss second 22 | #define mp(x,y) make_pair(x,y) 23 | #define pll pair 24 | #define pb push_back 25 | 26 | ll inf = 1e18; 27 | ll mod = 1e9 + 7 ; 28 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 29 | 30 | /****************************************************************************/ 31 | 32 | ll fun(ll a,ll b,ll k){ 33 | ll ret=0ll; 34 | ll cumulative_sum=1ll; 35 | ll sum=1ll; 36 | ll mul=k; 37 | while(sum<=a){ 38 | if((sum+mul)>a)break; 39 | sum+=mul; 40 | cumulative_sum+=sum; 41 | mul*=k; 42 | cumulative_sum%=mod; 43 | } 44 | ret+=(b-a+1ll)*cumulative_sum; 45 | ret%=mod; 46 | sum+=mul; 47 | while(sum<=b){ 48 | ret+=((b-sum+1ll)%mod)*(sum%mod); 49 | ret%=mod; 50 | mul=mul*k; 51 | sum+=mul; 52 | } 53 | return ret; 54 | } 55 | 56 | int main() 57 | { 58 | freopen("input.txt","r",stdin); 59 | // freopen("output.txt","w",stdout); 60 | ll t=1; 61 | s(t); 62 | while(t--){ 63 | ll a,b,k; 64 | s3(k,a,b); 65 | cout< 4 | #include 5 | using namespace std; 6 | #define fio ios_base::sync_with_stdio(false) 7 | 8 | #define ll long long int 9 | 10 | #define s(x) scanf("%lld",&x) 11 | #define s2(x,y) s(x)+s(y) 12 | #define s3(x,y,z) s(x)+s(y)+s(z) 13 | 14 | #define p(x) printf("%lld\n",x) 15 | #define p2(x,y) p(x)+p(y) 16 | #define p3(x,y,z) p(x)+p(y)+p(z) 17 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 18 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 19 | 20 | #define ff first 21 | #define ss second 22 | #define mp(x,y) make_pair(x,y) 23 | #define pll pair 24 | #define pb push_back 25 | 26 | ll inf = 1e18; 27 | ll mod = 1e9 + 7 ; 28 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 29 | 30 | /****************************************************************************/ 31 | 32 | ll modExp(ll a,ll e,ll mod){ 33 | if(e==0)return 1ll; 34 | if(a==0) return 0ll; 35 | 36 | ll pro=1; 37 | while(e>0){ 38 | if(e&1)pro=(pro*a)%mod; 39 | a=(a*a)%mod; 40 | e=e/2; 41 | } 42 | return pro; 43 | } 44 | 45 | int main() 46 | { 47 | freopen("input.txt","r",stdin); 48 | freopen("output1.txt","w",stdout); 49 | ll t=1; 50 | s(t); 51 | while(t--){ 52 | ll a,n,pp; 53 | s3(a,n,pp); 54 | ll ans = a; 55 | F(i,1,n)ans=modExp(ans,i,pp); 56 | cout< 4 | #include 5 | using namespace std; 6 | #define fio ios_base::sync_with_stdio(false) 7 | 8 | #define ll long long int 9 | 10 | #define s(x) scanf("%lld",&x) 11 | #define s2(x,y) s(x)+s(y) 12 | #define s3(x,y,z) s(x)+s(y)+s(z) 13 | 14 | #define p(x) printf("%lld\n",x) 15 | #define p2(x,y) p(x)+p(y) 16 | #define p3(x,y,z) p(x)+p(y)+p(z) 17 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 18 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 19 | 20 | #define ff first 21 | #define ss second 22 | #define mp(x,y) make_pair(x,y) 23 | #define pll pair 24 | #define pb push_back 25 | 26 | ll inf = 1e18; 27 | ll mod = 1e9 + 7 ; 28 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 29 | 30 | /****************************************************************************/ 31 | 32 | vector> grid; 33 | ll dp[50][50][50][50]; 34 | 35 | ll findMaxCoins(ll sr,ll er,ll sc,ll ec){ 36 | if(sr>er or sc>ec)return 0; 37 | if(sr==er and sc==ec) return 0; 38 | if(dp[sr][er][sc][ec]!=-1)return dp[sr][er][sc][ec]; 39 | 40 | ll mini=(ll)INT_MAX; 41 | F(r,sr,er){ 42 | F(c,sc,ec){ 43 | mini=min(mini,grid[r][c]); 44 | } 45 | } 46 | 47 | ll ans=mini; 48 | F(r,sr,er-1) ans=max(ans,mini+findMaxCoins(sr,r,sc,ec)+findMaxCoins(r+1,er,sc,ec)); 49 | F(c,sc,ec-1) ans=max(ans,mini+findMaxCoins(sr,er,sc,c)+findMaxCoins(sr,er,c+1,ec)); 50 | 51 | return dp[sr][er][sc][ec]=ans; 52 | } 53 | 54 | int main() 55 | { 56 | freopen("input.txt","r",stdin); 57 | freopen("output.txt","w",stdout); 58 | ll t=1; 59 | ll tc=1; 60 | s(t); 61 | while(t--){ 62 | grid.clear(); 63 | memset(dp,-1,sizeof(dp)); 64 | ll r,c; 65 | s2(r,c); 66 | grid=vector>(r+1,vector(c+1,0)); 67 | F(i,1,r) 68 | F(j,1,c) 69 | s(grid[i][j]); 70 | 71 | ll ans = findMaxCoins(1,r,1,c); 72 | cout<<"Case #"< 3 | #include 4 | using namespace std; 5 | #define fio ios_base::sync_with_stdio(false) 6 | 7 | #define ll long long int 8 | 9 | #define s(x) scanf("%lld",&x) 10 | #define s2(x,y) s(x)+s(y) 11 | #define s3(x,y,z) s(x)+s(y)+s(z) 12 | 13 | #define p(x) printf("%lld\n",x) 14 | #define p2(x,y) p(x)+p(y) 15 | #define p3(x,y,z) p(x)+p(y)+p(z) 16 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 17 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 18 | 19 | #define ff first 20 | #define ss second 21 | #define mp(x,y) make_pair(x,y) 22 | #define pll pair 23 | #define pb push_back 24 | 25 | 26 | ll inf = 1e18; 27 | ll mod = 1e9 + 7 ; 28 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 29 | 30 | int main() 31 | { 32 | freopen("input.txt","r",stdin); 33 | freopen("output.txt","w",stdout); 34 | ll t=1; 35 | // s(t); 36 | while(t--){ 37 | ll r,c; 38 | s2(r,c); 39 | vector>mat(r,vector(c,0)); 40 | F(i,0,r-1){ 41 | F(j,0,c-1){ 42 | bool a; 43 | cin>>a; 44 | mat[i][j]=a; 45 | } 46 | } 47 | 48 | // Flip that row whose most adjacent bit is set to zero 49 | F(i,0,r-1){ 50 | if(mat[i][0]==0)mat[i].flip(); 51 | } 52 | // Since all bits of first column are set to 1, add it to the overall sum+= r x pow(2,c-1). 53 | ll ans=r*(1ll<<(c-1ll)); 54 | 55 | F(j,1,c-1){ 56 | ll ct=0; 57 | // Now compute the number of 1s and 0s in every column and flip it if 0s exceed 1s. 58 | F(i,0,r-1){ 59 | if(mat[i][j]==1)ct++; 60 | } 61 | // Here the flip takes place (only logically, not the actual flip operation as it's not needed) 62 | ct=max(ct,r-ct); 63 | 64 | // Now add it to the overall sum , note that any bit at the jth columns will be the c-1-j th bit in the overall number 65 | ans+=ct*(1ll<<(c-1ll-j)); 66 | } 67 | p(ans); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /MATRIX SEARCH - LINEAR SEARCH - CODING BLOCKS.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | using namespace std; 5 | #define fio ios_base::sync_with_stdio(false) 6 | 7 | #define ll long long int 8 | 9 | #define s(x) scanf("%lld",&x) 10 | #define s2(x,y) s(x)+s(y) 11 | #define s3(x,y,z) s(x)+s(y)+s(z) 12 | 13 | #define p(x) printf("%lld\n",x) 14 | #define p2(x,y) p(x)+p(y) 15 | #define p3(x,y,z) p(x)+p(y)+p(z) 16 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 17 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 18 | 19 | #define ff first 20 | #define ss second 21 | #define mp(x,y) make_pair(x,y) 22 | #define pll pair 23 | #define pb push_back 24 | 25 | ll inf = 1e18; 26 | ll mod = 1e9 + 7 ; 27 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 28 | 29 | int main() 30 | { 31 | freopen("input.txt","r",stdin); 32 | // freopen("output.txt","w",stdout); 33 | ll t=1; 34 | // s(t); 35 | while(t--){ 36 | ll r,c; 37 | s2(r,c); 38 | 39 | // Accept the input in the matrix 40 | vector>mat; 41 | mat.resize(r); 42 | for(ll i=0;i>mat[i][j]; 46 | } 47 | } 48 | 49 | // Accept the input 50 | ll num; 51 | cin>>num; 52 | 53 | // Initially begin searching from right top corner 54 | ll row,col; 55 | row=0; 56 | col=c-1; 57 | 58 | // This flag indicates if the number has been found or not 59 | bool fg=0; 60 | 61 | // This loop will run untill we remain in the bounds of the matrix 62 | while(row=0){ 63 | // If the number is found, set the flag and break 64 | if(mat[row][col]==num){ 65 | fg=1; 66 | break; 67 | } 68 | // If the matrix element is greater than number, we must check in the previous column as that 69 | // will contain lower numbers. 70 | if(mat[row][col]>num)col--; 71 | else row++; // Else check in the next row which will contain higher numbers. 72 | } 73 | 74 | if(fg)cout<<"1"< 4 | #include 5 | using namespace std; 6 | #define fio ios_base::sync_with_stdio(false) 7 | 8 | #define ll long long int 9 | 10 | #define s(x) scanf("%lld",&x) 11 | #define s2(x,y) s(x)+s(y) 12 | #define s3(x,y,z) s(x)+s(y)+s(z) 13 | 14 | #define p(x) printf("%lld\n",x) 15 | #define p2(x,y) p(x)+p(y) 16 | #define p3(x,y,z) p(x)+p(y)+p(z) 17 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 18 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 19 | 20 | #define ff first 21 | #define ss second 22 | #define mp(x,y) make_pair(x,y) 23 | #define pll pair 24 | #define pb push_back 25 | 26 | ll inf = 1e18; 27 | ll mod = 1e9 + 7 ; 28 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 29 | 30 | /**************************CODE GOES HERE*********************************************/ 31 | vectorar; 32 | unordered_map resmp; 33 | vector> queries; 34 | ll freq[1000000 + 5]; 35 | ll n; 36 | ll sqrtn; 37 | 38 | bool comp(pair a,pair b){ 39 | if(a.first.first/sqrtn == b.first.first/sqrtn)return a.first.secondlf){ 85 | start--; 86 | freq[ar[start]]++; 87 | if(freq[ar[start]]==1)ct++; 88 | } 89 | 90 | while(startrt){ 103 | freq[ar[end]]--; 104 | if(freq[ar[end]]==0)ct--; 105 | end--; 106 | } 107 | resmp[ind]=ct; 108 | } 109 | F(i,1,q)p(resmp[i]); 110 | } 111 | return 0; 112 | } -------------------------------------------------------------------------------- /MODULO SUM- CODING BLOCKS - DP(1).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | // #include 3 | using namespace std; 4 | #define fio ios_base::sync_with_stdio(false) 5 | 6 | #define ll long long int 7 | 8 | #define s(x) scanf("%lld",&x) 9 | #define s2(x,y) s(x)+s(y) 10 | #define s3(x,y,z) s(x)+s(y)+s(z) 11 | 12 | #define p(x) printf("%lld\n",x) 13 | #define p2(x,y) p(x)+p(y) 14 | #define p3(x,y,z) p(x)+p(y)+p(z) 15 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 16 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 17 | 18 | #define ff first 19 | #define ss second 20 | #define mp(x,y) make_pair(x,y) 21 | #define pll pair 22 | #define pb push_back 23 | 24 | ll inf = 1e18; 25 | ll mod = 1e9 + 7 ; 26 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 27 | 28 | map,bool> dp; 29 | 30 | // Returns the result if there is a nonempty set of elements whose sum%m is zero 31 | bool isPossible(vector &vec, ll m, ll ind, ll modulo){ 32 | // base case : if we have reached the end, we will see if the modulo value is zero or not 33 | // Note that the modulo will contain the remainder obtained after dividing the sum of all 34 | // the elements considered so far by m. 35 | if(ind>=vec.size())return modulo==0; 36 | 37 | // Return the solution if already calculated 38 | if(dp.find({ind,modulo})!=dp.end())return dp[{ind,modulo}]; 39 | 40 | // If by adding the current element in the overall sum, we attain modulo zero , return success. 41 | if(vec[ind]%m == m-modulo)return dp[{ind,modulo}]=true; 42 | 43 | // Else consider the two options, either add this element in the overall sum or don't add. 44 | return dp[{ind,modulo}]= isPossible(vec,m,ind+1,modulo) or isPossible(vec,m,ind+1,(modulo+vec[ind])%m); 45 | } 46 | 47 | int main() 48 | { 49 | // freopen("input.txt","r",stdin); 50 | // freopen("output.txt","w",stdout); 51 | ll t=1; 52 | //s(t); 53 | while(t--){ 54 | ll n,m; 55 | s2(n,m); 56 | vectorvec; 57 | F(i,1,n){ 58 | ll num; 59 | cin>>num; 60 | vec.push_back(num); 61 | } 62 | if(isPossible(vec,m,0,m))cout<<"YES\n"; 63 | else cout<<"NO\n"; 64 | } 65 | } -------------------------------------------------------------------------------- /MONEY CHANGE - DP (EASY) - CODING BLOCKS.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | using namespace std; 5 | #define fio ios_base::sync_with_stdio(false) 6 | 7 | #define ll long long int 8 | 9 | #define s(x) scanf("%lld",&x) 10 | #define s2(x,y) s(x)+s(y) 11 | #define s3(x,y,z) s(x)+s(y)+s(z) 12 | 13 | #define p(x) printf("%lld\n",x) 14 | #define p2(x,y) p(x)+p(y) 15 | #define p3(x,y,z) p(x)+p(y)+p(z) 16 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 17 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 18 | 19 | #define ff first 20 | #define ss second 21 | #define mp(x,y) make_pair(x,y) 22 | #define pll pair 23 | #define pb push_back 24 | 25 | ll inf = 1e18; 26 | ll mod = 1e9 + 7 ; 27 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 28 | 29 | int main() 30 | { 31 | // freopen("input.txt","r",stdin); 32 | // freopen("output.txt","w",stdout); 33 | ll t=1; 34 | s(t); 35 | while(t--){ 36 | ll n; 37 | s(n); 38 | vector coins(n); 39 | F(i,1,n)cin>>coins[i-1]; 40 | ll N; 41 | s(N); 42 | 43 | // dp[i][j] represents the number of ways to make change for j cents using coins[0] to coins[i] denominations. 44 | vector> dp(n,vector(N+1,0)); 45 | F(i,0,n-1)dp[i][0]=1ll; 46 | F(j,1,N){ 47 | F(i,0,n-1){ 48 | // Option 1: You dont't take that denomination 49 | if(i>0)dp[i][j]=dp[i-1][j]; 50 | if(coins[i]<=j){ 51 | // Option 2: You take the denomination 52 | dp[i][j]+=dp[i][j-coins[i]]; // Total number of ways is the sum of both these options 53 | dp[i][j]%=mod; 54 | } 55 | } 56 | } 57 | p(dp[n-1][N]); 58 | } 59 | } 60 | 61 | -------------------------------------------------------------------------------- /MURTHAL PARANTHA - BINARY SEARCH - CODING BLOCKS.cpp: -------------------------------------------------------------------------------- 1 | // http://www.spoj.com/problems/PRATA/ 2 | 3 | #include 4 | #include 5 | using namespace std; 6 | #define fio ios_base::sync_with_stdio(false) 7 | 8 | #define ll long long int 9 | 10 | #define s(x) scanf("%lld",&x) 11 | #define s2(x,y) s(x)+s(y) 12 | #define s3(x,y,z) s(x)+s(y)+s(z) 13 | 14 | #define p(x) printf("%lld\n",x) 15 | #define p2(x,y) p(x)+p(y) 16 | #define p3(x,y,z) p(x)+p(y)+p(z) 17 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 18 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 19 | 20 | #define ff first 21 | #define ss second 22 | #define mp(x,y) make_pair(x,y) 23 | #define pll pair 24 | #define pb push_back 25 | 26 | ll inf = 1e18; 27 | ll mod = 1e9 + 7 ; 28 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 29 | 30 | 31 | bool isPossible(vector &rnk, ll minTime,ll paranthas){ 32 | ll n=rnk.size(); 33 | for(ll i=0;i0){ 37 | tm+=ct*rnk[i]; 38 | paranthas--; 39 | ct++; 40 | } 41 | if(paranthas==0)return true; 42 | } 43 | return (paranthas==0); 44 | } 45 | 46 | int main() 47 | { 48 | // freopen("input.txt","r",stdin); 49 | // freopen("output.txt","w",stdout); 50 | ll t=1; 51 | // s(t); 52 | while(t--){ 53 | 54 | // Accept the number of paranthas to be made 55 | ll par; 56 | s(par); 57 | ll n; 58 | s(n); 59 | 60 | // Accept the rank of cooks at various stalls 61 | vector ran(n); 62 | F(i,0,n-1)cin>>ran[i]; 63 | 64 | // Here we will be binary searching on the time needed to cook the given paranthas 65 | // lo and hi are initialised the logically minimum and maximum value respectively 66 | ll lo=0; 67 | ll hi=1e18; 68 | 69 | // ans stores the minimum amount of time needed to cook the paranthas 70 | // ans is initialised to some logically maximum value 71 | ll ans=1e18; 72 | 73 | while(lo<=hi){ 74 | ll mid=lo+hi; 75 | mid>>=1; 76 | // if it is possible to cook the paranthas in "mid" units of time, then use it for computing the ans and 77 | // again start trying for smaller values by reducing hi to mid-1 78 | if(isPossible(ran,mid,par)){ 79 | ans=min(ans,mid); 80 | hi=mid-1; 81 | }else { 82 | // Else search for larger units of time 83 | lo=mid+1; 84 | } 85 | } 86 | p(ans); 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /Neighbouring Enemy -DP(Easy) -CODING BLOCKS.cpp: -------------------------------------------------------------------------------- 1 | // http://codeforces.com/contest/455/problem/A 2 | 3 | // SPACE EFFICIENT APPROACH 4 | // FOR EASIER BUT SPACE COSTLY APPROACH, LOOK FOR THE PREVIOUS SUBMISSION FOR THE SAME PROBLEM 5 | 6 | #include 7 | #include 8 | using namespace std; 9 | #define fio ios_base::sync_with_stdio(false) 10 | 11 | #define ll long long int 12 | 13 | #define s(x) scanf("%lld",&x) 14 | #define s2(x,y) s(x)+s(y) 15 | #define s3(x,y,z) s(x)+s(y)+s(z) 16 | 17 | #define p(x) printf("%lld\n",x) 18 | #define p2(x,y) p(x)+p(y) 19 | #define p3(x,y,z) p(x)+p(y)+p(z) 20 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 21 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 22 | 23 | #define ff first 24 | #define ss second 25 | #define mp(x,y) make_pair(x,y) 26 | #define pll pair 27 | #define pb push_back 28 | 29 | ll inf = 1e18; 30 | ll mod = 1e9 + 7 ; 31 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 32 | 33 | int main() 34 | { 35 | // freopen("input.txt","r",stdin); 36 | freopen("output.txt","w",stdout); 37 | ll t=1; 38 | // s(t); 39 | while(t--){ 40 | ll n; 41 | s(n); 42 | vectorb; 43 | mapmp; 44 | ll num; 45 | F(i,0,n-1){cin>>num; mp[num]++;} 46 | for(auto ele:mp){ 47 | b.push_back(ele.first); 48 | } 49 | 50 | n=b.size(); 51 | vectordp(n,0); 52 | dp[0]=b[0]*mp[b[0]]; 53 | 54 | for(ll i=1;i=2) temp+=dp[i-2]; 58 | dp[i]=max(dp[i-1],temp); 59 | }else dp[i]=dp[i-1]+b[i]*mp[b[i]]; 60 | } 61 | cout< 3 | using namespace std; 4 | #define fio ios_base::sync_with_stdio(false) 5 | 6 | #define ll long long int 7 | 8 | #define s(x) scanf("%lld",&x) 9 | #define s2(x,y) s(x)+s(y) 10 | #define s3(x,y,z) s(x)+s(y)+s(z) 11 | 12 | #define p(x) printf("%lld\n",x) 13 | #define p2(x,y) p(x)+p(y) 14 | #define p3(x,y,z) p(x)+p(y)+p(z) 15 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 16 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 17 | 18 | #define ff first 19 | #define ss second 20 | #define mp(x,y) make_pair(x,y) 21 | #define pll pair 22 | #define pb push_back 23 | 24 | ll inf = 1e18; 25 | ll mod = 1e9 + 7 ; 26 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 27 | 28 | int main() 29 | { 30 | // freopen("input.txt","r",stdin); 31 | // freopen("output.txt","w",stdout); 32 | ll n; 33 | ll ct=0; 34 | ll t; 35 | s(t); 36 | while(t--){ 37 | cin>>n; 38 | ct++; 39 | // Accept the input in a vector 40 | vectorar(n); 41 | for(ll i=0;i>ar[i]; 42 | 43 | // Sort the vector in ascending order 44 | sort(ar.begin(), ar.end()); 45 | 46 | // Accept the money Deepak has 47 | ll tar; 48 | s(tar); 49 | 50 | // Since in cases of multiple solutions, we have to output that one which 51 | // has the minimum difference betweeen the two prices of the roses, so we 52 | // are maintaining the difference for every solution and will output the one 53 | // with minimum difference. Initially, the minimum difference is assumed to be 54 | // infinity. 55 | 56 | ll curDif=INT_MAX; 57 | ll p1,p2; 58 | 59 | for(ll i=0;itar)break; 61 | 62 | // toFind is the value of the price of the second rose in the pair 63 | ll tofind=tar-ar[i]; 64 | 65 | // ind is the index of the toFind variable in the sorted array if it exists 66 | ll ind = lower_bound(ar.begin()+i+1,ar.end(),tofind)-ar.begin(); 67 | 68 | // if ind is greater than n, it indicates that the second rose of the pair 69 | // does not exists, so continue finding another pair. 70 | if(ind>=n)continue; 71 | 72 | // if we have found the second rose of the pair, we will check for the minimum difference condition 73 | // and then assign the value of the two prices in p1 and p2. 74 | if(ar[ind]==tofind){ 75 | if(ar[ind]-ar[i] 3 | #include 4 | using namespace std; 5 | 6 | #define ll long long int 7 | 8 | #define s(x) scanf("%lld",&x) 9 | #define s2(x,y) s(x)+s(y) 10 | #define s3(x,y,z) s(x)+s(y)+s(z) 11 | 12 | #define p(x) printf("%lld\n",x) 13 | #define p2(x,y) p(x)+p(y) 14 | #define p3(x,y,z) p(x)+p(y)+p(z) 15 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 16 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 17 | 18 | #define ff first 19 | #define ss second 20 | #define mp(x,y) make_pair(x,y) 21 | #define pll pair 22 | #define pb push_back 23 | ll mod = 1e9 + 7 ; 24 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 25 | 26 | int main(){ 27 | // freopen("input.txt","r",stdin); 28 | // freopen("output.txt","w",stdout); 29 | 30 | ll n; 31 | s(n); 32 | vectorar(n+1); 33 | F(i,1,n)s(ar[i]); 34 | 35 | // l2r[] arrays stores the value of x for every i as stated in the question 36 | vectorl2r(n+1); 37 | // r2l[] arrays stores the value of y for every i as stated in the question 38 | vectorr2l(n+1); 39 | // st is a stack that stores a pair of numbers ({Number, Index of the number}) 40 | // such that at any moment the stack top will contain a pair of numbers such that 41 | // the second number in the pair is the largest number smaller than i 42 | // in case of l2r[] and smallest number larger than i in case of r2l[] and the first 43 | // number in the pair is greater than ar[i]. 44 | stack st; 45 | 46 | // Base case for l2r[] 47 | l2r[1]=-1; 48 | st.push({ar[1],1}); 49 | 50 | for(ll i=2;i<=n;i++){ 51 | // keep popping out unless the stack top's pair's first number is larger than ar[i] 52 | while(!st.empty() and st.top().ff<=ar[i])st.pop(); 53 | 54 | // If stack is empty, simply means there is no possible value of x, so make it -1. 55 | if(st.empty())l2r[i]=-1; 56 | // Else y is the index which is stored as the second number of the pair 57 | else l2r[i]=st.top().ss; 58 | 59 | // Now push the current element 60 | st.push({ar[i],i}); 61 | } 62 | while(!st.empty())st.pop(); 63 | 64 | // Base case for r2l[] 65 | r2l[n]=-1; 66 | st.push({ar[n],n}); 67 | for(ll i=n-1;i>0;i--){ 68 | 69 | // keep popping out unless the stack top's pair's first number is larger than ar[i] 70 | while(!st.empty() and st.top().ff<=ar[i])st.pop(); 71 | 72 | // If stack is empty, simply means there is no possible value of x, so make it -1. 73 | if(st.empty())r2l[i]=-1; 74 | // Else y is the index which is stored as the second number of the pair 75 | else r2l[i]=st.top().ss; 76 | // Now push the current element 77 | st.push({ar[i],i}); 78 | } 79 | // Now l2r[] and r2l[] respectively stores the values of x and y respectively for each i 80 | for(ll i=1;i<=n;i++)cout< 4 | #include 5 | using namespace std; 6 | 7 | #define ll long long in 8 | 9 | #define s(x) scanf("%lld",&x) 10 | #define s2(x,y) s(x)+s(y) 11 | #define s3(x,y,z) s(x)+s(y)+s(z) 12 | 13 | #define p(x) printf("%lld\n",x) 14 | #define p2(x,y) p(x)+p(y) 15 | #define p3(x,y,z) p(x)+p(y)+p(z) 16 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 17 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 18 | 19 | #define ff first 20 | #define ss second 21 | #define mp(x,y) make_pair(x,y) 22 | #define pll pair 23 | #define pb push_back 24 | 25 | ll mod = 1e9 + 7 ; 26 | ll inf = 1e18 ; 27 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 28 | 29 | int main() 30 | { 31 | // freopen("input.txt","r",stdin); 32 | // freopen("output.txt","w",stdout); 33 | ll t; 34 | cin>>t; 35 | while(t--){ 36 | ll n,m,k; 37 | cin>>n>>m>>k; 38 | vectorK(n),C(m),Z(m); 39 | 40 | // Chose this data type as we need atleast 100 bits to keep track of 100 possible chests 41 | __int128_t one=1; 42 | 43 | // key[i] will contain the bit representation of the chests ith key can open 44 | vector<__int128_t>key(n,0); 45 | 46 | for(ll i=0;i>K[i]; 47 | for(ll i=0;i>C[i]; 48 | for(ll i=0;i>Z[i]; 49 | 50 | for(ll i=0;i1){ 53 | key[i]|=one< k)continue; 61 | 62 | // 'chests' contain the bit representation of the chests which K keys 63 | // (set bits in the mask)can together open. 64 | __int128_t chests=0; 65 | for(ll j=0;j 3 | #include 4 | using namespace std; 5 | #define fio ios_base::sync_with_stdio(false) 6 | 7 | #define ll long long int 8 | 9 | #define s(x) scanf("%lld",&x) 10 | #define s2(x,y) s(x)+s(y) 11 | #define s3(x,y,z) s(x)+s(y)+s(z) 12 | 13 | #define p(x) printf("%lld\n",x) 14 | #define p2(x,y) p(x)+p(y) 15 | #define p3(x,y,z) p(x)+p(y)+p(z) 16 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 17 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 18 | 19 | #define ff first 20 | #define ss second 21 | #define mp(x,y) make_pair(x,y) 22 | #define pll pair 23 | #define pb push_back 24 | 25 | ll inf = 1e18; 26 | ll mod = 1e9 + 7 ; 27 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 28 | 29 | int main() 30 | { 31 | // freopen("input.txt","r",stdin); 32 | // freopen("output.txt","w",stdout); 33 | ll t=1; 34 | // s(t); 35 | while(t--){ 36 | ll n; 37 | cin>>n; 38 | ll a=1; 39 | ll b=2; 40 | // Note that if we place the tile vertically the problem reduces to finding the number of ways in 2 X n-1 board 41 | // If we place the tile horizontally , the problem reduces to finding the number of ways in 2 x n-2 board since 42 | // only 1 tile can only be placed in only horizontal manner above the horizontally placed tile. 43 | // So total number of ways : F(n)= F(n+1)+F(n+2). 44 | // Or if placed in the opposite manner : F(n) = F(n-1) + F(n-2) = nth fibonacci number 45 | 46 | if(n==1)cout< 4 | #include 5 | using namespace std; 6 | 7 | #define ll long long int 8 | 9 | #define s(x) scanf("%lld",&x) 10 | #define s2(x,y) s(x)+s(y) 11 | #define s3(x,y,z) s(x)+s(y)+s(z) 12 | 13 | #define p(x) printf("%lld\n",x) 14 | #define p2(x,y) p(x)+p(y) 15 | #define p3(x,y,z) p(x)+p(y)+p(z) 16 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 17 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 18 | 19 | #define ff first 20 | #define ss second 21 | #define mp(x,y) make_pair(x,y) 22 | #define pll pair 23 | #define pb push_back 24 | ll mod = 1e9 + 7 ; 25 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 26 | 27 | int main() 28 | { 29 | freopen("input.txt","r",stdin); 30 | freopen("output.txt","w",stdout); 31 | ll t; 32 | s(t); 33 | while(t--){ 34 | ll n,k; 35 | s2(n,k); 36 | vectorar(n); 37 | while(n--){ 38 | ll num; s(num); 39 | // ct counts the number of set bits in num 40 | ll ct=0; 41 | while(num){ 42 | // This formula simply clears the first set bit from right in num 43 | num=num&(num-1); 44 | ct++; 45 | } 46 | ar.pb(ct); 47 | } 48 | // sort to find the K largest number of set bits and sum them up 49 | sort(ar.begin(),ar.end()); 50 | ll ans=0; 51 | for(ll i=ar.size()-1,j=1;i>=0 and j<=k;j++,i--)ans+=ar[i]; 52 | p(ans); 53 | } 54 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Competitive Programming -Problems 2 | Contains the solution for the Competitive Programming problems 3 | -------------------------------------------------------------------------------- /REGEX MATCHING - DP(MEDIUM) - HACKERBLOCKS.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | using namespace std; 5 | #define fio ios_base::sync_with_stdio(false) 6 | 7 | #define ll long long int 8 | 9 | #define s(x) scanf("%lld",&x) 10 | #define s2(x,y) s(x)+s(y) 11 | #define s3(x,y,z) s(x)+s(y)+s(z) 12 | 13 | #define p(x) printf("%lld\n",x) 14 | #define p2(x,y) p(x)+p(y) 15 | #define p3(x,y,z) p(x)+p(y)+p(z) 16 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 17 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 18 | 19 | #define ff first 20 | #define ss second 21 | #define mp(x,y) make_pair(x,y) 22 | #define pll pair 23 | #define pb push_back 24 | 25 | ll inf = 1e18; 26 | ll mod = 1e9 + 7 ; 27 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 28 | 29 | bool isMatch(string str,string pat){ 30 | str='.'+str; 31 | pat='.'+pat; 32 | if(str==pat)return true; 33 | ll n = str.size(); 34 | ll m = pat.size(); 35 | vector> dp(n,vector(m,false)); 36 | dp[0][0]=true; 37 | 38 | // This sets the base case when string is of 0 size. In that case if pattern is of x*y*z*.... 39 | // then, dp[0][j] will be true coz in this case , '*' means O or more preceding characters, so 40 | // for x*y*z*.... type pattern can be assumed to be pattern of 0 size , hence when string and pattern 41 | // are both of size 0, dp[][] will be true. 42 | for(ll j=1;j>str>>pat; 78 | cout< 3 | #include 4 | using namespace std; 5 | #define fio ios_base::sync_with_stdio(false) 6 | 7 | #define ll long long int 8 | 9 | #define s(x) scanf("%lld",&x) 10 | #define s2(x,y) s(x)+s(y) 11 | #define s3(x,y,z) s(x)+s(y)+s(z) 12 | 13 | #define p(x) printf("%lld\n",x) 14 | #define p2(x,y) p(x)+p(y) 15 | #define p3(x,y,z) p(x)+p(y)+p(z) 16 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 17 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 18 | 19 | #define ff first 20 | #define ss second 21 | #define mp(x,y) make_pair(x,y) 22 | #define pll pair 23 | #define pb push_back 24 | 25 | ll inf = 1e18; 26 | ll mod = 1e9 + 7 ; 27 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 28 | 29 | int main() 30 | { 31 | // freopen("input.txt","r",stdin); 32 | // freopen("output.txt","w",stdout); 33 | ll t=1; 34 | //s(t); 35 | while(t--){ 36 | ll n; 37 | s(n); 38 | vector> a(n+1); 39 | F(i,1,n){ 40 | a[i].resize(n+1); 41 | F(j,1,n){ 42 | cin>>a[i][j]; 43 | } 44 | } 45 | 46 | F(i,1,n){ 47 | F(j,1,n){ 48 | // Basic Observation 49 | ll res = a[i][j]|a[j][n-i+1]|a[n-j+1][i]|a[n-i+1][n-j+1]; 50 | cout< 2 | #include 3 | using namespace std; 4 | #define fio ios_base::sync_with_stdio(false) 5 | 6 | #define ll long long int 7 | 8 | #define s(x) scanf("%lld",&x) 9 | #define s2(x,y) s(x)+s(y) 10 | #define s3(x,y,z) s(x)+s(y)+s(z) 11 | 12 | #define p(x) printf("%lld\n",x) 13 | #define p2(x,y) p(x)+p(y) 14 | #define p3(x,y,z) p(x)+p(y)+p(z) 15 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 16 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 17 | 18 | #define ff first 19 | #define ss second 20 | #define mp(x,y) make_pair(x,y) 21 | #define pll pair 22 | #define pb push_back 23 | 24 | ll inf = 1e18; 25 | ll mod = 1e9 + 7 ; 26 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 27 | 28 | /****************************************************************************/ 29 | 30 | ll solve(vector &ar){ 31 | ll n=ar.size(); 32 | // fin is the final maximum answer 33 | ll fin=0; 34 | // Since n can be maximum 16, therefore we can check for all possible subsets 35 | // which will be 2^N - 1. 36 | for(ll i=1;i>=1; 59 | } 60 | fin=max(fin,temp_ans); 61 | } 62 | return fin; 63 | } 64 | 65 | int main() 66 | { 67 | // freopen("input.txt","r",stdin); 68 | // freopen("output.txt","w",stdout); 69 | ll t=1; 70 | s(t); 71 | while(t--){ 72 | ll n; 73 | cin>>n; 74 | vectorar; 75 | ll num; 76 | F(i,1,n){ 77 | cin>>num; 78 | ar.pb(num); 79 | } 80 | 81 | ll fin = solve(ar); 82 | if(fin&1){ 83 | cout<<"Odd\n"; 84 | }else{ 85 | cout<<"Even\n"; 86 | } 87 | } 88 | return 0; 89 | } -------------------------------------------------------------------------------- /SIMPLE ENOUGH - DnC-Adhoc - HACKERBLOCKS.cpp: -------------------------------------------------------------------------------- 1 | // https://hack.codingblocks.com/contests/c/133/907 2 | 3 | #include 4 | #include 5 | using namespace std; 6 | #define fio ios_base::sync_with_stdio(false) 7 | 8 | #define ll long long int 9 | 10 | #define s(x) scanf("%lld",&x) 11 | #define s2(x,y) s(x)+s(y) 12 | #define s3(x,y,z) s(x)+s(y)+s(z) 13 | 14 | #define p(x) printf("%lld\n",x) 15 | #define p2(x,y) p(x)+p(y) 16 | #define p3(x,y,z) p(x)+p(y)+p(z) 17 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 18 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 19 | 20 | #define ff first 21 | #define ss second 22 | #define mp(x,y) make_pair(x,y) 23 | #define pll pair 24 | #define pb push_back 25 | 26 | ll inf = 1e18; 27 | ll mod = 1e9 + 7 ; 28 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 29 | 30 | int main() 31 | { 32 | freopen("input.txt","r",stdin); 33 | // freopen("output.txt","w",stdout); 34 | ll t=1; 35 | // s(t); 36 | while(t--){ 37 | ll n,l,r; 38 | s3(n,l,r); 39 | // ar will store the binary breakdown of n in reverse order. therefore we are reversing it also later. 40 | vectorar; 41 | ll temp=n; 42 | while(temp){ 43 | ar.push_back(temp&1); 44 | temp>>=1; 45 | } 46 | reverse(ar.begin(), ar.end()); 47 | ll ans=0ll; 48 | ll sz=(ll)ar.size(); 49 | for(ll i=0ll;i 2 | #include 3 | using namespace std; 4 | #define fio ios_base::sync_with_stdio(false) 5 | 6 | #define ll long long int 7 | 8 | #define s(x) scanf("%lld",&x) 9 | #define s2(x,y) s(x)+s(y) 10 | #define s3(x,y,z) s(x)+s(y)+s(z) 11 | 12 | #define p(x) printf("%lld\n",x) 13 | #define p2(x,y) p(x)+p(y) 14 | #define p3(x,y,z) p(x)+p(y)+p(z) 15 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 16 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 17 | 18 | #define ff first 19 | #define ss second 20 | #define mp(x,y) make_pair(x,y) 21 | #define pll pair 22 | #define pb push_back 23 | 24 | ll inf = 1e18; 25 | ll mod = 1e9 + 7 ; 26 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 27 | 28 | /****************************************************************************/ 29 | 30 | int main() 31 | { 32 | // freopen("input.txt","r",stdin); 33 | // freopen("output.txt","w",stdout); 34 | ll t=1; 35 | // s(t); 36 | while(t--){ 37 | ll n,k; 38 | s2(n,k); 39 | // dp[i] represents the number possible number of ways Singham can have laddu if 40 | // i laddus are presented to him. 41 | vectordp(100005,0); 42 | // cum_sum[i] represents the cumulative sum of all the ways in which Singham can 43 | // have laddu for all number of laddus less than equal to i 44 | vectorcum_sum(100005,0); 45 | dp[0]=1ll; 46 | F(i,1,100000){ 47 | if(i 2 | #include 3 | using namespace std; 4 | #define fio ios_base::sync_with_stdio(false) 5 | 6 | #define ll long long int 7 | 8 | #define s(x) scanf("%lld",&x) 9 | #define s2(x,y) s(x)+s(y) 10 | #define s3(x,y,z) s(x)+s(y)+s(z) 11 | 12 | #define p(x) printf("%lld\n",x) 13 | #define p2(x,y) p(x)+p(y) 14 | #define p3(x,y,z) p(x)+p(y)+p(z) 15 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 16 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 17 | 18 | #define ff first 19 | #define ss second 20 | #define mp(x,y) make_pair(x,y) 21 | #define pll pair 22 | #define pb push_back 23 | 24 | ll inf = 1e18; 25 | ll mod = 1e9 + 7 ; 26 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 27 | 28 | /****************************************************************************/ 29 | 30 | int main() 31 | { 32 | // freopen("input.txt","r",stdin); 33 | // freopen("output.txt","w",stdout); 34 | ll t=1; 35 | s(t); 36 | while(t--){ 37 | ll P; 38 | s(P); 39 | ll ans=0ll; 40 | while(P){ 41 | if(P&1){ ans++; P--; } 42 | else { P=P>>1; } 43 | } 44 | p(ans); 45 | } 46 | return 0; 47 | } -------------------------------------------------------------------------------- /SINGHAM AND MAZE - DP - HACKERBLOCKS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | #define fio ios_base::sync_with_stdio(false) 5 | 6 | #define ll long long int 7 | 8 | #define s(x) scanf("%lld",&x) 9 | #define s2(x,y) s(x)+s(y) 10 | #define s3(x,y,z) s(x)+s(y)+s(z) 11 | 12 | #define p(x) printf("%lld\n",x) 13 | #define p2(x,y) p(x)+p(y) 14 | #define p3(x,y,z) p(x)+p(y)+p(z) 15 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 16 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 17 | 18 | #define ff first 19 | #define ss second 20 | #define mp(x,y) make_pair(x,y) 21 | #define pll pair 22 | #define pb push_back 23 | 24 | ll inf = 1e18; 25 | ll mod = 1e9 + 7 ; 26 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 27 | 28 | /****************************************************************************/ 29 | 30 | int main() 31 | { 32 | // freopen("input.txt","r",stdin); 33 | // freopen("output.txt","w",stdout); 34 | ll t=1; 35 | // s(t); 36 | while(t--){ 37 | ll n; 38 | s(n); 39 | vectorar(n+1,0); 40 | F(i,1,n)s(ar[i]); 41 | // dp[i] will represent the number of portals needed to move to the next room 42 | vectordp(n+1,0ll); 43 | // sum[i] will represent the cumulative sum of the number of portals needed to 44 | // move to the i+1th room from the first room. 45 | vectorsum(n+1,0ll); 46 | dp[1]=2; 47 | sum[1]=2; 48 | F(i,2,n){ 49 | // if the second portal leads to the same room, you can move to the next room using 50 | // just two portals, one for coming back to the same portal and the other one to 51 | // move to the next portal. 52 | if(ar[i]==i)dp[i]=2; 53 | // Else The total number of portals needed will be equal to the number of portals 54 | // needed to move from i-1 th room to the ith room which is computed using cumulative 55 | // sum array. 56 | else dp[i]= 2+(sum[i-1]-sum[ar[i]-1]+mod)%mod; 57 | sum[i]=sum[i-1]+dp[i]; 58 | sum[i]%=mod; 59 | } 60 | // finally sum[n] contains the total number of portals needed to move to the n+1th room 61 | // from the first room which is the needed question. 62 | p(sum[n]); 63 | } 64 | return 0; 65 | } -------------------------------------------------------------------------------- /SITTING ARRANGEMENT AT CODING BLOCKS.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | using namespace std; 5 | #define fio ios_base::sync_with_stdio(false) 6 | 7 | #define ll long long int 8 | 9 | #define s(x) scanf("%lld",&x) 10 | #define s2(x,y) s(x)+s(y) 11 | #define s3(x,y,z) s(x)+s(y)+s(z) 12 | 13 | #define p(x) printf("%lld\n",x) 14 | #define p2(x,y) p(x)+p(y) 15 | #define p3(x,y,z) p(x)+p(y)+p(z) 16 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 17 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 18 | 19 | #define ff first 20 | #define ss second 21 | #define mp(x,y) make_pair(x,y) 22 | #define pll pair 23 | #define pb push_back 24 | 25 | 26 | ll inf = 1e6+1; 27 | ll mod = 1e9 + 7 ; 28 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 29 | 30 | int main() 31 | { 32 | // freopen("input.txt","r",stdin); 33 | // freopen("output.txt","w",stdout); 34 | ll t=1; 35 | //s(t); 36 | while(t--){ 37 | ll n,k; 38 | s2(n,k); 39 | vectorar(n,0); 40 | for(ll i=0;i>ar[i]; 41 | // Keep two pointers 42 | ll lf=0,rt=0; 43 | // Best stores the maximum subset of consecutive integers found so far 44 | ll best=k; 45 | do{ 46 | // while the gap between the coordinates represented by ar[lf] and ar[rt] is greater than k,increment the left pointer 47 | while(ar[rt]-ar[lf]-(rt-lf) > k)lf++; 48 | best=max(best,rt-lf+1+k); 49 | // Increment the right pointer anyway 50 | rt++; 51 | }while(rt 4 | #include 5 | using namespace std; 6 | #define fio ios_base::sync_with_stdio(false) 7 | 8 | #define ll long long int 9 | 10 | #define s(x) scanf("%lld",&x) 11 | #define s2(x,y) s(x)+s(y) 12 | #define s3(x,y,z) s(x)+s(y)+s(z) 13 | 14 | #define p(x) printf("%lld\n",x) 15 | #define p2(x,y) p(x)+p(y) 16 | #define p3(x,y,z) p(x)+p(y)+p(z) 17 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 18 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 19 | 20 | #define ff first 21 | #define ss second 22 | #define mp(x,y) make_pair(x,y) 23 | #define pll pair 24 | #define pb push_back 25 | 26 | ll inf = 1e18; 27 | ll mod = 1e9 + 7 ; 28 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 29 | 30 | /****************************************************************************/ 31 | 32 | int main() 33 | { 34 | freopen("input.txt","r",stdin); 35 | // freopen("output.txt","w",stdout); 36 | ll t=1; 37 | // s(t); 38 | while(t--){ 39 | string str; 40 | cin>>str; 41 | stack st; 42 | bool fg=0; 43 | ll sz=str.size(); 44 | RF(i,sz-1,0){ 45 | if(str[i]=='i')st.push("int"); 46 | else if(str[i]=='p'){ 47 | sz=st.size(); 48 | if(sz<2){fg=1; break;} 49 | else { 50 | string newstr="pair<"; 51 | string astr = st.top(); st.pop(); 52 | string bstr = st.top(); st.pop(); 53 | newstr+=astr+","+bstr+">"; 54 | st.push(newstr); 55 | } 56 | } 57 | else {fg=1; break;} 58 | } 59 | sz=st.size(); 60 | if(fg==1 or sz>1 or st.top()=="int")cout<<"-1\n"; 61 | else cout< 4 | #include 5 | using namespace std; 6 | #define fio ios_base::sync_with_stdio(false) 7 | 8 | #define ll long long int 9 | 10 | #define s(x) scanf("%lld",&x) 11 | #define s2(x,y) s(x)+s(y) 12 | #define s3(x,y,z) s(x)+s(y)+s(z) 13 | 14 | #define p(x) printf("%lld\n",x) 15 | #define p2(x,y) p(x)+p(y) 16 | #define p3(x,y,z) p(x)+p(y)+p(z) 17 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 18 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 19 | 20 | #define ff first 21 | #define ss second 22 | #define mp(x,y) make_pair(x,y) 23 | #define pll pair 24 | #define pb push_back 25 | 26 | ll inf = 1e18; 27 | ll mod = 1e9 + 7 ; 28 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 29 | 30 | int main() 31 | { 32 | // freopen("input.txt","r",stdin); 33 | // freopen("output.txt","w",stdout); 34 | ll t=1; 35 | s(t); 36 | while(t--){ 37 | ll n; 38 | s(n); 39 | vectorar; 40 | F(i,0,n-1){ 41 | ll num; 42 | cin>>num; 43 | ar.pb(num); 44 | } 45 | 46 | if(n==1){ 47 | cout<<"0\n"; 48 | continue; 49 | } 50 | 51 | vectordp[2]; 52 | // dp[0][i] means maximum achievable sum by placing 1 at index i using the first i elements only 53 | dp[0]=vector(n,0ll); 54 | // dp[1][i] means maximum achievable sum by placing ar[i] at index i using the first i elements only 55 | dp[1]=vector(n,0ll); 56 | 57 | for(ll i=1;i 2 | #include 3 | using namespace std; 4 | #define fio ios_base::sync_with_stdio(false) 5 | 6 | #define ll long long int 7 | 8 | #define s(x) scanf("%lld",&x) 9 | #define s2(x,y) s(x)+s(y) 10 | #define s3(x,y,z) s(x)+s(y)+s(z) 11 | 12 | #define p(x) printf("%lld\n",x) 13 | #define p2(x,y) p(x)+p(y) 14 | #define p3(x,y,z) p(x)+p(y)+p(z) 15 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 16 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 17 | 18 | #define ff first 19 | #define ss second 20 | #define mp(x,y) make_pair(x,y) 21 | #define pll pair 22 | #define pb push_back 23 | 24 | ll inf = 1e18; 25 | ll mod = 1e9 + 7 ; 26 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 27 | 28 | /****************************************************************************/ 29 | 30 | int main() 31 | { 32 | freopen("input.txt","r",stdin); 33 | freopen("output.txt","w",stdout); 34 | ll t=1; 35 | s(t); 36 | 37 | while(t--){ 38 | ll n; 39 | s(n); 40 | vector freq(32); 41 | while(n--){ 42 | ll num; 43 | s(num); 44 | ll bt=0; 45 | while(num>0){ 46 | if(num&1)freq[bt]++; 47 | bt++; 48 | num>>=1; 49 | } 50 | } 51 | ll ans=0; 52 | ll maxi=0; 53 | for(ll bt=0;bt<32;bt++){ 54 | if(freq[bt]>maxi){ 55 | maxi=freq[bt]; 56 | ans=bt; 57 | } 58 | } 59 | p(ans); 60 | } 61 | return 0; 62 | } -------------------------------------------------------------------------------- /THE PIRATES AND THEIR CHESTS.cpp: -------------------------------------------------------------------------------- 1 | // https://www.hackerearth.com/practice/algorithms/greedy/basics-of-greedy-algorithms/practice-problems/algorithm/monk-in-the-magical-land/description/ 2 | 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | #define ll long long int 8 | // typedef __int128_t ll 9 | 10 | #define s(x) scanf("%lld",&x) 11 | #define s2(x,y) s(x)+s(y) 12 | #define s3(x,y,z) s(x)+s(y)+s(z) 13 | 14 | #define p(x) printf("%lld\n",x) 15 | #define p2(x,y) p(x)+p(y) 16 | #define p3(x,y,z) p(x)+p(y)+p(z) 17 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 18 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 19 | 20 | #define ff first 21 | #define ss second 22 | #define mp(x,y) make_pair(x,y) 23 | #define pll pair 24 | #define pb push_back 25 | 26 | ll mod = 1e9 + 7 ; 27 | ll inf = 1e18 ; 28 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 29 | 30 | int main() 31 | { 32 | // freopen("input.txt","r",stdin); 33 | // freopen("output.txt","w",stdout); 34 | ll t; 35 | cin>>t; 36 | while(t--){ 37 | ll n,m,k; 38 | cin>>n>>m>>k; 39 | vectorK(n),C(m),Z(m); 40 | 41 | // Chose this data type as we need atleast 100 bits to keep track of 100 possible chests 42 | __int128_t one=1; 43 | 44 | // key[i] will contain the bit representation of the chests ith key can open 45 | vector<__int128_t>key(n,0); 46 | 47 | for(ll i=0;i>K[i]; 48 | for(ll i=0;i>C[i]; 49 | for(ll i=0;i>Z[i]; 50 | 51 | for(ll i=0;i1){ 54 | key[i]|=one< k)continue; 62 | 63 | // 'chests' contain the bit representation of the chests which K keys 64 | // (set bits in the mask)can together open. 65 | __int128_t chests=0; 66 | for(ll j=0;j 3 | #include 4 | using namespace std; 5 | #define fio ios_base::sync_with_stdio(false) 6 | 7 | #define ll long long int 8 | 9 | #define s(x) scanf("%lld",&x) 10 | #define s2(x,y) s(x)+s(y) 11 | #define s3(x,y,z) s(x)+s(y)+s(z) 12 | 13 | #define p(x) printf("%lld\n",x) 14 | #define p2(x,y) p(x)+p(y) 15 | #define p3(x,y,z) p(x)+p(y)+p(z) 16 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 17 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 18 | 19 | #define ff first 20 | #define ss second 21 | #define mp(x,y) make_pair(x,y) 22 | #define pll pair 23 | #define pb push_back 24 | 25 | ll inf = 1e18; 26 | ll mod = 1e9 + 7 ; 27 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 28 | 29 | int main() 30 | { 31 | // freopen("input.txt","r",stdin); 32 | // freopen("output.txt","w",stdout); 33 | ll t=1; 34 | s(t); 35 | while(t--){ 36 | ll n,m; 37 | s2(n,m); 38 | vectordp(n+1,0); 39 | dp[0]=1ll; 40 | for(ll i=1;i<=n;i++){ 41 | dp[i]=dp[i-1]; 42 | dp[i]+=((i-m)>=0)?dp[i-m]:0; 43 | dp[i]%=mod; 44 | } 45 | p(dp[n]); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /TREE TRAVERSAL (ADVANCED) - RESTORE TREE USING INORDER AND PREORDER WITHOUT RECURSION - CODEFIGHTS.cpp: -------------------------------------------------------------------------------- 1 | //https://codefights.com/interview-practice/task/AaWaYxi8gjtbqgp2M/description 2 | 3 | // Definition for binary tree: 4 | // template 5 | // struct Tree { 6 | // Tree(const T &v) : value(v), left(nullptr), right(nullptr) {} 7 | // T value; 8 | // Tree *left; 9 | // Tree *right; 10 | // }; 11 | 12 | //------------------------------------ ITERATIVE SOLUTION ----------------------------------------------// 13 | 14 | #define nodeptr Tree* 15 | Tree * restoreBinaryTree(std::vector inorder, std::vector preorder) { 16 | if(preorder.size()==0)return nullptr; 17 | stack stk; 18 | int preptr=0; 19 | int inptr=0; 20 | nodeptr root; 21 | nodeptr cur=new Tree(preorder[0]); 22 | stk.push(cur); 23 | root=cur; 24 | preptr++; 25 | bool isRight=false; 26 | while(inptrvalue){ 28 | if(isRight){ 29 | cur->right = new Tree(preorder[preptr]); 30 | cur=cur->right; 31 | isRight=false; 32 | }else{ 33 | cur->left = new Tree(preorder[preptr]); 34 | cur=cur->left; 35 | } 36 | preptr++; 37 | stk.push(cur); 38 | }else{ 39 | isRight=true; 40 | cur=stk.top(); 41 | stk.pop(); 42 | inptr++; 43 | } 44 | } 45 | return root; 46 | } 47 | 48 | //------------------------------------------- RECURSI1VE SOLUTION -------------------------------------// 49 | 50 | // #define nodeptr Tree* 51 | // int indx=0; 52 | // nodeptr helper(vector &inorder,int lf,int rt,vector &preorder){ 53 | // int id = -1; 54 | // for(int i=lf;i<=rt;i++) 55 | // if(inorder[i]==preorder[indx]){ 56 | // id=i; 57 | // break; 58 | // } 59 | 60 | // if(id==-1)return nullptr; 61 | // nodeptr root = new Tree(preorder[indx]); 62 | // indx++; 63 | // root->left = helper(inorder,lf,id-1,preorder); 64 | // root->right = helper(inorder,id+1,rt,preorder); 65 | // return root; 66 | // } 67 | 68 | // Tree * restoreBinaryTree(std::vector inorder, std::vector preorder) { 69 | // return helper(inorder,0,inorder.size()-1,preorder); 70 | // } 71 | -------------------------------------------------------------------------------- /Test Case Generator.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | #define fio ios_base::sync_with_stdio(false) 5 | 6 | #define ll long long int 7 | 8 | #define s(x) scanf("%lld",&x) 9 | #define s2(x,y) s(x)+s(y) 10 | #define s3(x,y,z) s(x)+s(y)+s(z) 11 | 12 | #define p(x) printf("%lld\n",x) 13 | #define p2(x,y) p(x)+p(y) 14 | #define p3(x,y,z) p(x)+p(y)+p(z) 15 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 16 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 17 | 18 | #define ff first 19 | #define ss second 20 | #define mp(x,y) make_pair(x,y) 21 | #define pll pair 22 | #define pb push_back 23 | 24 | ll inf = 1e18; 25 | ll mod = 1e9 + 7 ; 26 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 27 | 28 | /****************************************************************************/ 29 | 30 | int main() 31 | { 32 | // freopen("input.txt","r",stdin); 33 | freopen("input.txt","w",stdout); 34 | ll t=1; 35 | 36 | srand(time(NULL)); 37 | // t = 10; 38 | // s(t); 39 | // cout<1000000){ 60 | cout<<"sorry \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"; 61 | return 0; 62 | } 63 | cout< 2 | #include 3 | using namespace std; 4 | #define fio ios_base::sync_with_stdio(false) 5 | 6 | #define ll long long int 7 | 8 | #define s(x) scanf("%lld",&x) 9 | #define s2(x,y) s(x)+s(y) 10 | #define s3(x,y,z) s(x)+s(y)+s(z) 11 | 12 | #define p(x) printf("%lld\n",x) 13 | #define p2(x,y) p(x)+p(y) 14 | #define p3(x,y,z) p(x)+p(y)+p(z) 15 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 16 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 17 | 18 | #define ff first 19 | #define ss second 20 | #define mp(x,y) make_pair(x,y) 21 | #define pll pair 22 | #define pb push_back 23 | 24 | ll inf = 1e18; 25 | ll mod = 1e9 + 7 ; 26 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 27 | 28 | ll dp[1005][1005][15][2]; 29 | ll n,m; 30 | string str1,str2; 31 | 32 | // Note: when the flag is set, it means, that there is already some common substring which is being continued 33 | // from the last call. 34 | // if flag==1, 35 | // dp[i][j][k][flag] = Maximum length of the disjoint subset of the required substring upto length of 36 | // i for string s, j for string t and considering the total subsets needed to be k. 37 | ll solve(ll i,ll j,ll k,ll flag){ 38 | 39 | // if k<0, then we can't use this configuration at all, so return Minimum possible value since we are maximising 40 | // in the calling function. 41 | if(k<0)return INT_MIN; 42 | 43 | // if this condition is satisfied, then we can safely assume that required number of disjoint subsets have been already 44 | // formed and we can return 0. 45 | if((i>=n or j>=m) and k==0)return 0; 46 | 47 | // same as the first condition 48 | if((i>=n or j>=m) and k!=0)return INT_MIN; 49 | 50 | if(dp[i][j][k][flag] != -1)return dp[i][j][k][flag]; 51 | 52 | // Choice 1: We are simply not comparing the s[i] and t[j] and trying to find other subsequences 53 | ll ans = max(solve(i,j+1,k,0),solve(i+1,j,k,0)); 54 | 55 | if(str1[i]==str2[j]){ 56 | // Choice 2: since s[i] and t[j] are same and the flag is set, so we are continuing with the last substring itself. 57 | if(flag) ans=max(ans,1+solve(i+1,j+1,k,1)); 58 | 59 | // Choice 3: Here, we have discontinued the previous common substring and started a new one, so k is decremented by 1. 60 | ans=max(ans,1+solve(i+1,j+1,k-1,1)); 61 | } 62 | dp[i][j][k][flag]=ans; 63 | return ans; 64 | } 65 | 66 | int main() 67 | { 68 | // freopen("input.txt","r",stdin); 69 | // freopen("output.txt","w",stdout); 70 | ll t=1; 71 | // s(t); 72 | while(t--){ 73 | ll k; 74 | memset(dp,-1,sizeof(dp)); 75 | s3(n,m,k); 76 | cin>>str1>>str2; 77 | cout< 3 | 4 | using namespace std; 5 | 6 | #define ll long long int 7 | 8 | #define s(x) scanf("%lld",&x) 9 | #define s2(x,y) s(x)+s(y) 10 | #define s3(x,y,z) s(x)+s(y)+s(z) 11 | 12 | #define p(x) printf("%lld\n",x) 13 | #define p2(x,y) p(x)+p(y) 14 | #define p3(x,y,z) p(x)+p(y)+p(z) 15 | #define fori(i,max) for(ll i=0;i<(ll)(max) ;(i)++) 16 | #define for2i(i,min,max) for(ll i=min;i<(ll)(max) ;(i)++) 17 | 18 | #define ff first 19 | #define ss second 20 | #define mp(x,y) make_pair(x,y) 21 | #define plb pair 22 | #define pb push_back 23 | #define mx 500090 24 | 25 | ll rt[mx+5]; 26 | ll lt[mx+5]; 27 | 28 | int main() 29 | { 30 | // freopen("input.txt","r",stdin); 31 | // freopen("output.txt","w",stdout); 32 | ll n,a,b,t; 33 | cin>>n>>a>>b>>t; 34 | string st; 35 | cin>>st; 36 | st=' '+st; 37 | 38 | // rt[i] contains total time needed to move from pic 1 to pic i (including the swiping time(a), observing time(1),rotating time(b)) 39 | for(ll i=2;i<=n;i++) 40 | rt[i]=rt[i-1]+a+1+b*(st[i]=='w'); 41 | 42 | // lt[i] contains total time needed to move from pic n+1 (pic 1) to pic i (including the swiping time(a), observing time(1),rotating time(b)) 43 | for(ll i=n;i>=2;i--) 44 | lt[i]=lt[i+1]+a+1+b*(st[i]=='w'); 45 | 46 | // subtract the time needed to observe and rotate(if needed) the first pic (pic 1) currently opened 47 | t -= 1+b*(st[1]=='w'); 48 | 49 | ll ans=0; 50 | 51 | // Go till pic i to the right from first pic and then move back till pic j to the left 52 | for(ll i=1,j=2;i<=n;i++){ 53 | if(rt[i]>t)break; 54 | 55 | // Here we are trying to find the upper bound for j till which we can see the pics in the remanining time 56 | while(j<=n && rt[i]+lt[j]+(i-1)*a > t)j++ ; // Note the factor (i-1)*a constitutes the time needed to swipe over the already seen pics since we are swiping back 57 | 58 | ans=max(ans,i+n-j+1); 59 | } 60 | 61 | // Go till pic i to the left from first pic and then move back till pic j to the right 62 | for(ll i=n+1,j=n;i>=2;i--) 63 | { 64 | if(lt[i]>t)break; 65 | 66 | // Here we are trying to find the upper bound for j till which we can see the pics in the remanining time 67 | while(j>=1 && lt[i]+rt[j]+(n-i+1)*a > t)j-- ; 68 | 69 | ans=max(ans,n-i+1+j); 70 | } 71 | cout< 3 | #include 4 | using namespace std; 5 | #define fio ios_base::sync_with_stdio(false) 6 | 7 | #define ll long long int 8 | 9 | #define s(x) scanf("%lld",&x) 10 | #define s2(x,y) s(x)+s(y) 11 | #define s3(x,y,z) s(x)+s(y)+s(z) 12 | 13 | #define p(x) printf("%lld\n",x) 14 | #define p2(x,y) p(x)+p(y) 15 | #define p3(x,y,z) p(x)+p(y)+p(z) 16 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 17 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 18 | 19 | #define ff first 20 | #define ss second 21 | #define mp(x,y) make_pair(x,y) 22 | #define pll pair 23 | #define pb push_back 24 | 25 | ll inf = 1e18; 26 | ll mod = 1e9 + 7 ; 27 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 28 | 29 | map dp; 30 | 31 | bool doesPatMatches(string &str, string &pat, ll i,ll j){ 32 | 33 | // Base Case : will only reach here if the pattern has matched the string 34 | if(i==str.size() and j==pat.size())return true; 35 | if(dp.find({i,j})!=dp.end())return dp[{i,j}]; 36 | 37 | if(i==str.size()){ 38 | if(pat[j]!='*')return false; // because * means 0 or more characters, hence if it is not `*`, there is no way to match the finished string with the remaining pattern 39 | return dp[{i,j}]=doesPatMatches(str,pat,i,j+1);// will only return true if pattern has only `*` ahead left 40 | } 41 | if(j==pat.size())return dp[{i,j}]=false; // pattern finished, but string is still left 42 | if(pat[j]=='*'){ 43 | // since '*' means 0 or more characters, the left part of 'or' considers 0 characters and the right part means 1 or more characters 44 | return dp[{i,j}] = doesPatMatches(str,pat,i,j+1) or doesPatMatches(str,pat,i+1,j); 45 | }else if(pat[j]=='?'){ 46 | return dp[{i,j}]= doesPatMatches(str,pat,i+1,j+1); 47 | }else{ 48 | return dp[{i,j}] = str[i]==pat[j] and doesPatMatches(str,pat,i+1,j+1); 49 | } 50 | } 51 | 52 | int main() 53 | { 54 | // freopen("input.txt","r",stdin); 55 | // freopen("output.txt","w",stdout); 56 | ll t=1; 57 | // s(t); 58 | while(t--){ 59 | dp.clear(); 60 | string str,pat; 61 | cin>>str>>pat; 62 | cout< 3 | #include 4 | using namespace std; 5 | #define fio ios_base::sync_with_stdio(false) 6 | 7 | #define ll long long int 8 | 9 | #define s(x) scanf("%lld",&x) 10 | #define s2(x,y) s(x)+s(y) 11 | #define s3(x,y,z) s(x)+s(y)+s(z) 12 | 13 | #define p(x) printf("%lld\n",x) 14 | #define p2(x,y) p(x)+p(y) 15 | #define p3(x,y,z) p(x)+p(y)+p(z) 16 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 17 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 18 | 19 | #define ff first 20 | #define ss second 21 | #define mp(x,y) make_pair(x,y) 22 | #define pll pair 23 | #define pb push_back 24 | 25 | ll inf = 1e18; 26 | ll mod = 1e9 + 7 ; 27 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 28 | 29 | map dp; 30 | 31 | bool doesPatMatches(string &str, string &pat, ll i,ll j){ 32 | 33 | // Base Cose : will only reach here if the pattern has matched the string 34 | if(i==str.size() and j==pat.size())return true; 35 | if(dp.find({i,j})!=dp.end())return dp[{i,j}]; 36 | 37 | if(i==str.size()){ 38 | if(pat[j]!='*')return false; // because * means 0 or more characters, hence if it is not `*`, there is no way to match the finished string with the remaining pattern 39 | return dp[{i,j}]=doesPatMatches(str,pat,i,j+1);// will only return true if pattern has only `*` ahead left 40 | } 41 | if(j==pat.size())return dp[{i,j}]=false; // pattern finished, but string is still left 42 | if(pat[j]=='*'){ 43 | // since '*' means 0 or more characters, the left part of 'or' considers 0 characters and the right part means 1 or more characters 44 | return dp[{i,j}] = doesPatMatches(str,pat,i,j+1) or doesPatMatches(str,pat,i+1,j); 45 | }else if(pat[j]=='?'){ 46 | return dp[{i,j}]= doesPatMatches(str,pat,i+1,j+1); 47 | }else{ 48 | return dp[{i,j}] = str[i]==pat[j] and doesPatMatches(str,pat,i+1,j+1); 49 | } 50 | } 51 | 52 | int main() 53 | { 54 | // freopen("input.txt","r",stdin); 55 | // freopen("output.txt","w",stdout); 56 | ll t=1; 57 | // s(t); 58 | while(t--){ 59 | dp.clear(); 60 | string str,pat; 61 | cin>>str>>pat; 62 | cout< 2 | using namespace std; 3 | 4 | 5 | int main(){ 6 | static const int MAX = 100; 7 | int n; 8 | scanf("%d",&n); 9 | vector ar(MAX+5); 10 | for(int i=1;i<=n;i++) cin>>ar[i]; 11 | 12 | int dp[MAX+5][2][2]={0}; 13 | int ans[MAX+5]={0}; 14 | ans[0]=0; 15 | int ret=0; 16 | for(int i=1;i<=n;i++){ 17 | bool G=false; 18 | bool C=false; 19 | if(ar[i]&1)C=true;; 20 | if((ar[i]>>1)&1)G=true; 21 | 22 | // cout< 4 | #include 5 | using namespace std; 6 | #define fio ios_base::sync_with_stdio(false) 7 | 8 | #define ll long long int 9 | 10 | #define s(x) scanf("%lld",&x) 11 | #define s2(x,y) s(x)+s(y) 12 | #define s3(x,y,z) s(x)+s(y)+s(z) 13 | 14 | #define p(x) printf("%lld\n",x) 15 | #define p2(x,y) p(x)+p(y) 16 | #define p3(x,y,z) p(x)+p(y)+p(z) 17 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 18 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 19 | 20 | #define ff first 21 | #define ss second 22 | #define mp(x,y) make_pair(x,y) 23 | #define pll pair 24 | #define pb push_back 25 | 26 | ll inf = 1e18; 27 | ll mod = 1e9 + 7 ; 28 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 29 | 30 | /****************************************************************************/ 31 | 32 | int main() 33 | { 34 | // freopen("input.txt","r",stdin); 35 | // freopen("output.txt","w",stdout); 36 | ll t=1; 37 | // s(t); 38 | while(t--){ 39 | ll n; 40 | s(n); 41 | vector ar(n+1); 42 | F(i,0,n-1)s(ar[i+1]); 43 | // dp[i][j] represents the maximum number of days Singham will NOT take rest till ith day 44 | // if he does the jth activity : 45 | // j=0 if he is resting, j is 1 if he is giving contest,j is 2 if he is gymming . 46 | ll dp[n+1][3]={0}; 47 | F(i,1,n){ 48 | dp[i][0]=max(dp[i-1][0],max(dp[i-1][1],dp[i-1][2])); 49 | if(ar[i]==1 or ar[i]==3)dp[i][1]=1+max(dp[i-1][0],dp[i-1][2]); 50 | if(ar[i]==2 or ar[i]==3)dp[i][2]=1+max(dp[i-1][0],dp[i-1][1]); 51 | } 52 | // Since we have found the complementary answer i.e. Maximum days Singham will not take rest, 53 | // we will subtract it with the total days to get the actual answer,i.e. Minimum days when 54 | // Singham will take rest. 55 | p(n-max(dp[n][0],max(dp[n][1],dp[n][2]))); 56 | } 57 | 58 | return 0; 59 | } -------------------------------------------------------------------------------- /XORED ARRANGEMENT.cpp: -------------------------------------------------------------------------------- 1 | // https://www.hackerearth.com/practice/algorithms/dynamic-programming/bit-masking/practice-problems/algorithm/when-the-integers-got-upset/ 2 | 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | #define ll long long int 8 | 9 | #define s(x) scanf("%lld",&x) 10 | #define s2(x,y) s(x)+s(y) 11 | #define s3(x,y,z) s(x)+s(y)+s(z) 12 | 13 | #define p(x) printf("%lld\n",x) 14 | #define p2(x,y) p(x)+p(y) 15 | #define p3(x,y,z) p(x)+p(y)+p(z) 16 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 17 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 18 | 19 | #define ff first 20 | #define ss second 21 | #define mp(x,y) make_pair(x,y) 22 | #define pll pair 23 | #define pb push_back 24 | 25 | ll inf = 1e18; 26 | ll mod = 1e9 + 7 ; 27 | 28 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 29 | ll n; 30 | // dp[mask][first][second] gives the answer for the arrangement where only those 31 | // integers are considered so far for which the bits are set in the mask and 32 | // the 'first' and 'second' represent numbers which were taken first and second 33 | // in the given combination. 34 | ll dp[5000][13][13]; 35 | 36 | 37 | vector ar,pos; 38 | 39 | 40 | ll solveDP(ll mask,ll fr,ll sc){ 41 | if(dp[mask][fr][sc]!=-1) return dp[mask][fr][sc]; 42 | 43 | // po contains the number of set bits in the mask 44 | ll po = __builtin_popcountll(mask); 45 | 46 | // if all numbers have been considered, return 0 47 | if(po==n)return 0; 48 | 49 | 50 | ll ans=inf; 51 | // th represents the third number to be chosen after first 'fr' 52 | // and second number 'sc',all from the last in the chosen arrangement, 53 | // have been considered 54 | for(ll th=0;th(n); 77 | pos = vector(n); 78 | 79 | F(i,0,n-1)cin>>ar[i]; 80 | F(i,0,n-1)cin>>pos[i]; 81 | 82 | if(n<3) { cout<<"0\n"; continue; } 83 | 84 | ll ans=inf; 85 | // i and j indicates the possible values of the first and second numbers 86 | // which can be chosen in any possible arrangement of numbers 87 | for(ll i=0;ien)return 0; 7 | if(st==en)return (ht==1)?1:0; 8 | int ans=0; 9 | for(int i=st;i<=en;i++){ 10 | ans= (ans+ (exactHt(st,i-1,ht-1)*exactHt(i+1,en,ht-1))%mod + (lessHt(st,i-1,ht-1)*exactHt(i+1,en,ht-1))%mod + (exactHt(st,i-1,ht-1)*lessHt(i+1,en,ht-1))%mod)%mod; 11 | } 12 | return ans; 13 | } 14 | 15 | int lessHt(int st,int en, int ht){ 16 | if(st>en)return 0; 17 | if(st==en)return (ht>0)?1:0; 18 | int ans=0; 19 | for(int i=st;i<=en;i++){ 20 | ans = (ans + (lessHt(st,i-1,ht-1)*lessHt(i+1,en,ht-1))%mod)%mod; 21 | } 22 | return ans; 23 | } 24 | 25 | int Solution::cntPermBST(int A, int B) { 26 | int ans=0; 27 | for(int i=1;i<=A;i++){ 28 | ans= (ans+ (exactHt(1,i-1,B)*exactHt(i+1,A,B))%mod + (lessHt(1,i-1,B)*exactHt(i+1,A,B))%mod + (exactHt(1,i-1,B)*lessHt(i+1,A,B))%mod)%mod; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /graph medium codeagon 2.cpp: -------------------------------------------------------------------------------- 1 | // https://www.hackerrank.com/contests/codeagon/challenges/happiness/problem 2 | 3 | #include 4 | #include 5 | using namespace std; 6 | #define fio ios_base::sync_with_stdio(false) 7 | 8 | #define ll long long int 9 | 10 | #define s(x) scanf("%lld",&x) 11 | #define s2(x,y) s(x)+s(y) 12 | #define s3(x,y,z) s(x)+s(y)+s(z) 13 | 14 | #define p(x) printf("%lld\n",x) 15 | #define p2(x,y) p(x)+p(y) 16 | #define p3(x,y,z) p(x)+p(y)+p(z) 17 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 18 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 19 | 20 | #define ff first 21 | #define ss second 22 | #define mp(x,y) make_pair(x,y) 23 | #define pll pair 24 | #define pb push_back 25 | 26 | ll inf = 1e18; 27 | ll mod = 1e9 + 7 ; 28 | const ll lim = 5e5+5; 29 | 30 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 31 | 32 | vectoradj[lim]; 33 | vectorvec; 34 | bool vis[lim]; 35 | 36 | void dfs(ll cur, ll par, ll &nodes, ll &edges){ 37 | nodes++; 38 | edges+=adj[cur].size(); 39 | vis[cur]=true; 40 | 41 | for(ll i=0;inodes-1){ 78 | ct++; 79 | vec[i].second--; 80 | edges=vec[i].second; 81 | if(edges>nodes-1){ 82 | ct++; 83 | break; 84 | } 85 | if(ct==2)break; 86 | } 87 | } 88 | 89 | if(ct<2){ 90 | sort(vec.begin(), vec.end()); 91 | F(i,0,vec.size()-1){ 92 | ll edges=vec[i].second; 93 | if(edges>0){ 94 | vec[i].first--; 95 | vec[i].second--; 96 | ct++; 97 | sort(vec.begin(), vec.end()); 98 | i=0; 99 | } 100 | if(ct==2)break; 101 | } 102 | } 103 | 104 | ll ans=0; 105 | F(i,0,vec.size()-1){ 106 | ans+=(vec[i].first)*(vec[i].first-1); 107 | } 108 | p(ans); 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /input.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 123 -------------------------------------------------------------------------------- /priority queue advanced.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/n-max-pair-combinations/ 2 | 3 | vector A; 4 | vector B; 5 | 6 | 7 | #define pii pair 8 | vector Solution::solve(vector &a, vector &b) { 9 | A=a; 10 | B=b; 11 | vectorans; 12 | int n=A.size(); 13 | if(n==0)return ans; 14 | 15 | sort(A.rbegin(),A.rend()); 16 | sort(B.rbegin(),B.rend()); 17 | 18 | priority_queue,vector>> pq; 19 | pq.push({A[0]+B[0],{0,0}}); 20 | mapmp; 21 | mp[{0,0}]=true; 22 | while(ans.size() p=pq.top(); 24 | pq.pop(); 25 | int l=p.second.first; 26 | int r=p.second.second; 27 | ans.push_back(p.first); 28 | if(r+1 4 | #include 5 | using namespace std; 6 | #define fio ios_base::sync_with_stdio(false) 7 | 8 | #define ll long long int 9 | 10 | #define s(x) scanf("%lld",&x) 11 | #define s2(x,y) s(x)+s(y) 12 | #define s3(x,y,z) s(x)+s(y)+s(z) 13 | 14 | #define p(x) printf("%lld\n",x) 15 | #define p2(x,y) p(x)+p(y) 16 | #define p3(x,y,z) p(x)+p(y)+p(z) 17 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 18 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 19 | 20 | #define ff first 21 | #define ss second 22 | #define mp(x,y) make_pair(x,y) 23 | #define pll pair 24 | #define pb push_back 25 | 26 | ll inf = 1e18; 27 | ll mod = 1e9 + 7 ; 28 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 29 | 30 | /****************************************************************************/ 31 | 32 | ll dp[5000][600]; 33 | ll mo; 34 | 35 | ll solve(ll sum,ll ind){ 36 | 37 | if(sum<0)return 0ll; 38 | 39 | if(ind==0){ 40 | if(sum==0)return 1ll; 41 | return 0ll; 42 | } 43 | 44 | if(dp[sum][ind]!=-1)return dp[sum][ind]; 45 | ll numOfCards=0ll; 46 | for(ll i=0;i<10;i++){ 47 | ll num=i; 48 | if(num%2 ==0){ 49 | num=2*num; 50 | if(num>9)num-=9; 51 | } 52 | 53 | numOfCards+=solve(sum-num,ind-1); 54 | numOfCards%=mod; 55 | } 56 | return dp[sum][ind]=numOfCards; 57 | } 58 | 59 | int main() 60 | { 61 | freopen("input.txt","r",stdin); 62 | // freopen("output.txt","w",stdout); 63 | ll t=1; 64 | s(t); 65 | memset(dp,-1,sizeof(dp)); 66 | while(t--){ 67 | 68 | ll x; 69 | s2(x,mo); 70 | ll ans=0ll; 71 | for(ll sum=0ll;sum<=x*9;sum+=mo){ 72 | ans+=solve(sum,x); 73 | ans%=mod; 74 | } 75 | p(ans); 76 | } 77 | return 0; 78 | } -------------------------------------------------------------------------------- /testing.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iam-ss/Competitive-Programming-Problems/d07e00c8d81e9d4ad8fd90aebf0edb3e39af90a6/testing.exe -------------------------------------------------------------------------------- /testing1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | #define fio ios_base::sync_with_stdio(false) 5 | 6 | #define ll long long int 7 | 8 | #define s(x) scanf("%lld",&x) 9 | #define s2(x,y) s(x)+s(y) 10 | #define s3(x,y,z) s(x)+s(y)+s(z) 11 | 12 | #define p(x) printf("%lld\n",x) 13 | #define p2(x,y) p(x)+p(y) 14 | #define p3(x,y,z) p(x)+p(y)+p(z) 15 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 16 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 17 | 18 | #define ff first 19 | #define ss second 20 | #define mp(x,y) make_pair(x,y) 21 | #define pll pair 22 | #define pb push_back 23 | 24 | ll inf = 1e18; 25 | ll mod = 1e9 + 7 ; 26 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 27 | 28 | /****************************************************************************/ 29 | 30 | class SquareMatrix{ 31 | vector> mat; 32 | ll sz; 33 | 34 | void fillMatrix(ll i,ll j){ 35 | ll toFill = 1; 36 | F(in,i,sz-1)mat[in][j]=toFill++; 37 | F(in,0,i-1)mat[in][j]=toFill++; 38 | } 39 | 40 | public: 41 | SquareMatrix(ll n=0):sz(n){ 42 | assert(n%2 != 0); 43 | mat = vector>(n,vector(n,0)); 44 | } 45 | 46 | void printMatrix(){ 47 | F(i,0,sz-1){ 48 | F(j,0,sz-1){ 49 | cout< 2 | #include 3 | #include 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | 26 | using namespace std; 27 | 28 | #define ll long long int 29 | 30 | #define s(x) scanf("%lld",&x) 31 | #define s2(x,y) s(x)+s(y) 32 | #define s3(x,y,z) s(x)+s(y)+s(z) 33 | 34 | #define p(x) printf("%lld\n",x) 35 | #define p2(x,y) p(x)+p(y) 36 | #define p3(x,y,z) p(x)+p(y)+p(z) 37 | #define fori(i,max) for(ll i=0;i<(ll)(max) ;(i)++) 38 | #define for2i(i,min,max) for(ll i=min;i<(ll)(max) ;(i)++) 39 | 40 | int main() 41 | { 42 | freopen("input.txt","r",stdin); 43 | freopen("output.txt","w",stdout); 44 | ll t; 45 | s(t); 46 | while(t--) 47 | { 48 | ll n,x; 49 | s2(n,x); 50 | ll *a=new ll[n+1]; 51 | 52 | fori(i,n) 53 | { 54 | s(a[i]); 55 | } 56 | bool flag=0; 57 | 58 | // left pointer 59 | ll c1=0; 60 | 61 | // right pointer 62 | ll c2=0; 63 | 64 | // sum contains the sum of all the array elements between left and right pointer index 65 | ll sum=0; 66 | 67 | for(ll i=0;ix) 77 | { 78 | while(sum>=x && c1<=c2) 79 | { 80 | 81 | if(c1==c2) 82 | { 83 | sum=0; 84 | c1++; 85 | c2++; 86 | break; 87 | } 88 | 89 | sum -= a[c1]; 90 | c1++ ; 91 | 92 | if(sum==x) 93 | { 94 | flag=1;break; 95 | } 96 | } 97 | if(flag==1)break; 98 | } 99 | 100 | } 101 | if(sum