├── Coding Exercise Solutions ├── Alice Problem.cpp ├── Alice Studies Informatics.cpp ├── Alice and Candies.cpp ├── Array Stabilization.cpp ├── Ashish and Bit Operations.cpp ├── Beautiful Numbers.cpp ├── Bitwise AND of Range of Numbers.cpp ├── Bob and Palindromes.cpp ├── Bob and his Brother.cpp ├── Bulbs.cpp ├── Bulls and Cows.cpp ├── Closest Divisors.cpp ├── Closest Room.cpp ├── Coprime Divisor.cpp ├── Count Primes.cpp ├── Count the Arrays.cpp ├── Decode Permutation.cpp ├── Decode XORed Array.cpp ├── Decreasing Sequence.cpp ├── Distinct Candies.cpp ├── Divisibility.cpp ├── Factorization.cpp ├── Find K Closest Elements.cpp ├── First Unique Character in a String.cpp ├── Flipping Coins.cpp ├── Good Array.cpp ├── Greatest Common Divisor.cpp ├── Hamming Distance.cpp ├── Koko Eating Bananas.cpp ├── Kth Largest Element.cpp ├── LEQ.cpp ├── Letter Combinations of a Phone Number.cpp ├── Logs.cpp ├── Longest Consecutive Run of 1s in Binary.cpp ├── Magic of the Locker.cpp ├── Make It Equal.cpp ├── Math.cpp ├── Maximum Score From Removing Stones.cpp ├── Maximum Score.cpp ├── Meet in the Middle.cpp ├── Minimum Number of Taps to Open.cpp ├── One Integer.cpp ├── POW.cpp ├── Palindrome Partitioning.cpp ├── Petya and Array.cpp ├── Pond.cpp ├── Power Sum.cpp ├── Power of Four.cpp ├── Prime Sum.cpp ├── Range Minimum Queries.cpp ├── Range Xor Queries.cpp ├── Remainders Game.cpp ├── Reverse Pairs.cpp ├── Row Gcd.cpp ├── Second Player.cpp ├── Segment Intersections.cpp ├── Selection of Personnel.cpp ├── Shortest Superstring.cpp ├── Skyline.cpp ├── Smallest Range Covering Elements from K Lists.cpp ├── Sort Integers by The Number of 1 Bits.cpp ├── Strange Function.cpp ├── Subarray Sum Divisible by N.cpp ├── Success Rate.cpp ├── Sudoku Solver.cpp ├── Target Sum.cpp ├── The World is a Theatre.cpp ├── Too Many Segments.cpp ├── Total Hamming Distance.cpp ├── Triples with Bitwise AND Equal To Zero.cpp ├── Valid Perfect Square.cpp ├── aMaximumPath.cpp ├── ashishAndAashika'sProblem.cpp ├── astronautPairs.cpp ├── barcode.cpp ├── bestTimeToBuyAndSellStock.cpp ├── blockedRoads.cpp ├── boredom.cpp ├── buildRoads.cpp ├── cheapestFlightWithinKStops.cpp ├── cherryPickup.cpp ├── classyNumbers.cpp ├── collectingCoins.cpp ├── comeBackQuickly.cpp ├── connectedCities.cpp ├── countAllPossibleRoutes.cpp ├── countOfSmallerNumbers.cpp ├── cut'emAll.cpp ├── delayTimeInNetwork.cpp ├── dijkstra.cpp ├── disjointSetUnion.cpp ├── distanceQueries.cpp ├── divisorGame.cpp ├── erectTheFence.cpp ├── findPattern.cpp ├── findTwoNonoverlappingSubArraysEachWithTargetSum.cpp ├── gameOfBullets.cpp ├── gameRoutes.cpp ├── gcdOnDirectedPath.cpp ├── gcdSortOfAnArray.cpp ├── ghost.cpp ├── littleElephantAndArray.cpp ├── longestCommonPath.cpp ├── lowestCommonAncestor.cpp ├── luckyNumber.cpp ├── makeItConnected.cpp ├── makeNetworkConnected.cpp ├── makePalindrome.cpp ├── maximumNumberOfCoins.cpp ├── maximumNumberOfEventsThatCanBeAttended.cpp ├── maximumProductSubarray.cpp ├── maximumWhiteSubtree.cpp ├── maximumXorQueries.cpp ├── messageRoute.cpp ├── minCostToReachDestination.cpp ├── minimumSpanningTree.cpp ├── minimumXorSumOfTwoArrays.cpp ├── moduloSum.cpp ├── nimGame.cpp ├── nimGameII.cpp ├── numberOfWaysToWearDifferentHatsToEachOther.cpp ├── numbersWithRepeatedDigits.cpp ├── palindromePartitioning.cpp ├── pathXOR.cpp ├── perfectSquares.cpp ├── powerfulArray.cpp ├── rangeSetQuery.cpp ├── rangeUpdateQueries.cpp ├── reconstructItinerary.cpp ├── removeMaximumNumberOfEdges.cpp ├── restrictedPermutation.cpp ├── ringMst.cpp ├── sleepingSchedule.cpp ├── specialPath.cpp ├── stairGame.cpp ├── subsequences.cpp ├── subtreeProblem.cpp ├── sumOfMaximumWeights.cpp ├── travelByCar.cpp ├── treeWithMaximumCost.cpp ├── validBfs.cpp ├── wordLadder.cpp └── wordSearch.cpp └── Java Codes ├── javaCodes.iml ├── out └── production │ └── javaCodes │ ├── AtcoderDP │ ├── Candies.class │ ├── Coins.class │ ├── Deque1.class │ ├── Deque2.class │ ├── DigitSum.class │ ├── Flowers$segmentTree.class │ ├── Flowers.class │ ├── Frog1Recursive.class │ ├── Frog1Tabulation.class │ ├── Frog2Tracing.class │ ├── Grid1.class │ └── IndependentSet.class │ ├── DFSTreesAndBackEdges │ ├── DfsTreesAndBackEdges.class │ ├── DfsTreesAndBackEdgesDirectedGraph.class │ └── PrintCycleInGraph.class │ ├── DivideAndConquer │ └── TernarySearch.class │ ├── FenwickTrees │ ├── FenwickTrees$fenwick.class │ ├── FenwickTrees.class │ ├── InversionCountsUsingFenwick$1.class │ ├── InversionCountsUsingFenwick$Pair.class │ ├── InversionCountsUsingFenwick$fenwick.class │ └── InversionCountsUsingFenwick.class │ ├── InclusionExclusion │ └── TotalDivisorInclusion.class │ ├── LinearRecurrenceAndMatrixExponentiation │ ├── BinaryExp.class │ ├── FastMultiplication.class │ ├── MatrixExp$Mat.class │ ├── MatrixExp.class │ └── ModularExp.class │ ├── RandomizeAndTime │ └── RandomAndTime.class │ ├── SegmentTree │ ├── SegmentTree$segmentTree.class │ ├── SegmentTree.class │ ├── SegmentTreeLazyProp$segmentTree.class │ └── SegmentTreeLazyProp.class │ ├── StringMatching │ ├── BruteForce.class │ ├── PolyHash.class │ ├── RabinCarb.class │ ├── Trie$Node.class │ ├── Trie.class │ ├── TriePatternMatching$Node.class │ └── TriePatternMatching.class │ ├── TheoremsInNumberTheory │ ├── ModuloArithmetics.class │ └── Totient.class │ ├── Trees │ └── AncestorPrinting.class │ └── main.class └── src ├── AtcoderDP ├── Candies.java ├── Coins.java ├── Deque1.java ├── Deque2.java ├── DigitSum.java ├── Flowers.java ├── Frog1Recursive.java ├── Frog1Tabulation.java ├── Frog2Tracing.java ├── Grid1.java ├── IndependentSet.java ├── Knapsack1.java ├── Knapsack2.java ├── LCS.java ├── LongestPath.java ├── Permutation.java ├── Slimes.java ├── Stones.java ├── Sushi.java ├── TabulationVsMemorisation.java ├── Vacation.java └── Walk.java ├── ConvexHull ├── ConvexHullUsingCollinearPoints.java └── GrahamScan.java ├── DFSTreesAndBackEdges ├── DfsTreesAndBackEdges.java ├── DfsTreesAndBackEdgesDirectedGraph.java └── PrintCycleInGraph.java ├── DivideAndConquer └── TernarySearch.java ├── FenwickTrees ├── FenwickTrees.java └── InversionCountsUsingFenwick.java ├── InclusionExclusion └── TotalDivisorInclusion.java ├── InteractiveProblems ├── GuessTheNumber.java ├── LostNumber.java └── XorGuessing.java ├── LCA ├── SparseTable.java └── TwoPointers.java ├── LinearRecurrenceAndMatrixExponentiation ├── BinaryExp.java ├── FastMultiplication.java ├── MatrixExp.java └── ModularExp.java ├── RandomizeAndTime └── RandomAndTime.java ├── SegmentTree ├── SegmentTree.java └── SegmentTreeLazyProp.java ├── SquareRootDecomposition ├── DQuerySPOJ.java ├── SortingBasedOnMos.java └── SqrtDecomposition.java ├── StringMatching ├── BruteForce.java ├── PolyHash.java ├── RabinCarb.java ├── Trie.java └── TriePatternMatching.java ├── StronglyConnectedComponents ├── Kosaraju.java └── TopologicalOrder.java ├── TheoremsInNumberTheory └── ModuloArithmetics.java └── Trees └── AncestorPrinting.java /Coding Exercise Solutions/Alice Problem.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | long long inv; 4 | 5 | void cal(int l,int mid,int r, vector&arr){ 6 | int ptr1=l,ptr2=mid+1; 7 | vectortemp(r-l+1); 8 | int ind=0; 9 | while(ptr1<=mid && ptr2<=r){ 10 | if(arr[ptr1]<=arr[ptr2]){ 11 | temp[ind++]=arr[ptr1++]; 12 | inv+=ptr2-(mid+1); 13 | } 14 | else{ 15 | temp[ind++]=arr[ptr2++]; 16 | } 17 | } 18 | while(ptr1<=mid){ 19 | temp[ind++]=arr[ptr1++]; 20 | inv+=ptr2-(mid+1); 21 | } 22 | while(ptr2<=r){ 23 | temp[ind++]=arr[ptr2++]; 24 | } 25 | ind=0; 26 | for(int i=l;i<=r;i++){ 27 | arr[i]=temp[ind++]; 28 | } 29 | } 30 | 31 | void merge(int l,int r,vector &arr){ 32 | if(larr){ 42 | inv=0; 43 | merge(0,n-1,arr); 44 | return inv; 45 | } 46 | -------------------------------------------------------------------------------- /Coding Exercise Solutions/Alice Studies Informatics.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int solve(int l,int r,int x,int y){ 5 | if(y%x!=0) 6 | { 7 | return 0; 8 | } 9 | int ans=0; 10 | l=(l+x-1)/x; 11 | r/=x; 12 | y/=x; 13 | for(int i=1;i<=sqrt(y);i++) 14 | { 15 | if(y%i==0) 16 | { 17 | int val1=i,val2=y/i; 18 | int gcd=__gcd(val1,val2); 19 | if(gcd==1 && (val1>=l && val1<=r) && (val2>=l && val2<=r)){ 20 | ans++; 21 | if(val1!=val2){ 22 | ans++; 23 | } 24 | } 25 | } 26 | } 27 | return ans; 28 | } -------------------------------------------------------------------------------- /Coding Exercise Solutions/Alice and Candies.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int solve(int n) 5 | { 6 | int ans=0; 7 | for(int i=1;i<=sqrt(n);i++) 8 | { 9 | if(n%i==0) 10 | { 11 | int x=i,y=n/i; 12 | if((x+y)%2==0) 13 | { 14 | ans++; 15 | } 16 | } 17 | } 18 | return ans; 19 | } -------------------------------------------------------------------------------- /Coding Exercise Solutions/Array Stabilization.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | vectorseg; 5 | void build(int v,int l,int r,vector&a){ 6 | if(l==r) 7 | { 8 | seg[v]=a[l]; 9 | return; 10 | } 11 | int mid=(l+r)/2; 12 | build(2*v,l,mid,a); 13 | build(2*v+1,mid+1,r,a); 14 | seg[v]=__gcd(seg[2*v],seg[2*v+1]); 15 | } 16 | int query(int v,int l,int r,int x,int y) 17 | { 18 | if(y=r) 23 | { 24 | return seg[v]; 25 | } 26 | int mid=(l+r)/2; 27 | return __gcd(query(2*v,l,mid,x,min(mid,y)), query(2*v+1,mid+1,r,max(x,mid+1),y)); 28 | } 29 | int solve(int n, vectora){ 30 | vectorarr(2*n); 31 | seg=vector(8*n+1); 32 | for(int i=0;i 2 | using namespace std; 3 | #define maxn 524288 4 | 5 | int seg[maxn]; 6 | void build(int v,int nl,int nr,int val, vector&a) 7 | { 8 | if(nl==nr) 9 | { 10 | seg[v]=a[nl]; 11 | } 12 | else 13 | { 14 | int mid=(nl+nr)/2; 15 | build(2*v,nl,mid,val-1,a); 16 | build(2*v+1,mid+1,nr,val-1,a); 17 | if(val%2==0) 18 | { 19 | seg[v]=seg[2*v]^seg[2*v+1]; 20 | } 21 | else 22 | { 23 | seg[v]=seg[2*v]|seg[2*v+1]; 24 | } 25 | } 26 | } 27 | void update(int v,int nl,int nr ,int pos,int val,int cnt) 28 | { 29 | if(nl==nr) 30 | { 31 | seg[v]=val; 32 | } 33 | else 34 | { 35 | int mid=(nl+nr)/2; 36 | if(pos<=mid) 37 | { 38 | update(2*v,nl,mid,pos,val,cnt-1); 39 | } 40 | else 41 | { 42 | update(2*v+1,mid+1,nr,pos,val,cnt-1); 43 | } 44 | 45 | if(cnt%2==0) 46 | { 47 | seg[v]=seg[2*v] ^ seg[2*v+1]; 48 | } 49 | else 50 | { 51 | seg[v]=seg[2*v] | seg[2*v+1]; 52 | } 53 | } 54 | } 55 | 56 | vector solve(int n, vectora, vector> queries){ 57 | memset(seg,0,sizeof(seg)); 58 | int val=pow(2,n); 59 | build(1,0,val-1,n,a); 60 | vectorans; 61 | for(int i=0;i 2 | using namespace std; 3 | #define mod 1000000007 4 | #define ll long long 5 | 6 | ll power(ll a,ll n){ 7 | ll p; 8 | if(n==0){ 9 | return 1; 10 | } 11 | p=power(a,n/2); 12 | p=(p*p)%mod; 13 | if(n%2){ 14 | p=(p*a)%mod; 15 | } 16 | return p; 17 | } 18 | 19 | bool check(int sum,int a,int b){ 20 | while(sum!=0){ 21 | if(sum%10!=a && sum%10!=b){ 22 | return false; 23 | } 24 | sum/=10; 25 | } 26 | return true; 27 | } 28 | 29 | int solve(int a,int b,int n){ 30 | int ans=0,sum=0; 31 | ll fact[n+1]; 32 | fact[0]=1; 33 | for(int i=1;i<=n;i++){ 34 | fact[i]=(fact[i-1]*(ll)i)%mod; 35 | } 36 | for(int i=0;i<=n;i++){ 37 | sum=(a*i)+ ((n-i)*b); 38 | if(check(sum,a,b)){ 39 | ll cal=fact[n]; 40 | cal=(cal*power(fact[i],mod-2))%mod; 41 | cal=(cal*power(fact[n-i],mod-2))%mod; 42 | ans=(ans+cal)%mod; 43 | } 44 | } 45 | return ans; 46 | } 47 | -------------------------------------------------------------------------------- /Coding Exercise Solutions/Bitwise AND of Range of Numbers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int rangeBitwiseAnd(int m, int n) { 5 | int diff=n-m,ans=0; 6 | for(int i=0;i<31;i++) 7 | { 8 | int val=1< 2 | using namespace std; 3 | #define mod 1000000007 4 | 5 | long long power(long long a,int n){ 6 | long long p; 7 | if(n==0){ 8 | return 1; 9 | } 10 | p=power(a,n/2); 11 | p=(p*p)%mod; 12 | if(n%2){ 13 | p=(p*a)%mod; 14 | } 15 | return p; 16 | } 17 | 18 | int solve(int n){ 19 | int ans=1; 20 | long long x=power(26,mod-2); 21 | long long y=(2*power(25,mod-2))%mod; 22 | y=(y*(1+mod-power(x,n/2)))%mod; 23 | ans=(ans+y)%mod; 24 | if(n%2==0){ 25 | ans=(ans+mod-power(x,n/2))%mod; 26 | } 27 | return ans; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /Coding Exercise Solutions/Bob and his Brother.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define inf 1e18 4 | 5 | long long check(vector&a ,vector&b, int x){ 6 | long long cnt=0; 7 | for(auto itr:a){ 8 | if(itr>x){ 9 | break; 10 | } 11 | cnt+=abs(itr-x); 12 | } 13 | for(int i=b.size()-1;i>=0;i--){ 14 | if(b[i] a, vector b){ 23 | sort(a.begin(),a.end()); 24 | sort(b.begin(),b.end()); 25 | int l=1,r=1e9,mid1,mid2; 26 | long long ans=inf; 27 | while(l<=r){ 28 | mid1=l+(r-l)/3; 29 | mid2=r-(r-l)/3; 30 | long long x=check(a,b,mid1); 31 | long long y=check(a,b,mid2); 32 | if(xy){ 37 | l=mid1+1; 38 | } 39 | else{ 40 | ans=min(ans,x); 41 | l=mid1+1; 42 | r=mid2-1; 43 | } 44 | } 45 | return ans; 46 | } 47 | -------------------------------------------------------------------------------- /Coding Exercise Solutions/Bulbs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int solve(int n, vector a){ 5 | int turn=0,ans=0; 6 | for(int i=0;i 2 | using namespace std; 3 | 4 | string getHint(string secret, string guess) { 5 | int n=secret.size(); 6 | int bull=0,cows=0; 7 | mapcnt1,cnt2; 8 | for(int i=0;i 2 | using namespace std; 3 | 4 | vector closestDivisors(int num) { 5 | vectorarr1,arr2; 6 | for(int i=sqrt(num+1);i>=1;i--) 7 | { 8 | if((num+1)%i==0) 9 | { 10 | arr1={i,(num+1)/i}; 11 | break; 12 | } 13 | } 14 | for(int i=sqrt(num+2);i>=1;i--) 15 | { 16 | if((num+2)%i==0) 17 | { 18 | arr2={i,(num+2)/i}; 19 | break; 20 | } 21 | } 22 | sort(arr1.begin(),arr1.end()); 23 | sort(arr2.begin(),arr2.end()); 24 | return (arr1[1]-arr1[0])< (arr2[1]-arr2[0])? arr1:arr2; 25 | } -------------------------------------------------------------------------------- /Coding Exercise Solutions/Coprime Divisor.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int gcd(int a,int b) 5 | { 6 | if(a 2 | using namespace std; 3 | 4 | int countPrimes(int n) { 5 | if(n==0 || n==1) 6 | { 7 | return 0; 8 | } 9 | vectorprime(n,1); 10 | prime[0]=0; 11 | prime[1]=0; 12 | for(int i=2;i*i 2 | using namespace std; 3 | #define mod 998244353 4 | #define ll long long 5 | 6 | ll power(ll a,ll n){ 7 | ll p; 8 | if(n==0){ 9 | return 1; 10 | } 11 | p=power(a,n/2); 12 | p=(p*p)%mod; 13 | if(n%2){ 14 | p=(p*a)%mod; 15 | } 16 | return p; 17 | } 18 | 19 | int NcR(int n,int r,vector&fact){ 20 | ll total=fact[n]; 21 | total=(total*power(fact[r],mod-2))%mod; 22 | total=(total*power(fact[n-r],mod-2))%mod; 23 | return total; 24 | } 25 | 26 | int solve(int n,int m){ 27 | int ans=0; 28 | vectorfact(m+1,1); 29 | for(int i=1;i<=m;i++){ 30 | fact[i]=(fact[i-1]*i)%mod; 31 | } 32 | if(n>2){ 33 | ans=((ll)NcR(m,n-1,fact)*power(2,n-3))%mod; 34 | ans=((ll)ans*(ll)(n-2))%mod; 35 | } 36 | return ans; 37 | } 38 | -------------------------------------------------------------------------------- /Coding Exercise Solutions/Decode Permutation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | vector decode(vector encoded) { 5 | int val=0,val1=0,n=encoded.size(); 6 | vectorans(n+1); 7 | for(int i=1;i<=n+1;i++) 8 | { 9 | val1^=i; 10 | } 11 | for(int i=0;i 2 | using namespace std; 3 | 4 | vector decode(vector encoded, int first) { 5 | int n=encoded.size(); 6 | vectorans(n+1); 7 | ans[0]=first; 8 | int pre=ans[0]; 9 | for(int i=1;i<=n;i++) 10 | { 11 | ans[i]=pre^encoded[i-1]; 12 | pre=ans[i]; 13 | } 14 | return ans; 15 | } -------------------------------------------------------------------------------- /Coding Exercise Solutions/Decreasing Sequence.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int solve(int L,int R){ 5 | if(R-L+1>L){ 6 | return -1; 7 | } 8 | return R; 9 | } -------------------------------------------------------------------------------- /Coding Exercise Solutions/Distinct Candies.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int distributeCandies(vector candies) { 5 | unordered_sets; 6 | for(auto itr:candies) 7 | { 8 | s.insert(itr); 9 | } 10 | int ans=min(s.size(),candies.size()/2); 11 | return ans; 12 | } -------------------------------------------------------------------------------- /Coding Exercise Solutions/Divisibility.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int numDivisible(int l,int r, vector arr){ 5 | int ans=0,n=arr.size(); 6 | 7 | for(int i=1;ir){ 17 | break; 18 | } 19 | } 20 | if(lcm>r) 21 | continue; 22 | 23 | if(cnt%2){ 24 | ans+=r/lcm; 25 | ans-=(l-1)/lcm; 26 | } 27 | else{ 28 | ans-=r/lcm; 29 | ans+=(l-1)/lcm; 30 | } 31 | } 32 | return ans; 33 | } -------------------------------------------------------------------------------- /Coding Exercise Solutions/Factorization.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define mod 1000000007 4 | #define ll long long 5 | #define maxn 150001 6 | 7 | ll power(ll a,ll n){ 8 | ll p; 9 | if(n==0){ 10 | return 1; 11 | } 12 | p=power(a,n/2); 13 | p=(p*p)%mod; 14 | if(n%2){ 15 | p=(p*a)%mod; 16 | } 17 | return p; 18 | } 19 | 20 | int NcR(int n,int r,vector&fact){ 21 | ll total=fact[n]; 22 | total=(total*power(fact[r],mod-2))%mod; 23 | total=(total*power(fact[n-r],mod-2))%mod; 24 | return total; 25 | } 26 | 27 | int solve(int n,int m){ 28 | int ans=1; 29 | vectorfact(maxn,1); 30 | for(int i=1;iprimes; 34 | for(int i=2;i<=sqrt(m);i++){ 35 | while(m%i==0){ 36 | primes[i]++; 37 | m/=i; 38 | } 39 | } 40 | if(m>1){ 41 | primes[m]++; 42 | } 43 | for(auto itr:primes){ 44 | ans=((ll)ans*(ll)NcR(n+itr.second-1,n-1,fact))%mod; 45 | } 46 | return ans; 47 | } -------------------------------------------------------------------------------- /Coding Exercise Solutions/Find K Closest Elements.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool mycompare(int a,int b){ 5 | return a>b; 6 | } 7 | 8 | vector findClosestElements(vector arr, int k, int x) { 9 | vectorans; 10 | priority_queue,vector>, greater>>q; 11 | int n=arr.size(); 12 | for(int i=0;i 3 | using namespace std; 4 | 5 | int firstUniqChar(string s) 6 | { 7 | unordered_mapcnt; 8 | int n=s.size(); 9 | 10 | for(int i=0;i 2 | using namespace std; 3 | vectorseg,lazy; 4 | 5 | void update(int v,int l,int r,int x,int y){ 6 | 7 | if(lazy[v]){ 8 | seg[v]=(r-l+1)-seg[v]; 9 | if(l!=r){ 10 | lazy[2*v]^=1; 11 | lazy[2*v+1]^=1; 12 | } 13 | lazy[v]=0; 14 | } 15 | if(y=r){ 19 | seg[v]=(r-l+1)-seg[v]; 20 | if(l!=r){ 21 | lazy[2*v]^=1; 22 | lazy[2*v+1]^=1; 23 | } 24 | return; 25 | } 26 | int mid=(l+r)/2; 27 | update(2*v,l,mid,x,min(mid,y)); 28 | update(2*v+1,mid+1,r,max(x,mid+1),y); 29 | seg[v]=seg[2*v]+seg[2*v+1]; 30 | } 31 | 32 | int range(int v,int l,int r,int x,int y){ 33 | if(y=r){ 45 | return seg[v]; 46 | } 47 | int mid=(l+r)/2; 48 | return range(2*v,l,mid,x,min(y,mid))+range(2*v+1,mid+1,r,max(mid+1,x),y); 49 | } 50 | 51 | vector solve(int n,int q, vector> query){ 52 | seg=lazy=vector(4*n+1); 53 | vectorans; 54 | for(int i=0;i 2 | using namespace std; 3 | 4 | int gcd(int a,int b){ 5 | if(a nums) { 14 | int ans=0; 15 | for(auto itr:nums) 16 | { 17 | ans=gcd(itr,ans); 18 | } 19 | return ans==1; 20 | } -------------------------------------------------------------------------------- /Coding Exercise Solutions/Greatest Common Divisor.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int solve(int a,int b){ 5 | if(a 2 | using namespace std; 3 | 4 | int hammingDistance(int x, int y) { 5 | int val=x^y,ans=0; 6 | while(val) 7 | { 8 | val=val&(val-1); 9 | ans++; 10 | } 11 | return ans; 12 | } -------------------------------------------------------------------------------- /Coding Exercise Solutions/Koko Eating Bananas.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int minEatingSpeed(vector piles, int h) { 5 | int n=piles.size(); 6 | int l=1,r=*max_element(piles.begin(),piles.end()),mid,ans; 7 | while(l<=r){ 8 | mid=(l+r)/2; 9 | int cnt=0; 10 | for(int i=0;i 2 | using namespace std; 3 | 4 | int findKthLargest(vector nums, int k) { 5 | priority_queue, greater> pq; 6 | for (int num : nums) { 7 | pq.push(num); 8 | if (pq.size() > k) { 9 | pq.pop(); 10 | } 11 | } 12 | return pq.top(); 13 | } -------------------------------------------------------------------------------- /Coding Exercise Solutions/LEQ.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define mod 998244353 4 | 5 | long long power(long long a,int n){ 6 | long long p; 7 | if(n==0) 8 | { 9 | return 1; 10 | } 11 | p=power(a,n/2); 12 | p=(p*p)%mod; 13 | if(n%2) 14 | { 15 | p=(p*a)%mod; 16 | } 17 | return p; 18 | } 19 | 20 | void update(int ind,int n,int val, vector&bit){ 21 | while(ind<=n){ 22 | bit[ind]=(bit[ind]+val)%mod; 23 | ind+=ind&-ind; 24 | } 25 | } 26 | 27 | int get(int ind, vector&bit){ 28 | int ans=0; 29 | while(ind>0){ 30 | ans=(ans+bit[ind])%mod; 31 | ind-=ind&-ind; 32 | } 33 | return ans; 34 | } 35 | 36 | int solve(int n,vector a){ 37 | vectorbit(n+1); 38 | mapmapping; 39 | for(int i=0;i 2 | using namespace std; 3 | 4 | int n; 5 | vectormapping={"abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"}; 6 | void solve(int pos, string &cur, vector&ans,string &digits) 7 | { 8 | if(pos==n) 9 | { 10 | ans.push_back(cur); 11 | return; 12 | } 13 | for(char ch: mapping[digits[pos]-'2']) 14 | { 15 | cur.push_back(ch); 16 | solve(pos+1,cur,ans,digits); 17 | cur.pop_back(); 18 | } 19 | } 20 | vector letterCombinations(string digits) { 21 | vectorans; 22 | n=digits.size(); 23 | if(n==0) 24 | { 25 | return ans; 26 | } 27 | string cur; 28 | solve(0,cur, ans,digits); 29 | sort(ans.begin(),ans.end()); 30 | return ans; 31 | } -------------------------------------------------------------------------------- /Coding Exercise Solutions/Logs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int solve(int n,int k,vector a){ 5 | int l=1,r=1e9,mid,ans; 6 | while(l<=r){ 7 | mid=(l+r)/2; 8 | int sum=0; 9 | for(int i=0;i 2 | using namespace std; 3 | 4 | int solve(int n) { 5 | int ans=0,cnt=0; 6 | for(int i=0;i<31;i++) 7 | { 8 | int val=1< 2 | using namespace std; 3 | #define mod 1000000007 4 | 5 | long long power(long long a,long long n) 6 | { 7 | long long p; 8 | if(n==0) 9 | { 10 | return 1; 11 | } 12 | p=power(a,n/2); 13 | p=(p*p)%mod; 14 | if(n%2) 15 | { 16 | p=(p*a)%mod; 17 | } 18 | return p; 19 | } 20 | 21 | int locker(int n){ 22 | int ans; 23 | if(n<2) 24 | { 25 | ans=n; 26 | } 27 | else{ 28 | int cnt=n/3,rem=n%3; 29 | if(rem==1) 30 | { 31 | ans=(power(3,cnt-1)*4)%mod; 32 | } 33 | else 34 | if(rem==2){ 35 | ans=(power(3,cnt)*2)%mod; 36 | } 37 | else{ 38 | ans=power(3,cnt); 39 | } 40 | } 41 | return ans; 42 | } -------------------------------------------------------------------------------- /Coding Exercise Solutions/Make It Equal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define maxn 100001 4 | 5 | int solve(int n,int k,vector h){ 6 | int cnt[maxn]={0}; 7 | for(int i=0;i=0;i--){ 17 | if(cnt[i]==n){ 18 | ans+=(sum>0); 19 | break; 20 | } 21 | if(sum+ cnt[i]>k) 22 | { 23 | ans++; 24 | sum=cnt[i]; 25 | continue; 26 | } 27 | sum+=cnt[i]; 28 | } 29 | return ans; 30 | } -------------------------------------------------------------------------------- /Coding Exercise Solutions/Math.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | vector solve(int n){ 5 | mapprimes; 6 | for(int i=2;i<=sqrt(n);i++) 7 | { 8 | while(n%i==0) 9 | { 10 | n/=i; 11 | primes[i]++; 12 | } 13 | } 14 | if(n>1) 15 | { 16 | primes[n]++; 17 | } 18 | int value=1,x=0; 19 | for(auto itr: primes){ 20 | x=max(x,(int)ceil(log2(itr.second))); 21 | value*=itr.first; 22 | } 23 | int cnt=0,val=1<ans={value,x}; 31 | return ans; 32 | } -------------------------------------------------------------------------------- /Coding Exercise Solutions/Maximum Score From Removing Stones.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int maximumScore(int a, int b, int c) { 5 | int ans=0; 6 | priority_queueq; 7 | q.push(a); 8 | q.push(b); 9 | q.push(c); 10 | while(q.size()>1) 11 | { 12 | int x=q.top(); 13 | q.pop(); 14 | int y=q.top(); 15 | q.pop(); 16 | x--; 17 | y--; 18 | if(x){ 19 | q.push(x); 20 | } 21 | if(y){ 22 | q.push(y); 23 | } 24 | ans++; 25 | } 26 | return ans; 27 | } -------------------------------------------------------------------------------- /Coding Exercise Solutions/Maximum Score.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int matrixScore(vector> grid) { 5 | int ans=0,n=grid.size(),m=grid[0].size(); 6 | ans=(1<<(m-1))*n; 7 | for(int j=1;j 2 | using namespace std; 3 | 4 | vector subset(vector &v){ 5 | vectorresult; 6 | int n=v.size(); 7 | for(int i=0;i arr){ 21 | vectorleft,right; 22 | for(int i=0;ileft_sum=subset(left); 31 | vectorright_sum=subset(right); 32 | 33 | sort(right_sum.begin(),right_sum.end()); 34 | long long ans=0; 35 | 36 | for(int i=0;i 2 | using namespace std; 3 | 4 | int minTaps(int n, vector ranges) { 5 | vector>v; 6 | for(int i=0;i<=n;i++) 7 | { 8 | v.push_back({i-ranges[i],i+ranges[i]}); 9 | } 10 | sort(v.begin(),v.end()); 11 | int s=0,e=0,ind=0; 12 | int ans=0; 13 | while(s 2 | using namespace std; 3 | 4 | int solve(vector nums) { 5 | priority_queue, greater> pq; 6 | int ans = 0; 7 | for (int i = 0; i < nums.size(); i++) { 8 | pq.push(nums[i]); 9 | } 10 | int a, b, sum; 11 | while (pq.size() > 1) { 12 | a = pq.top(); 13 | pq.pop(); 14 | b = pq.top(); 15 | pq.pop(); 16 | sum = a + b; 17 | ans += sum; 18 | pq.push(sum); 19 | } 20 | return ans; 21 | } -------------------------------------------------------------------------------- /Coding Exercise Solutions/POW.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | string solve(int A,int B, int C){ 5 | int sign1=(A<0 && C%2); 6 | int sign2=(B<0 && C%2); 7 | string ans; 8 | if(sign1!=sign2){ 9 | ans=sign1? "<": ">"; 10 | } 11 | else{ 12 | if(abs(A)==abs(B)){ 13 | ans="="; 14 | } 15 | else if((abs(A) 2 | using namespace std; 3 | 4 | bool palindrome(string &str){ 5 | string temp=str; 6 | reverse(temp.begin(),temp.end()); 7 | return temp==str; 8 | } 9 | 10 | void solve(string &str, int pos,vector&cur, vector>&ans){ 11 | if(pos>=str.size()){ 12 | ans.push_back(cur); 13 | return; 14 | } 15 | for(int i=pos;i> partition(string s) { 26 | vector>ans; 27 | vectorcur; 28 | solve(s,0,cur,ans); 29 | return ans; 30 | } -------------------------------------------------------------------------------- /Coding Exercise Solutions/Petya and Array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define maxn 100001 4 | #define fi first 5 | #define se second 6 | 7 | int seg[4*maxn]; 8 | void update(int v,int l,int r,int val,int pos) 9 | { 10 | if(l==r) 11 | { 12 | seg[v]=val; 13 | return; 14 | } 15 | int mid=(l+r)/2; 16 | if(pos<=mid) 17 | { 18 | update(2*v,l,mid,val,pos); 19 | } 20 | else{ 21 | update(2*v+1,mid+1,r,val,pos); 22 | } 23 | seg[v]=seg[2*v]+seg[2*v+1]; 24 | } 25 | int query(int v,int l,int r,int x,int y) 26 | { 27 | if(y&p ,const pair&p1) 39 | { 40 | if(p.fi==p1.fi) 41 | { 42 | return p.sea){ 47 | memset(seg,0,sizeof(seg)); 48 | long long x,y,ans=0; 49 | vector>v; 50 | vectorpre(n+1); 51 | for(int i=0;i 2 | using namespace std; 3 | 4 | int solve(int n,int k,vector> grid){ 5 | int l=0,r=1e9,mid,ans=-1; 6 | int x=(k*k)/2; 7 | x++; 8 | while(l<=r){ 9 | mid=(l+r)/2; 10 | vector>pre(n,vector(n)); 11 | for(int i=0;imid){ 14 | pre[i][j]=1; 15 | } 16 | if(i-1>=0){ 17 | pre[i][j]+=pre[i-1][j]; 18 | } 19 | if(j-1>=0){ 20 | pre[i][j]+=pre[i][j-1]; 21 | } 22 | if(i-1>=0 && j-1>=0){ 23 | pre[i][j]-=pre[i-1][j-1]; 24 | } 25 | } 26 | } 27 | int flag=0; 28 | for(int i=k-1;i=0){ 32 | val-=pre[i-k][j]; 33 | } 34 | if(j-k>=0){ 35 | val-=pre[i][j-k]; 36 | } 37 | if(i-k>=0 && j-k>=0){ 38 | val+=pre[i-k][j-k]; 39 | } 40 | if(val 2 | using namespace std; 3 | 4 | int power(int a,int n){ 5 | if(n==0){ 6 | return 1; 7 | } 8 | return a*power(a,n-1); 9 | } 10 | 11 | int cal(int x,int n, vector&vec){ 12 | int sum=0,answer=0; 13 | for(auto itr:vec){ 14 | sum+=power(itr,n); 15 | } 16 | if(sum==x){ 17 | return 1; 18 | } 19 | else{ 20 | int answer=0; 21 | int v=vec.empty()?1:vec.back()+1; 22 | while(sum+ power(v,n)<=x){ 23 | vec.push_back(v); 24 | answer+=cal(x,n,vec); 25 | vec.pop_back(); 26 | v++; 27 | } 28 | return answer; 29 | } 30 | } 31 | 32 | int solve(int x,int n){ 33 | vectorvec; 34 | return cal(x,n,vec); 35 | } 36 | 37 | -------------------------------------------------------------------------------- /Coding Exercise Solutions/Power of Four.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool isPowerOfFour(int n) { 5 | if(n<=0) 6 | { 7 | return false; 8 | } 9 | return (n&(n-1))==0 && (1431655765&n)==n; 10 | } -------------------------------------------------------------------------------- /Coding Exercise Solutions/Prime Sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int solve(int n){ 5 | vectorprime(n+1,1); 6 | for(int i=2;i*i<=n;i++){ 7 | if(prime[i]){ 8 | for(int j=i*i;j<=n;j+=i){ 9 | prime[j]=0; 10 | } 11 | } 12 | } 13 | int ans=0; 14 | for(int i=3;i<=n-2;i++){ 15 | if(prime[i]==1 && prime[i+2]==1){ 16 | ans++; 17 | } 18 | } 19 | return ans; 20 | } -------------------------------------------------------------------------------- /Coding Exercise Solutions/Range Minimum Queries.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define maxn 100001 4 | #define inf 2e9 5 | 6 | int seg[4*maxn]; 7 | void build(int v,int l,int r, vector&a){ 8 | if(l==r) 9 | { 10 | seg[v]=a[l]; 11 | return; 12 | } 13 | int mid=(l+r)/2; 14 | build(2*v,l,mid,a); 15 | build(2*v+1,mid+1,r,a); 16 | seg[v]=min(seg[2*v],seg[2*v+1]); 17 | } 18 | int query(int v,int l,int r,int x,int y) 19 | { 20 | if(y=x) 40 | { 41 | update(2*v,l,mid,x,val); 42 | } 43 | else 44 | { 45 | update(2*v+1,mid+1,r,x,val); 46 | } 47 | seg[v]=min(seg[2*v],seg[2*v+1]); 48 | } 49 | vector solve(int n, vectorarr, vector> queries){ 50 | memset(seg,0,sizeof(seg)); 51 | build(1,0,n-1,arr); 52 | vectorans; 53 | for(int i=0;i 2 | using namespace std; 3 | #define maxn 100001 4 | 5 | int seg[4*maxn]; 6 | void build(int v,int l,int r, vector &a){ 7 | if(l==r) 8 | { 9 | seg[v]=a[l]; 10 | return; 11 | } 12 | int mid=(l+r)/2; 13 | build(2*v,l,mid,a); 14 | build(2*v+1,mid+1,r,a); 15 | seg[v]=seg[2*v]^seg[2*v+1]; 16 | } 17 | int query(int v,int l,int r,int x,int y) 18 | { 19 | if(ysolve(int n, vectora, vector> queries){ 31 | memset(seg,0,sizeof(seg)); 32 | build(1,0,n-1,a); 33 | vectorans; 34 | for(int i=0;i 2 | using namespace std; 3 | #define maxn 100001 4 | 5 | string solve(int n,int k, vector c){ 6 | int spf[maxn],cnt[maxn]; 7 | for(int i=0;i 2 | using namespace std; 3 | int ans; 4 | void checkCount(vector&nums,int l,int mid,int r){ 5 | int leftend=l,rightend=mid+1,ptr=mid+1,cnt=0,ind=0; 6 | vectortemp(r-l+1); 7 | while(leftend<=mid){ 8 | while(rightend<=r && nums[leftend]> (long long)2*(long long)nums[rightend]){ 9 | cnt++; 10 | rightend++; 11 | } 12 | ans+=cnt; 13 | while(ptr<=r && nums[leftend]>=nums[ptr]){ 14 | temp[ind++]=nums[ptr++]; 15 | } 16 | temp[ind++]=nums[leftend++]; 17 | } 18 | while(ptr<=r){ 19 | temp[ind++]=nums[ptr++]; 20 | } 21 | for(int i=0;i&nums,int l,int r){ 27 | if(l nums) { 37 | int n=nums.size(); 38 | if(n==0){ 39 | return 0; 40 | } 41 | ans=0; 42 | mergeSort(nums,0,n-1); 43 | return ans; 44 | } 45 | -------------------------------------------------------------------------------- /Coding Exercise Solutions/Row Gcd.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | vector solve(int n,int m,vectora, vectorb) 5 | { 6 | vectorans(m); 7 | int gcd=0; 8 | for(int i=1;i 2 | using namespace std; 3 | #define mod 1000000007 4 | 5 | long long power(long long a,int n){ 6 | long long p; 7 | if(n==0){ 8 | return 1; 9 | } 10 | p=power(a,n/2); 11 | p=(p*p)%mod; 12 | if(n%2){ 13 | p=(p*a)%mod; 14 | } 15 | return p; 16 | } 17 | 18 | int solve(int n,vector a){ 19 | vectorans(n); 20 | sort(a.begin(),a.end(),greater()); 21 | ans[0]=a[0]; 22 | ans[1]=a[1]; 23 | for(long long i=2;i 2 | using namespace std; 3 | #define inf 2e9 4 | 5 | int solve(int n,int k, int l1,int r1,int l2, int r2){ 6 | int ans=inf; 7 | if(max(l1,l2)<=min(r1,r2)){ 8 | long long rem=max((long long)0,k- (long long)n* (long long)(min(r1,r2)-max(l1,l2))); 9 | long long maxPossible=(long long)n* (long long)(abs(l1-l2)+abs(r1-r2)); 10 | ans=min(rem, maxPossible) + max((long long)0, rem-maxPossible)*2; 11 | } 12 | else{ 13 | long long invest=max(l1,l2)-min(r1,r2); 14 | for(int i=1;i<=n;i++){ 15 | long long curans=invest*i; 16 | long long maxPossible=(long long)(max(r1,r2)-min(l1,l2))*(long long)i; 17 | curans+=min((long long)k, maxPossible) + max((long long)0, k-maxPossible)*2; 18 | ans=min((long long)ans,curans); 19 | } 20 | } 21 | return ans; 22 | } 23 | -------------------------------------------------------------------------------- /Coding Exercise Solutions/Selection of Personnel.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | long long solve(int n){ 5 | vector>precomp(n+1,vector(n+1,1)); 6 | for(int i=1;i<=n;i++){ 7 | for(int j=1;j 2 | using namespace std; 3 | #define mod 1000000 4 | 5 | int solve(int n){ 6 | vector>precomp(n+1,vector(n+1)); 7 | for(int i=0;i<=n;i++){ 8 | for(int j=0;j<=i;j++){ 9 | if(j==0){ 10 | precomp[i][j]=1; 11 | } 12 | else{ 13 | precomp[i][j]=(precomp[i-1][j]+precomp[i][j-1])%mod; 14 | } 15 | } 16 | } 17 | return precomp[n][n]; 18 | } -------------------------------------------------------------------------------- /Coding Exercise Solutions/Smallest Range Covering Elements from K Lists.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | vector smallestRange(vector> nums) { 5 | vector>vec; 6 | vectorres; 7 | int k=nums.size(); 8 | for(int i=0;icnt; 18 | int l=0,r=0; 19 | while(r 2 | using namespace std; 3 | 4 | bool comp(const int& a, const int& b){ 5 | int c1 = __builtin_popcount(a); 6 | int c2 = __builtin_popcount(b); 7 | if(c1 == c2) 8 | return a < b; 9 | return c1 < c2; 10 | } 11 | 12 | vector sortByBits(vector arr) { 13 | sort(arr.begin(),arr.end(), comp); 14 | return arr; 15 | } -------------------------------------------------------------------------------- /Coding Exercise Solutions/Strange Function.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | long long power9(long long a, long long n){ 5 | long long p; 6 | if(n==0){ 7 | return 1; 8 | } 9 | p=power9(a,n/2); 10 | p=(p*p)%9; 11 | if(n%2){ 12 | p=(p*a)%9; 13 | } 14 | return p; 15 | } 16 | int solve(long long A, long long N){ 17 | int ans= power9(A, N); 18 | if(!ans){ 19 | ans=9; 20 | } 21 | return ans; 22 | } -------------------------------------------------------------------------------- /Coding Exercise Solutions/Subarray Sum Divisible by N.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int solve(int n, vector nums){ 5 | vectormod(n); 6 | mod[0]++; 7 | int sum=0; 8 | for(int i=0;i 2 | using namespace std; 3 | 4 | bool check(long long x,long long y,long long p,long long q,long long val){ 5 | return (p*val>=x) && (q*val- p*val>=y-x); 6 | } 7 | 8 | long long solve(int x,int y,int p,int q){ 9 | int l=1,r=1e9,mid,ans; 10 | if(!check(x,y,p,q,r)){ 11 | return -1; 12 | } 13 | while(l<=r){ 14 | mid=(l+r)/2; 15 | if(check(x,y,p,q,mid)){ 16 | r=mid-1; 17 | ans=mid; 18 | } 19 | else{ 20 | l=mid+1; 21 | } 22 | } 23 | long long total=(long long)q*(long long )ans; 24 | total-=y; 25 | return total; 26 | } -------------------------------------------------------------------------------- /Coding Exercise Solutions/Sudoku Solver.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool isValid(vector>& matrix, int x, int y, int val) { 5 | for (int j = 0; j < 9; j++) { 6 | if (matrix[x][j] == val) return false; 7 | } 8 | 9 | for (int i = 0; i < 9; i++) { 10 | if (matrix[i][y] == val) return false; 11 | } 12 | 13 | int smi = x / 3 * 3; 14 | int smj = y / 3 * 3; 15 | 16 | for (int i = 0; i < 3; i++) { 17 | for (int j = 0; j < 3; j++) { 18 | if (matrix[smi + i][smj + j] == val) return false; 19 | } 20 | } 21 | 22 | return true; 23 | } 24 | 25 | bool solveSudoku(vector>& matrix, int i, int j) { 26 | if (i == 9) { 27 | return true; 28 | } 29 | 30 | int ni = 0, nj = 0; 31 | if (j == 8) { 32 | ni = i + 1; 33 | nj = 0; 34 | } else { 35 | ni = i; 36 | nj = j + 1; 37 | } 38 | 39 | if (matrix[i][j] != 0) { 40 | if (solveSudoku(matrix, ni, nj)) return true; 41 | } else { 42 | for (int po = 1; po <= 9; po++) { 43 | if (isValid(matrix, i, j, po)) { 44 | matrix[i][j] = po; 45 | if (solveSudoku(matrix, ni, nj)) return true; 46 | matrix[i][j] = 0; 47 | } 48 | } 49 | } 50 | 51 | return false; 52 | } 53 | vector> solve(vector> matrix) { 54 | solveSudoku(matrix,0,0); 55 | return matrix; 56 | } -------------------------------------------------------------------------------- /Coding Exercise Solutions/Target Sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int ans; 5 | void solve(int x,vector&nums,int sum,char ch,int val,int n) 6 | { 7 | if(x==n-1) 8 | { 9 | if(sum+(ch=='+'?nums[x]:-nums[x])==val) 10 | { 11 | ans++; 12 | } 13 | return; 14 | } 15 | solve(x+1,nums,sum+(ch=='+'?nums[x]:-nums[x]),'+',val,n); 16 | solve(x+1,nums,sum+(ch=='+'?nums[x]:-nums[x]),'-',val,n); 17 | } 18 | int findTargetSumWays(vector nums, int target) { 19 | ans=0; 20 | int n=nums.size (); 21 | solve(0,nums,0,'+',target,n); 22 | solve(0,nums,0,'-',target,n); 23 | return ans; 24 | } -------------------------------------------------------------------------------- /Coding Exercise Solutions/The World is a Theatre.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define maxn 31 4 | 5 | long long solve(int n,int m,int t){ 6 | vector>precomp(maxn,vector(maxn,1)); 7 | for(int i=1;i 2 | using namespace std; 3 | #define maxn 100005 4 | 5 | int solve(int n,int k,vector> segments){ 6 | vectorv[maxn]; 7 | for(int i=0;i>cur; 14 | for(int i=1;i0){ 17 | cur.insert({-segments[itr-1][1],itr}); 18 | } 19 | else{ 20 | cur.erase({-segments[abs(itr)-1][1],-itr}); 21 | } 22 | } 23 | while(cur.size()>k){ 24 | cnt++; 25 | auto itr=cur.begin(); 26 | cur.erase(itr); 27 | } 28 | } 29 | return cnt; 30 | } 31 | -------------------------------------------------------------------------------- /Coding Exercise Solutions/Total Hamming Distance.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int totalHammingDistance(vector nums) { 5 | int ans=0,n=nums.size(); 6 | for(int i=0;i<=30;i++) 7 | { 8 | int ones=0,zeros=0,val=1< 2 | using namespace std; 3 | 4 | int countTriplets(vector nums) { 5 | unordered_mapstore; 6 | int n=nums.size(); 7 | for(int i=0;i 2 | using namespace std; 3 | 4 | bool isPerfectSquare(int num) { 5 | long long l=1,r=num,mid; 6 | while(l<=r) 7 | { 8 | mid=(l+r)/2; 9 | if(mid*mid==num){ 10 | return true; 11 | } 12 | else if(mid*mid>num){ 13 | r=mid-1; 14 | } 15 | else{ 16 | l=mid+1; 17 | } 18 | } 19 | return false; 20 | } -------------------------------------------------------------------------------- /Coding Exercise Solutions/ashishAndAashika'sProblem.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define maxn 200001 4 | 5 | long long bit[maxn]; 6 | long long sum(long long idx) 7 | { 8 | long long ans=0; 9 | while(idx>0) 10 | { 11 | ans+=bit[idx]; 12 | idx-=idx&-idx; 13 | } 14 | return ans; 15 | } 16 | void update(long long x,long long val) 17 | { 18 | while(x a ){ 26 | memset(bit,0,sizeof(bit)); 27 | vectorl(n),r(n); 28 | mapm; 29 | for(int i=0;i=0;i--) 36 | { 37 | m[a[i]]++; 38 | r[i]=m[a[i]]; 39 | } 40 | long long ans=0; 41 | for(int i=n-1;i>=0;i--) 42 | { 43 | long long x=sum(l[i]-1); 44 | ans+=x; 45 | update(r[i],1); 46 | } 47 | return ans; 48 | } -------------------------------------------------------------------------------- /Coding Exercise Solutions/astronautPairs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Graph{ 5 | list *l; 6 | int V; 7 | public: 8 | Graph(int v){ 9 | V = v; 10 | //Array of Linked List 11 | l = new list[V]; 12 | } 13 | 14 | void addEdge(int i,int j,bool bidir=true){ 15 | l[i].push_back(j); 16 | if(bidir){ 17 | l[j].push_back(i); 18 | } 19 | } 20 | int traverseHelper(int s,bool *visited){ 21 | visited[s] = true; 22 | int size = 1; 23 | 24 | //visit the neighbours of s and thier neighbours recursilvely 25 | for(int nbr:l[s]){ 26 | if(!visited[nbr]){ 27 | size += traverseHelper(nbr,visited); 28 | } 29 | } 30 | return size; 31 | } 32 | //DFS - Depth First Search O(V+E) Linear 33 | int countAstronauts(){ 34 | bool *visited = new bool[V]{0}; 35 | int ans = V*(V-1)/2; 36 | 37 | for(int i=0;i > astronauts){ 52 | //complete this method 53 | Graph g(n); 54 | 55 | for(auto edge : astronauts){ 56 | g.addEdge(edge.first,edge.second); 57 | } 58 | 59 | return g.countAstronauts(); 60 | } 61 | -------------------------------------------------------------------------------- /Coding Exercise Solutions/barcode.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define inf 1e9 4 | 5 | int solve(int n,int m,int x,int y, vector> grid){ 6 | int cnt1=0,cnt2=0; 7 | vectorblack(m+1),white(m+1); 8 | for(int i=0;idp1(m+1,inf),dp2(m+1,inf); 30 | dp1[0]=dp2[0]=0; 31 | for(int i=1;i<=m;i++) 32 | { 33 | for(int j=x;j<=y;j++) 34 | { 35 | if(i-j>=0) 36 | { 37 | cnt1=black[i]-black[i-j]; 38 | cnt2=white[i]-white[i-j]; 39 | dp1[i]=min(dp1[i],dp2[i-j]+cnt1); 40 | dp2[i]=min(dp2[i],dp1[i-j]+cnt2); 41 | } 42 | } 43 | } 44 | int ans=min(dp1[m],dp2[m]); 45 | return ans; 46 | } -------------------------------------------------------------------------------- /Coding Exercise Solutions/bestTimeToBuyAndSellStock.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int maxProfit(int k, vector prices) { 5 | int n=prices.size(); 6 | if(n==0) 7 | { 8 | return 0; 9 | } 10 | int dp[k+1][n+1]; 11 | memset(dp,0,sizeof(dp)); 12 | for(int i=1;i<=k;i++) 13 | { 14 | for(int j=1;j 2 | using namespace std; 3 | 4 | vector>v; 5 | vectorpath; 6 | #define inf 1e9 7 | 8 | int dijkstra(int n){ 9 | priority_queue, vector>, greater>>q; 10 | vectordis(n+1,inf); 11 | q.push({0,1}); 12 | dis[1]=0; 13 | while(q.size()) 14 | { 15 | pairp=q.top(); 16 | q.pop(); 17 | int x=p.second,val=p.first; 18 | if(x==n){ 19 | break; 20 | } 21 | for(auto itr:v[x]){ 22 | if(dis[itr]>val+1){ 23 | dis[itr]=val+1; 24 | q.push({dis[itr],itr}); 25 | path[itr]=x; 26 | } 27 | } 28 | } 29 | return dis[n]==inf?-1:dis[n]; 30 | } 31 | 32 | vector solve(int n,int m, vector> edge){ 33 | v=vector>(n+1); 34 | path=vector(n+1); 35 | for(int i=0;ians(m,-1); 40 | vectortour; 41 | int value=dijkstra(n); 42 | set>pathedge; 43 | if(value!=-1){ 44 | int x=n; 45 | while(x!=1){ 46 | tour.push_back(x); 47 | x=path[x]; 48 | } 49 | tour.push_back(1); 50 | reverse(tour.begin(),tour.end()); 51 | for(int i=1;i 2 | using namespace std; 3 | #define maxn 100001 4 | 5 | long long solve(int n, vector a) 6 | { 7 | vectordp(maxn),cnt(maxn); 8 | for(int i=0;i 2 | using namespace std; 3 | #define maxn 100001 4 | #define pb push_back 5 | vectorpar(maxn),sz(maxn,1); 6 | int find(int x) 7 | { 8 | if(x!=par[x]) 9 | { 10 | return par[x]=find(par[x]); 11 | } 12 | return x; 13 | } 14 | void Union(int x,int y) 15 | { 16 | int val=find(x),val1=find(y); 17 | if(val==val1) 18 | { 19 | return; 20 | } 21 | if(sz[val]>sz[val1]) 22 | { 23 | swap(val,val1); 24 | } 25 | par[val]=val1; 26 | sz[val1]+=sz[val]; 27 | } 28 | long long buildRoads(vector>Coordinates){ 29 | int n=Coordinates.size(); 30 | long long p,q; 31 | iota(par.begin(),par.end(),0); 32 | sz.assign(maxn,1); 33 | vector>x,y; 34 | for(long long i=0;i>edges; 43 | for(int i=1;i 2 | using namespace std; 3 | 4 | int findCheapestPrice(int n, vector> flights, int src, int dst, int k) { 5 | vector dist( n, 1000000000 ); 6 | dist[src] = 0; 7 | 8 | for( int i=0; i <= k; i++ ) { 9 | vector tmp( dist ); 10 | for( auto flight : flights ) { 11 | if( dist[ flight[0] ] != INT_MAX ) { 12 | tmp[ flight[1] ] = min( tmp[flight[1]], dist[ flight[0] ] + flight[2] ); 13 | } 14 | } 15 | dist = tmp; 16 | } 17 | return dist[dst] == INT_MAX ? -1 : dist[dst]; 18 | } -------------------------------------------------------------------------------- /Coding Exercise Solutions/cherryPickup.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int dy[4]={-1,0,1}; 5 | int solve(int r,int c1,int c2,int n,int m,vector>&a,vector>>&dp) 6 | { 7 | if(r<0 || c1<0 || c2<0 || r>=n || c1>=m || c2>=m) 8 | { 9 | return 0; 10 | } 11 | if(dp[r][c1][c2]!=-1) 12 | { 13 | return dp[r][c1][c2]; 14 | } 15 | int ans=0,val=a[r][c1]; 16 | if(c1!=c2) 17 | { 18 | val+=a[r][c2]; 19 | } 20 | for(int i=0;i<3;i++) 21 | { 22 | for(int j=0;j<3;j++) 23 | { 24 | ans=max(ans,val+solve(r+1, c1+dy[i], c2+dy[j],n,m ,a,dp)); 25 | } 26 | } 27 | return dp[r][c1][c2]=ans; 28 | } 29 | int cherryPickup(vector> grid) { 30 | int n=grid.size(),m=grid[0].size(); 31 | vector>>dp(n+1,vector>(m+1,vector(m+1,-1))); 32 | return solve(0,0,m-1,n,m,grid,dp); 33 | } -------------------------------------------------------------------------------- /Coding Exercise Solutions/classyNumbers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define maxn 19 4 | long long dp[2][maxn][maxn]; 5 | vectorv; 6 | 7 | long long cal(int pos,int fl,int cnt) 8 | { 9 | if (pos==v.size()) 10 | { 11 | return (cnt<=3); 12 | } 13 | if(dp[fl][pos][cnt]!=-1) 14 | { 15 | return dp[fl][pos][cnt]; 16 | } 17 | int limit=9; 18 | if(!fl) 19 | { 20 | limit=v[pos]; 21 | } 22 | long long ans=0; 23 | for(int i=0;i<=limit;i++) 24 | { 25 | int flag=(i 2 | using namespace std; 3 | 4 | int solve(vector> matrix) { 5 | int n=matrix.size(); 6 | if(n==0) 7 | { 8 | return 0; 9 | } 10 | int m=matrix[0].size(); 11 | vector>dp(n,vector(m)); 12 | for(int i=0;i=0?dp[i-1][j]:0; 22 | int y=j-1>=0?dp[i][j-1]:0; 23 | dp[i][j]=matrix[i][j]+max(x,y); 24 | } 25 | } 26 | } 27 | return dp[n-1][m-1]; 28 | } -------------------------------------------------------------------------------- /Coding Exercise Solutions/comeBackQuickly.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define inf 1e9 4 | #define fi first 5 | #define se second 6 | 7 | vectorsolve(int n, int m, vector>road){ 8 | int x,y,val; 9 | vector>>v(n+1); 10 | for(int i=0;ians(n,inf); 16 | for(int i=1;i<=n;i++) 17 | { 18 | vectordis(n+1,inf); 19 | priority_queue, vector>, greater>>q; 20 | q.push({0,i}); 21 | int flag=1; 22 | val=inf; 23 | while(q.size()) 24 | { 25 | pairp=q.top(); 26 | q.pop(); 27 | for(auto itr:v[p.se]) 28 | { 29 | x=itr.fi,y=itr.se; 30 | if(x==i) 31 | { 32 | val=min(val,y+p.fi); 33 | } 34 | if(dis[x]>p.fi+y) 35 | { 36 | dis[x]=p.fi+y; 37 | q.push({dis[x],x}); 38 | } 39 | } 40 | } 41 | ans[i-1]=val==inf?-1:val; 42 | } 43 | return ans; 44 | } -------------------------------------------------------------------------------- /Coding Exercise Solutions/connectedCities.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool BFS(int n, unordered_map> g) { 5 | queue q; 6 | q.push(0); 7 | unordered_map vis; 8 | vis[0] = true; 9 | while (!q.empty()) { 10 | int P = q.front(); 11 | q.pop(); 12 | for (auto &c : g[P]) { 13 | if (!vis[c]) { 14 | vis[c] = true; 15 | q.push(c); 16 | } 17 | } 18 | } 19 | return vis.size() == n; 20 | } 21 | bool solve(int n, vector> roads) { 22 | unordered_map> g; 23 | for (auto &c : roads){ 24 | g[c[0]].insert(c[1]); 25 | } 26 | 27 | bool allvis = BFS(n, g); 28 | if (!allvis){ 29 | return allvis; 30 | } 31 | unordered_map> revg; 32 | for (auto &[k, v] : g) { 33 | for (auto &c : v) revg[c].insert(k); 34 | } 35 | allvis = BFS(n, revg); 36 | return allvis; 37 | } -------------------------------------------------------------------------------- /Coding Exercise Solutions/countAllPossibleRoutes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define mod 1000000007 4 | 5 | long long dp[101][201],n; 6 | long long solve(int pos,int finish,int fuel,vector&a) 7 | { 8 | if(fuel<0) 9 | { 10 | return 0; 11 | } 12 | if(dp[pos][fuel]!=-1) 13 | { 14 | return dp[pos][fuel]; 15 | } 16 | long long ans=(pos==finish); 17 | for(int i=0;i locations, int start, int finish, int fuel) { 27 | memset(dp,-1,sizeof(dp)); 28 | n=locations.size(); 29 | return solve(start,finish,fuel,locations); 30 | } -------------------------------------------------------------------------------- /Coding Exercise Solutions/countOfSmallerNumbers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void update(int x,int n,vector&bit) 5 | { 6 | while(x&bit) 13 | { 14 | int ans=0; 15 | while(x>0) 16 | { 17 | ans+=bit[x]; 18 | x-=x&-x; 19 | } 20 | return ans; 21 | } 22 | vector countSmaller(vector nums) { 23 | int n=nums.size(); 24 | vectorbit(n+1); 25 | vectortemp=nums; 26 | sort(temp.begin(),temp.end()); 27 | for(int i=0;ians(n); 33 | for(int i=n-1;i>=0;i--) 34 | { 35 | int val=get(nums[i]-1,bit); 36 | ans[i]=val; 37 | update(nums[i],n+1,bit); 38 | } 39 | return ans; 40 | } -------------------------------------------------------------------------------- /Coding Exercise Solutions/cut'emAll.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int ans; 5 | vectorsub; 6 | vector> g; 7 | 8 | void dfs(int k, int par) 9 | { 10 | sub[k]=1; 11 | for(auto it:g[k]) 12 | { 13 | if(it==par) 14 | continue; 15 | dfs(it, k); 16 | sub[k]+=sub[it]; 17 | } 18 | if(sub[k]%2==0&&k!=1) 19 | { 20 | ans++; 21 | } 22 | } 23 | int solve(int n, vector> edges) 24 | { 25 | ans=0; 26 | g=vector>(n+1,vector()); 27 | sub=vector(n+1); 28 | for(int i=0;i 2 | using namespace std; 3 | 4 | int networkDelayTime(vector> times, int N, int K) { 5 | // build graph 6 | vector > > graph(N+1); 7 | for (auto edge : times) { 8 | int fr_node = edge[0]; 9 | int to_node = edge[1]; 10 | int cost = edge[2]; 11 | graph[fr_node].push_back(make_pair(cost, to_node)); 12 | } 13 | vector dist(N+1, INT_MAX); 14 | priority_queue, vector>, greater>> pq; 15 | dist[K] = 0; 16 | pq.push(make_pair(0, K)); 17 | while (!pq.empty()) { 18 | pair x = pq.top(); 19 | pq.pop(); 20 | for (auto neighbor : graph[x.second]) { 21 | int ar = dist[x.second] + neighbor.first; 22 | if (ar < dist[neighbor.second]) { 23 | dist[neighbor.second] = ar; 24 | pq.push(make_pair(ar, neighbor.second)); 25 | } 26 | } 27 | } 28 | int max_time = INT_MIN; 29 | for (int i = 1; i < dist.size(); ++i) { 30 | if (max_time < dist[i]) { 31 | max_time = dist[i]; 32 | } 33 | } 34 | 35 | return max_time == INT_MAX? -1 : max_time; 36 | } -------------------------------------------------------------------------------- /Coding Exercise Solutions/dijkstra.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define fi first 4 | #define se second 5 | #define pb push_back 6 | const int inf = 1e9; 7 | class Dijkstra 8 | { 9 | public: 10 | vector dis,par; 11 | vector>> adj; 12 | Dijkstra(int n,vector>> A) 13 | { 14 | dis.resize(n+1); 15 | par.resize(n+1,0); 16 | for(int i=1;i<=n;++i) 17 | dis[i]=inf; 18 | adj = A; 19 | } 20 | 21 | void find_dis(int root) 22 | { 23 | set> s; 24 | s.insert({0,root}); 25 | dis[root]=0; 26 | par[root]=root; 27 | while(!s.empty()) 28 | { 29 | pair p=*s.begin(); 30 | s.erase(p); 31 | for(auto i:adj[p.se]) 32 | { 33 | if(p.fi+i.fi find_path(int root) 45 | { 46 | vector path; 47 | int p=root; 48 | while(p!=1) 49 | { 50 | path.pb(p); 51 | p=par[p]; 52 | } 53 | path.pb(1); 54 | reverse(path.begin(),path.end()); 55 | return path; 56 | } 57 | }; 58 | 59 | vector shortestPath(vector> roads,int n) 60 | { 61 | vector>> adj(n+1); 62 | for(auto i : roads) 63 | { 64 | adj[i[0]].pb({i[2],i[1]}); 65 | adj[i[1]].pb({i[2],i[0]}); 66 | } 67 | 68 | Dijkstra d(n,adj); 69 | d.find_dis(1); 70 | 71 | 72 | return d.find_path(n);; 73 | } -------------------------------------------------------------------------------- /Coding Exercise Solutions/disjointSetUnion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define maxn 100001 5 | int find(int x,vector&visit) 6 | { 7 | while(x!=visit[x]) 8 | { 9 | return visit[x]=find(visit[x],visit); 10 | } 11 | return x; 12 | } 13 | void Union(int x,int y,vector&visit,vector&sz) 14 | { 15 | int val=find(x,visit),val1=find(y,visit); 16 | if(val==val1) 17 | { 18 | return; 19 | } 20 | if(sz[val]>sz[val1]) 21 | { 22 | swap(val,val1); 23 | } 24 | visit[val]=val1; 25 | sz[val1]+=sz[val]; 26 | } 27 | vector DSU(vector>query) 28 | { 29 | int n=query.size(); 30 | vectorvisit(maxn),sz(maxn,1); 31 | iota(visit.begin(),visit.end(),0); 32 | vectorans; 33 | for(int i=0;i 2 | using namespace std; 3 | 4 | vector>v,up; 5 | vectortin,tout,depth; 6 | int level,timer; 7 | void dfs(int x,int p,int d) 8 | { 9 | tin[x]=++timer; 10 | up[x][0]=p; 11 | depth[x]=d; 12 | for(int i=1;i<=level;i++) 13 | { 14 | up[x][i]=up[up[x][i-1]][i-1]; 15 | } 16 | for(auto itr:v[x]) 17 | { 18 | if(itr!=p) 19 | { 20 | dfs(itr,x,d+1); 21 | } 22 | } 23 | tout[x]=++timer; 24 | } 25 | bool checker(int u,int v) 26 | { 27 | return tin[u]<=tin[v] && tout[u]>=tout[v]; 28 | } 29 | int lca(int u,int v) 30 | { 31 | if(checker(u,v)) 32 | { 33 | return u; 34 | } 35 | if(checker(v,u)) 36 | { 37 | return v; 38 | } 39 | for(int i=level;i>=0;i--) 40 | { 41 | if(!checker(up[u][i],v)) 42 | { 43 | u=up[u][i]; 44 | } 45 | } 46 | return up[u][0]; 47 | } 48 | vector distanceQuery(int n, vector>edges, vector>queries) 49 | { 50 | int x,y,val; 51 | level=ceil(log2(n)); 52 | tin.resize(n+1); 53 | tout.resize(n+1,1e9); 54 | depth.resize(n+1); 55 | v=vector>(n+1,vector()); 56 | up.assign(n+1,vector(level+1)); 57 | for(int i=0;ians; 65 | for(int i=0;i 2 | using namespace std; 3 | 4 | bool divisorGame(int n) { 5 | return n%2==0; 6 | } -------------------------------------------------------------------------------- /Coding Exercise Solutions/erectTheFence.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool comp(vector &v1, vector &v2){ 5 | if(v1[0]==v2[0]){ 6 | return v1[1]& a, vector& b, vector& c){ 11 | return (a[0]*(c[1]-b[1]) + b[0]*(a[1]-c[1]) + c[0]*(b[1]-a[1]))>0; 12 | } 13 | 14 | bool ccw(vector& a, vector& b, vector& c){ 15 | return (a[0]*(c[1]-b[1]) + b[0]*(a[1]-c[1]) + c[0]*(b[1]-a[1]))<0; 16 | } 17 | 18 | vector> outerTrees(vector> trees) { 19 | sort(trees.begin(),trees.end(),comp); 20 | int n=trees.size(); 21 | 22 | if(n<3){ 23 | return trees; 24 | } 25 | vector>convex_hull,up,down; 26 | 27 | vectorfirst=trees[0],last=trees.back(); 28 | 29 | up.push_back(first); 30 | down.push_back(first); 31 | 32 | for(int i=1;i=2 && ccw(up[up.size()-2],up.back(),trees[i])){ 35 | up.pop_back(); 36 | } 37 | up.push_back(trees[i]); 38 | } 39 | if(i==n-1 || !cw(first,trees[i],last)){ 40 | while(down.size()>=2 && cw(down[down.size()-2],down.back(),trees[i])){ 41 | down.pop_back(); 42 | } 43 | down.push_back(trees[i]); 44 | } 45 | } 46 | for(auto itr:up){ 47 | convex_hull.push_back(itr); 48 | } 49 | for(auto itr:down){ 50 | convex_hull.push_back(itr); 51 | } 52 | sort(convex_hull.begin(),convex_hull.end()); 53 | 54 | convex_hull.resize(unique(convex_hull.begin(),convex_hull.end())-convex_hull.begin()); 55 | 56 | return convex_hull; 57 | } -------------------------------------------------------------------------------- /Coding Exercise Solutions/findPattern.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define maxn 100001 4 | 5 | long long p=29, mod=1e9+7,power[maxn]; 6 | int findPattern(string word, string pattern){ 7 | power[0]=1; 8 | for(int i=1;ihash(n+1); 14 | for(int i=0;i 2 | using namespace std; 3 | 4 | #define inf 1e9 5 | 6 | int minSumOfLengths(vector arr, int target) { 7 | int n=arr.size(); 8 | vectordp(n,inf); 9 | int ans=inf,len=inf; 10 | int l=0,r=0,sum=0; 11 | while(rtarget){ 15 | sum-=arr[l]; 16 | l++; 17 | } 18 | if(sum==target){ 19 | if(l!=0 && dp[l-1]!=inf){ 20 | ans=min(ans,dp[l-1]+r-l+1); 21 | } 22 | len=min(len, r-l+1); 23 | } 24 | dp[r]=len; 25 | r++; 26 | } 27 | return ans==inf?-1:ans; 28 | } -------------------------------------------------------------------------------- /Coding Exercise Solutions/gameOfBullets.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | string solve(int n, vector A) 5 | { 6 | int val=0; 7 | for(int i=0;i 2 | using namespace std; 3 | #define mod 1000000007 4 | 5 | int gameRoutes(int n, vector> teleporters) 6 | { 7 | long long x,y; 8 | vector>v(n+1); 9 | vectorind(n+1); 10 | vectordp(n+1); 11 | for(int i=0;iq; 18 | vectortopo; 19 | for(int i=1;i<=n;i++) 20 | { 21 | if(ind[i]==0) 22 | { 23 | q.push(i); 24 | } 25 | } 26 | while(q.size()) 27 | { 28 | x=q.front(); 29 | q.pop(); 30 | topo.push_back(x); 31 | for(auto itr:v[x]) 32 | { 33 | ind[itr]--; 34 | if(ind[itr]==0) 35 | { 36 | q.push(itr); 37 | } 38 | } 39 | } 40 | dp[1]=1; 41 | for(auto itr:topo) 42 | { 43 | for(auto it:v[itr]) 44 | { 45 | dp[it]=(dp[it]+dp[itr])%mod; 46 | } 47 | } 48 | return dp[n]; 49 | } -------------------------------------------------------------------------------- /Coding Exercise Solutions/gcdSortOfAnArray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define maxn 100001 4 | 5 | set factors(int x,vector&spf){ 6 | setret; 7 | while(x!=1) 8 | { 9 | ret.insert(spf[x]); 10 | x/=spf[x]; 11 | } 12 | return ret; 13 | } 14 | int find(int x,vector&par){ 15 | if(x!=par[x]) 16 | { 17 | return par[x]=find(par[x],par); 18 | } 19 | return x; 20 | } 21 | void Unions(int x,int y,vector&par, vector&sz){ 22 | int val=find(x,par),val1=find(y,par); 23 | if(val==val1) 24 | { 25 | return; 26 | } 27 | if(sz[val]>sz[val1]) 28 | { 29 | swap(val,val1); 30 | } 31 | par[val]=val1; 32 | sz[val1]+=sz[val]; 33 | } 34 | bool gcdSort(vector nums) { 35 | vectorspf(maxn),par(maxn),sz(maxn); 36 | int n=nums.size(); 37 | for(int i=0;itemp=nums; 52 | sort(temp.begin(),temp.end()); 53 | for(int i=0;is=factors(nums[i],spf); 56 | for(auto itr:s) 57 | { 58 | Unions(nums[i],itr,par,sz); 59 | } 60 | } 61 | for(int i=0;i 2 | using namespace std; 3 | 4 | int trie[500001][26],cnt[500001],node; 5 | void insert(string &str) 6 | { 7 | int val=0,n=str.size(); 8 | for(int i=0;i words ){ 36 | int n=words.size(); 37 | node=0; 38 | memset(trie,0,sizeof(trie)); 39 | memset(cnt,0,sizeof(cnt)); 40 | for(int i=0;i 2 | using namespace std; 3 | 4 | int block,total; 5 | bool comp(const vector&v1, const vector&v2){ 6 | int x=(v1[0]+1)/block,y=(v2[0]+1)/block; 7 | if(x==y){ 8 | return v1[1]&cnt){ 14 | if(x>cnt.size()){ 15 | return; 16 | } 17 | if(cnt[x]==x){ 18 | total--; 19 | } 20 | cnt[x]+=val; 21 | if(cnt[x]==x){ 22 | total++; 23 | } 24 | } 25 | 26 | vector solve(int n,int m,vector a, vector> query){ 27 | block=n/(int)sqrt(n); 28 | total=0; 29 | 30 | vector>vec(m); 31 | for(int i=0;ians(m),cnt(n+1); 37 | int curl=0,curr=0; 38 | 39 | for(int i=0;il){ 46 | add(a[curl-1],1,cnt); 47 | curl--; 48 | } 49 | while(curr<=r){ 50 | add(a[curr],1,cnt); 51 | curr++; 52 | } 53 | while(curr>r+1){ 54 | add(a[curr-1],-1,cnt); 55 | curr--; 56 | } 57 | ans[vec[i][2]]=total; 58 | } 59 | return ans; 60 | } 61 | -------------------------------------------------------------------------------- /Coding Exercise Solutions/lowestCommonAncestor.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | vector>v,up; 5 | vectortin,tout; 6 | int level,timer; 7 | void dfs(int x,int p) 8 | { 9 | tin[x]=++timer; 10 | up[x][0]=p; 11 | for(int i=1;i<=level;i++) 12 | { 13 | up[x][i]=up[up[x][i-1]][i-1]; 14 | } 15 | for(auto itr:v[x]) 16 | { 17 | if(itr!=p) 18 | { 19 | dfs(itr,x); 20 | } 21 | } 22 | tout[x]=++timer; 23 | } 24 | bool is_ancestor(int u, int v) 25 | { 26 | return tin[u]<=tin[v] && tout[u]>=tout[v]; 27 | } 28 | int lca(int u,int v) 29 | { 30 | if(is_ancestor(u,v)) 31 | { 32 | return u; 33 | } 34 | if(is_ancestor(v,u)) 35 | { 36 | return v; 37 | } 38 | for(int i=level;i>=0;i--) 39 | { 40 | if(!is_ancestor(up[u][i],v)) 41 | { 42 | u=up[u][i]; 43 | } 44 | } 45 | return up[u][0]; 46 | } 47 | 48 | vector LCA(int n, vector>edges, vector>queries) 49 | { 50 | tin.resize(n+1); 51 | tout.resize(n+1,1e9); 52 | level=ceil(log2(n)); 53 | up.assign(n+1,vector(level+1)); 54 | v=vector>(n+1,vector()); 55 | int x,y,val; 56 | vectorans; 57 | for(int i=0;i 2 | using namespace std; 3 | 4 | string solve(int n,int a,int b, vector A) 5 | { 6 | int cnt1=0,cnt2=0,cnt3=0; 7 | for(int i=0;icnt3) 27 | { 28 | return "Bob"; 29 | } 30 | else{ 31 | return "Alice"; 32 | } 33 | } -------------------------------------------------------------------------------- /Coding Exercise Solutions/makeItConnected.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define inf 1e18 4 | vectorpar,sz; 5 | 6 | int find(int x){ 7 | if(x!=par[x]){ 8 | return par[x]=find(par[x]); 9 | } 10 | return x; 11 | } 12 | void Unions(int x,int y){ 13 | int val=find(x),val1=find(y); 14 | if(val==val1){ 15 | return; 16 | } 17 | if(sz[val]>sz[val1]){ 18 | swap(val,val1); 19 | } 20 | par[val]=val1; 21 | sz[val1]+=sz[val]; 22 | } 23 | 24 | long long solve(int n,int m,vector a, vector> special){ 25 | par=sz=vector(n+1,1); 26 | iota(par.begin(),par.end(),0); 27 | vector>v; 28 | long long mini=inf,x; 29 | for(int i=0;i 2 | using namespace std; 3 | 4 | int find(int x,vector&visit) 5 | { 6 | if(x!=visit[x]) 7 | { 8 | return visit[x]=find(visit[x],visit); 9 | } 10 | return x; 11 | } 12 | void Union(int x,int y,vector&visit,vector&sz) 13 | { 14 | int val=find(x,visit),val1=find(y,visit); 15 | if(val==val1) 16 | { 17 | return; 18 | } 19 | if(sz[val]>sz[val1]) 20 | { 21 | swap(val,val1); 22 | } 23 | visit[val]=val1; 24 | sz[val1]+=sz[val]; 25 | } 26 | int makeConnected(int n, vector> connections) { 27 | vectorvisit(n),sz(n); 28 | iota(visit.begin(),visit.end(),0); 29 | sz.assign(n,1); 30 | int cnt=0,extra=0; 31 | for(int i=0;i 2 | using namespace std; 3 | #define maxn 200001 4 | vectorpar,sz; 5 | 6 | int find(int x){ 7 | if(par[x]!=x){ 8 | return par[x]=find(par[x]); 9 | } 10 | return x; 11 | } 12 | void Unions(int x,int y){ 13 | int val=find(x),val1=find(y); 14 | if(val==val1){ 15 | return ; 16 | } 17 | if(sz[val]>sz[val1]){ 18 | swap(val,val1); 19 | } 20 | par[val]=val1; 21 | sz[val1]+=sz[val]; 22 | } 23 | 24 | int solve(int n,vector a){ 25 | par=sz=vector(maxn); 26 | iota(par.begin(),par.end(),0); 27 | sets; 28 | sz.assign(maxn,1); 29 | for(int i=0;i 2 | using namespace std; 3 | 4 | int maxCoins(vector piles) 5 | { 6 | sort(piles.begin(),piles.end()); 7 | int ans=0,n=piles.size(); 8 | for(int i=n/3;i 2 | using namespace std; 3 | 4 | bool comp(const vector&a,const vector&b) 5 | { 6 | return a[1]&end) 10 | { 11 | int l=0,r=pos,mid,ans=-1; 12 | while(l<=r) 13 | { 14 | mid=(l+r)/2; 15 | if(end[mid]> events, int k) { 27 | int n=events.size(); 28 | sort(events.begin(),events.end(),comp); 29 | vectorend(n); 30 | for(int i=0;i>dp(n+1,vector(k+1,0)); 35 | dp[0][0]=events[0][2]; 36 | for(int i=1;i 2 | using namespace std; 3 | #define ll long long 4 | #define inf 1e18 5 | 6 | int maxProduct(vector nums) { 7 | ll n=nums.size(); 8 | ll ans=nums[0],cur_max=nums[0],cur_min=nums[0]; 9 | for(int i=1;i=0) 12 | { 13 | cur_max=max(cur_max*(ll)nums[i],(ll)nums[i]); 14 | cur_min=min(cur_min*(ll)nums[i],(ll)nums[i]); 15 | } 16 | else{ 17 | ll temp_max=max(cur_min*(ll)nums[i],(ll)nums[i]); 18 | cur_min=min(cur_max*(ll)nums[i],(ll)nums[i]); 19 | cur_max=max(temp_max,(ll)nums[i]); 20 | } 21 | ans=max(ans,cur_max); 22 | } 23 | return ans; 24 | } -------------------------------------------------------------------------------- /Coding Exercise Solutions/maximumWhiteSubtree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | vector>adj; 5 | void dfs1(int nd , int pr, vector&dp1, vector&a) 6 | { 7 | dp1[nd] = a[nd-1] == 1 ? 1 : -1; 8 | 9 | for (auto ch : adj[nd]) 10 | { 11 | if (ch == pr) 12 | continue; 13 | 14 | dfs1(ch, nd,dp1,a ); 15 | dp1[nd] += max(0, dp1[ch]); 16 | } 17 | } 18 | 19 | void dfs2(int nd, int pr ,vector &dp2, vector&dp1) 20 | { 21 | dp2[nd] = dp1[nd]; 22 | 23 | if (pr) 24 | { 25 | int val = dp2[pr] - max(0, dp1[nd]); 26 | dp2[nd] += max(0, val); 27 | } 28 | 29 | for (auto ch : adj[nd]) 30 | { 31 | if (ch == pr) 32 | continue; 33 | 34 | dfs2(ch, nd,dp2,dp1); 35 | } 36 | } 37 | 38 | vector maxWhiteSubtree(int n, vector a, vector> edges) 39 | { 40 | adj=vector>(n+1,vector()); 41 | vectordp1(n+1),dp2(n+1); 42 | vectorans; 43 | int x,y; 44 | for (int i = 0; i < n - 1; ++i) 45 | { 46 | x=edges[i][0],y=edges[i][1]; 47 | adj[x].push_back(y); 48 | adj[y].push_back(x); 49 | } 50 | 51 | dfs1(1,0,dp1,a); 52 | dfs2(1,0,dp2,dp1); 53 | 54 | for(int i=1;i<=n;i++) 55 | { 56 | ans.push_back(dp2[i]); 57 | } 58 | return ans; 59 | 60 | } -------------------------------------------------------------------------------- /Coding Exercise Solutions/maximumXorQueries.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int trie[3000001][2],node; 5 | void insert(int x) 6 | { 7 | int val=0,flag,val1; 8 | for(int i=30;i>=0;i--) 9 | { 10 | val1=1<=0;i--) 27 | { 28 | val1=1< maximizeXor(vector nums, vector> queries) { 46 | node=0; 47 | memset(trie,0,sizeof(trie)); 48 | vector,int>>v; 49 | vectorans(queries.size(),-1); 50 | for(int i=0;i 2 | using namespace std; 3 | 4 | int messageRoute(int n, vector>edges){ 5 | vector>v(n+1); 6 | int x,y,val,val1; 7 | for(int i=0;idis(n+1,1e9); 14 | queueq; 15 | vectorvisit(n+1); 16 | q.push(1); 17 | dis[1]=1; 18 | visit[1]=1; 19 | while(!q.empty()) 20 | { 21 | x=q.front(); 22 | q.pop(); 23 | for(auto itr:v[x]) 24 | { 25 | if(!visit[itr]) 26 | { 27 | visit[itr]=1; 28 | q.push(itr); 29 | } 30 | if(dis[itr]>dis[x]+1) 31 | { 32 | dis[itr]=dis[x]+1; 33 | } 34 | } 35 | } 36 | if(dis[n]==(int)1e9) 37 | { 38 | return -1; 39 | } 40 | return dis[n]; 41 | } -------------------------------------------------------------------------------- /Coding Exercise Solutions/minCostToReachDestination.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define inf 1e9 4 | 5 | int minCost(int maxTime, vector> edges, vector passingFees) { 6 | int n=passingFees.size(); 7 | vector>>v(n); 8 | for(int i=0;i>dp(maxTime+1,vector(n+1,inf)); 15 | dp[0][0]=passingFees[0]; 16 | int ans=inf; 17 | for(int i=0;i<=maxTime;i++) 18 | { 19 | for(int j=0;j 2 | using namespace std; 3 | 4 | vectorvisit,sz; 5 | bool comp(const vector& arr1, const vector& arr2) 6 | { 7 | return arr1[2]&visit) 10 | { 11 | if(x!=visit[x]) 12 | { 13 | return visit[x]=find(visit[x],visit); 14 | } 15 | return x; 16 | } 17 | void Union(int x,int y,vector&visit, vector&sz) 18 | { 19 | int val=find(x,visit),val1=find(y,visit); 20 | if(val==val1) 21 | { 22 | return; 23 | } 24 | if(sz[val]>sz[val1]) 25 | { 26 | swap(val,val1); 27 | } 28 | visit[val]=val1; 29 | sz[val1]+=sz[val]; 30 | } 31 | int MST(int n, vector> edges) 32 | { 33 | int x,y,val; 34 | visit=sz=vector(n+1,1); 35 | iota(visit.begin(),visit.end(),0); 36 | sort(edges.begin(),edges.end(),comp); 37 | int ans=0; 38 | for(int i=0;i 2 | using namespace std; 3 | #define maxn 16385 4 | #define inf 1e9 5 | 6 | int n,dp[15][maxn]; 7 | int solve(int pos,int mask,vector&a,vector&b) 8 | { 9 | if(pos==n) 10 | { 11 | return mask==0?0:inf; 12 | } 13 | if(dp[pos][mask]!=-1) 14 | { 15 | return dp[pos][mask]; 16 | } 17 | int ans=inf; 18 | for(int i=0;i nums1, vector nums2) { 29 | n=nums1.size(); 30 | memset(dp,-1,sizeof(dp)); 31 | int val=1< 2 | using namespace std; 3 | 4 | string solve(int n,int m,vector a){ 5 | if(n>m) 6 | { 7 | return "Yes"; 8 | } 9 | vector>dp(n,vector(m+1,false)); 10 | dp[0][a[0]%m]=true; 11 | for(int i=1;i 2 | using namespace std; 3 | 4 | bool canWinNim(int n) { 5 | return (n%4!=0); 6 | } -------------------------------------------------------------------------------- /Coding Exercise Solutions/nimGameII.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | string solve(int n, vector heaps){ 5 | for(int i=0;i 2 | using namespace std; 3 | #define maxn 1024 4 | #define mod 1000000007 5 | 6 | long long dp[40][maxn]; 7 | vector>v; 8 | int solve(int hat,int mask) 9 | { 10 | if(hat>=40) 11 | { 12 | return mask==0; 13 | } 14 | if(dp[hat][mask]!=-1) 15 | { 16 | return dp[hat][mask]; 17 | } 18 | long long ans=0,x,val; 19 | ans=(ans+solve(hat+1,mask))%mod; 20 | for(int i=0;i> hats) { 32 | v.resize(40); 33 | int n=hats.size(); 34 | memset(dp,-1,sizeof(dp)); 35 | for(int i=0;i 2 | using namespace std; 3 | 4 | vectorv; 5 | int dp[2][10][1024],len; 6 | int solve(int pos,int flag,int mask) 7 | { 8 | if(pos==len) 9 | { 10 | return 1; 11 | } 12 | if(dp[flag][pos][mask]!=-1) 13 | { 14 | return dp[flag][pos][mask]; 15 | } 16 | int limit=9; 17 | if(!flag) 18 | { 19 | limit=v[pos]; 20 | } 21 | int ans=0; 22 | for(int i=(pos==0);i<=limit;i++) 23 | { 24 | int fl=0; 25 | if(i 2 | using namespace std; 3 | 4 | int minCut(string str) { 5 | int n; 6 | n=str.size(); 7 | vector>pal(n,vector(n)); 8 | for(int i=0;i=0 && y=0 && ydp(n); 29 | for(int i=0;i 2 | using namespace std; 3 | 4 | vector>v,up; 5 | vectortin,tout,pre; 6 | int level,timer; 7 | void dfs(int x,int p,int w,vector& a) 8 | { 9 | tin[x]=++timer; 10 | up[x][0]=p; 11 | pre[x]=w^pre[p]; 12 | for(int i=1;i<=level;i++) 13 | { 14 | up[x][i]=up[up[x][i-1]][i-1]; 15 | } 16 | for(auto itr:v[x]) 17 | { 18 | if(itr!=p) 19 | { 20 | dfs(itr,x,a[itr-1],a); 21 | } 22 | } 23 | tout[x]=++timer; 24 | } 25 | bool is_ancestor(int u, int v) 26 | { 27 | return tin[u]<=tin[v] && tout[u]>=tout[v]; 28 | } 29 | int lca(int u,int v) 30 | { 31 | if(is_ancestor(u,v)) 32 | { 33 | return u; 34 | } 35 | if(is_ancestor(v,u)) 36 | { 37 | return v; 38 | } 39 | for(int i=level;i>=0;i--) 40 | { 41 | if(!is_ancestor(up[u][i],v)) 42 | { 43 | u=up[u][i]; 44 | } 45 | } 46 | return up[u][0]; 47 | } 48 | vector pathXor(int n, vector a, vector> edges, vector> queries){ 49 | 50 | tin.assign(n+1,-1); 51 | tout.assign(n+1,1e9); 52 | v=vector>(n+1,vector()); 53 | pre.resize(n+1); 54 | level=ceil(log2(n)); 55 | up.assign(n+1,vector(level+1)); 56 | int x,y,val; 57 | for(int i=0;ians; 65 | for(int i=0;i 2 | using namespace std; 3 | #define maxn 1e9 4 | 5 | int numSquares(int n) { 6 | vectordp(n+1,maxn); 7 | dp[0]=0; 8 | dp[1]=1; 9 | for(int i=2;i<=n;i++) 10 | { 11 | int x=sqrt(i); 12 | for(int j=1;j<=x;j++) 13 | { 14 | int y=j*j; 15 | if(i-y>=0) 16 | { 17 | dp[i]=min(dp[i],dp[i-y]+1); 18 | } 19 | } 20 | } 21 | return dp[n]; 22 | } -------------------------------------------------------------------------------- /Coding Exercise Solutions/powerfulArray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define maxn 100001 4 | long long block,total; 5 | 6 | bool comp(const vector&v1, const vector &v2){ 7 | int x=(v1[0]+1)/block,y=(v2[0]+1)/block; 8 | if(x==y){ 9 | return v1[1]&cnt){ 15 | total-=(long long)cnt[x]*(long long)cnt[x]*(long long)x; 16 | cnt[x]+=val; 17 | total+=(long long)cnt[x]*(long long )cnt[x]*(long long)x; 18 | } 19 | 20 | vector solve(int n,int m, vector a, vector> query){ 21 | block=sqrt(n); 22 | total=0; 23 | vector>vec; 24 | for(int i=0;ians(m); 30 | vectorcnt(maxn); 31 | int curl=0,curr=0; 32 | 33 | for(int i=0;il){ 40 | add(a[curl-1],1,cnt); 41 | curl--; 42 | } 43 | while(curr<=r){ 44 | add(a[curr],1,cnt); 45 | curr++; 46 | } 47 | while(curr>r+1){ 48 | add(a[curr-1],-1,cnt); 49 | curr--; 50 | } 51 | ans[vec[i][2]]=total; 52 | } 53 | return ans; 54 | } 55 | 56 | -------------------------------------------------------------------------------- /Coding Exercise Solutions/rangeSetQuery.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define maxn 200001 4 | #define fi first 5 | #define se second 6 | 7 | int bit[maxn]; 8 | bool comp(pair,int>&p, pair,int>&p1) 9 | { 10 | if(p.fi.se==p1.fi.se) 11 | { 12 | return p.fi.fi0) 28 | { 29 | ans+=bit[index]; 30 | index-=index&-index; 31 | } 32 | return ans; 33 | } 34 | vector solve(int n, vector c, vector> queries){ 35 | memset(bit,0,sizeof(bit)); 36 | int q=queries.size(); 37 | pair,int>p[q]; 38 | int x,y; 39 | for(int i=0;ians(q); 47 | mapm; 48 | x=0; 49 | for(int i=0;i 2 | using namespace std; 3 | #define maxn 100001 4 | 5 | int bit[maxn]; 6 | void update(int index,int val) 7 | { 8 | while(index0) 18 | { 19 | ans+=bit[index]; 20 | index-=index&-index; 21 | } 22 | return ans; 23 | } 24 | vector solve(int n, vector nums, vector> queries){ 25 | memset(bit,0,sizeof(bit)); 26 | vectorans; 27 | for(int i=0;i 2 | using namespace std; 3 | 4 | vectorans; 5 | map>m; 6 | void dfs(string str) 7 | { 8 | while(m[str].size()>0) 9 | { 10 | auto it=m[str].begin(); 11 | string str1=*it; 12 | m[str].erase(it); 13 | dfs(str1); 14 | } 15 | ans.push_back(str); 16 | } 17 | vector findItinerary(vector> tickets) { 18 | int n=tickets.size(); 19 | ans.clear(); 20 | m.clear(); 21 | for(int i=0;i 2 | using namespace std; 3 | 4 | vector solve(int n,int m, vector> arr) 5 | { 6 | vector>v(n+1); 7 | vectorin(n+1); 8 | int x,y; 9 | for(int i=0;is; 17 | vectortopo; 18 | for(int i=1;i<=n;i++) 19 | { 20 | if(in[i]==0) 21 | { 22 | s.insert(i); 23 | } 24 | } 25 | while(s.size()) 26 | { 27 | x=*s.begin(); 28 | s.erase(x); 29 | topo.push_back(x); 30 | for(auto itr:v[x]) 31 | { 32 | in[itr]--; 33 | if(in[itr]==0) 34 | { 35 | s.insert(itr); 36 | } 37 | } 38 | } 39 | vectorans; 40 | if(topo.size()==n) 41 | { 42 | ans=topo; 43 | } 44 | return ans; 45 | } -------------------------------------------------------------------------------- /Coding Exercise Solutions/ringMst.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool comp(const vector&a, const vector&b){ 5 | return a[1]> operation){ 9 | sort(operation.begin(),operation.end(),comp); 10 | vectorcomponent(m+1); 11 | component[0]=n; 12 | int val=0; 13 | for(int i=0;i 2 | using namespace std; 3 | 4 | int solve(int n, int h,int l, int r, vector a){ 5 | vector>dp(n+1,vector(h,-1)); 6 | dp[0][0]=0; 7 | for(int i=0;i=val)); 16 | val=(x-1+j)%h; 17 | dp[i+1][val]=max(dp[i+1][val],dp[i][j]+(l<=val && r>=val)); 18 | } 19 | } 20 | } 21 | int ans=0; 22 | for(int i=0;i 2 | using namespace std; 3 | 4 | vectormarked,sz; 5 | int find(int x) 6 | { 7 | if(marked[x]!=x) 8 | { 9 | return marked[x]=find(marked[x]); 10 | } 11 | return x; 12 | } 13 | void Union(int x,int y) 14 | { 15 | int val=find(x),val1=find(y); 16 | if(val==val1) 17 | { 18 | return; 19 | } 20 | if(sz[val]>sz[val1]) 21 | { 22 | swap(val,val1); 23 | } 24 | marked[val]=val1; 25 | sz[val1]+=sz[val]; 26 | } 27 | int specialPath(int n, vector a, vector> edges,int start, int end) 28 | { 29 | int x,y,val,flag; 30 | int l=1,r=1e9,mid,ans=1e9; 31 | marked=vector(n+1); 32 | while(l<=r) 33 | { 34 | mid=(l+r)/2; 35 | iota(marked.begin(),marked.end(),0); 36 | sz.assign(n+1,1); 37 | for(int i=0;i 2 | using namespace std; 3 | 4 | string solve(int n, vector balls){ 5 | int xr=0; 6 | for(int i=0;i 2 | using namespace std; 3 | #define maxn 100001 4 | 5 | long long bit[maxn][12]; 6 | void update(long long index,long long x, long long val) 7 | { 8 | while(index0) 18 | { 19 | ans+=bit[index][x]; 20 | index-=index&-index; 21 | } 22 | return ans; 23 | } 24 | long long solve(int n, int k, vector seq){ 25 | memset(bit,0,sizeof(bit)); 26 | long long ans=0,val; 27 | for(int i=0;i 2 | using namespace std; 3 | 4 | void dfs(int x,int p, vector&sub, vector>&v) 5 | { 6 | sub[x]=1; 7 | for(auto itr:v[x]) 8 | { 9 | if(itr!=p) 10 | { 11 | dfs(itr,x,sub,v); 12 | sub[x]+=sub[itr]; 13 | } 14 | } 15 | } 16 | vector subtreeProblem (int n, vector> edges, vector queries) 17 | { 18 | vectorsub(n+1); 19 | vector>v(n+1); 20 | int x,y; 21 | for(int i=0;ians; 28 | dfs(1,-1,sub,v); 29 | for(int i=0;i 2 | using namespace std; 3 | 4 | int find(int x,vector&par){ 5 | if(x!=par[x]) 6 | { 7 | return par[x]=find(par[x],par); 8 | } 9 | return x; 10 | } 11 | void Unions(int x,int y,vector&par, vector& sz){ 12 | int val=find(x,par),val1=find(y,par); 13 | if(val==val1){ 14 | return; 15 | } 16 | if(sz[val]>sz[val1]){ 17 | swap(val,val1); 18 | } 19 | sz[val1]+=sz[val]; 20 | par[val]=val1; 21 | } 22 | 23 | bool comp(const vector&v1, const vector&v2){ 24 | return v1[2]> edges){ 28 | vectorpar(n+1),sz(n+1,1); 29 | iota(par.begin(),par.end(),0); 30 | sort(edges.begin(),edges.end(),comp); 31 | long long ans=0; 32 | for(int i=0;i 2 | using namespace std; 3 | #define inf 1e9 4 | 5 | vector travelByCar(int n,int l, vector>edges ,vector> queries) 6 | { 7 | vector>dis(n+1,vector(n+1,inf)),fuel(n+1,vector(n+1,inf)); 8 | int x,y,val; 9 | for(int i=0;idis[j][i]+dis[i][k]) 28 | { 29 | dis[j][k]=dis[j][i]+dis[i][k]; 30 | } 31 | } 32 | } 33 | } 34 | for(int i=0;ifuel[j][i]+fuel[i][k]) 55 | { 56 | fuel[j][k]=fuel[j][i]+fuel[i][k]; 57 | } 58 | } 59 | } 60 | } 61 | vectorans; 62 | for(int i=0;i 2 | using namespace std; 3 | 4 | vector>v; 5 | vectorsub,dp,ans; 6 | void dfs_1(int x,int p,vector a) 7 | { 8 | sub[x]=a[x-1]; 9 | for(auto itr:v[x]) 10 | { 11 | if(itr!=p) 12 | { 13 | dfs_1(itr,x,a); 14 | sub[x]+=sub[itr]; 15 | dp[x]+=dp[itr]+sub[itr]; 16 | } 17 | } 18 | } 19 | void dfs(int x,int p) 20 | { 21 | long long par; 22 | if(p!=0) 23 | { 24 | par=ans[p]-dp[x]-sub[x]; 25 | ans[x]=par+dp[x]+sub[1]-sub[x]; 26 | } 27 | for(auto itr:v[x]) 28 | { 29 | if(itr!=p) 30 | { 31 | dfs(itr,x); 32 | } 33 | } 34 | } 35 | long long treeMaxCost(int n, vector a ,vector> edges) 36 | { 37 | v=vector>(n+1,vector()); 38 | sub.assign(n+1,0); 39 | ans.assign(n+1,0); 40 | dp.assign(n+1,0); 41 | long long x,y,val; 42 | for(int i=0;i 2 | using namespace std; 3 | 4 | bool validBfs(int n, vector sequence , vector> edges){ 5 | int x,y,val,val1; 6 | vector>v(n+1); 7 | vectorvisit(n+1); 8 | for(int i=0;iq; 15 | q.push(1); 16 | visit[1]=1; 17 | y=1; 18 | int flag=0; 19 | while(q.size()) 20 | { 21 | x=q.front(); 22 | q.pop(); 23 | sets; 24 | for(int i=0;i 2 | using namespace std; 3 | 4 | int ladderLength(string beginWord, string endWord, vector wordList) { 5 | unordered_sets; 6 | for(auto itr:wordList) 7 | { 8 | s.insert(itr); 9 | } 10 | queueq; 11 | int ans=0; 12 | q.push(beginWord); 13 | while(!q.empty()) 14 | { 15 | ans++; 16 | vectorv; 17 | int n=q.size(); 18 | for(int i=0;i 2 | using namespace std; 3 | 4 | int node,trie[300001][26],cnt[300001]; 5 | mapvec; 6 | void insert(vector&words) 7 | { 8 | for(int i=0;i>&board,int pos,vector&ans) 25 | { 26 | if(cnt[pos]>0) 27 | { 28 | ans.push_back(vec[pos]); 29 | cnt[pos]=0; 30 | } 31 | if(x<0 || y<0 || x>=n || y>=m || board[x][y]=='#') 32 | { 33 | return; 34 | } 35 | int val=board[x][y]-'a'; 36 | if(!trie[pos][val]) 37 | { 38 | return; 39 | } 40 | char ch=board[x][y]; 41 | board[x][y]='#'; 42 | find(x+1,y,n,m,board,trie[pos][val],ans); 43 | find(x-1,y,n,m,board,trie[pos][val],ans); 44 | find(x,y+1,n,m,board,trie[pos][val],ans); 45 | find(x,y-1,n,m,board,trie[pos][val],ans); 46 | board[x][y]=ch; 47 | } 48 | vector findWords(vector> board, vector words) { 49 | int n=board.size(),m=board[0].size(); 50 | memset(trie,0,sizeof(trie)); 51 | memset(cnt,0,sizeof(cnt)); 52 | vec.clear(); 53 | node=0; 54 | vectorans; 55 | insert(words); 56 | for(int i=0;i 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Java Codes/out/production/javaCodes/AtcoderDP/Candies.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apaarkamal/Coding-Minutes-Competitive-Programming/a0d25194de16399fdea4f9a14b8f9de7b93a8f90/Java Codes/out/production/javaCodes/AtcoderDP/Candies.class -------------------------------------------------------------------------------- /Java Codes/out/production/javaCodes/AtcoderDP/Coins.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apaarkamal/Coding-Minutes-Competitive-Programming/a0d25194de16399fdea4f9a14b8f9de7b93a8f90/Java Codes/out/production/javaCodes/AtcoderDP/Coins.class -------------------------------------------------------------------------------- /Java Codes/out/production/javaCodes/AtcoderDP/Deque1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apaarkamal/Coding-Minutes-Competitive-Programming/a0d25194de16399fdea4f9a14b8f9de7b93a8f90/Java Codes/out/production/javaCodes/AtcoderDP/Deque1.class -------------------------------------------------------------------------------- /Java Codes/out/production/javaCodes/AtcoderDP/Deque2.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apaarkamal/Coding-Minutes-Competitive-Programming/a0d25194de16399fdea4f9a14b8f9de7b93a8f90/Java Codes/out/production/javaCodes/AtcoderDP/Deque2.class -------------------------------------------------------------------------------- /Java Codes/out/production/javaCodes/AtcoderDP/DigitSum.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apaarkamal/Coding-Minutes-Competitive-Programming/a0d25194de16399fdea4f9a14b8f9de7b93a8f90/Java Codes/out/production/javaCodes/AtcoderDP/DigitSum.class -------------------------------------------------------------------------------- /Java Codes/out/production/javaCodes/AtcoderDP/Flowers$segmentTree.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apaarkamal/Coding-Minutes-Competitive-Programming/a0d25194de16399fdea4f9a14b8f9de7b93a8f90/Java Codes/out/production/javaCodes/AtcoderDP/Flowers$segmentTree.class -------------------------------------------------------------------------------- /Java Codes/out/production/javaCodes/AtcoderDP/Flowers.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apaarkamal/Coding-Minutes-Competitive-Programming/a0d25194de16399fdea4f9a14b8f9de7b93a8f90/Java Codes/out/production/javaCodes/AtcoderDP/Flowers.class -------------------------------------------------------------------------------- /Java Codes/out/production/javaCodes/AtcoderDP/Frog1Recursive.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apaarkamal/Coding-Minutes-Competitive-Programming/a0d25194de16399fdea4f9a14b8f9de7b93a8f90/Java Codes/out/production/javaCodes/AtcoderDP/Frog1Recursive.class -------------------------------------------------------------------------------- /Java Codes/out/production/javaCodes/AtcoderDP/Frog1Tabulation.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apaarkamal/Coding-Minutes-Competitive-Programming/a0d25194de16399fdea4f9a14b8f9de7b93a8f90/Java Codes/out/production/javaCodes/AtcoderDP/Frog1Tabulation.class -------------------------------------------------------------------------------- /Java Codes/out/production/javaCodes/AtcoderDP/Frog2Tracing.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apaarkamal/Coding-Minutes-Competitive-Programming/a0d25194de16399fdea4f9a14b8f9de7b93a8f90/Java Codes/out/production/javaCodes/AtcoderDP/Frog2Tracing.class -------------------------------------------------------------------------------- /Java Codes/out/production/javaCodes/AtcoderDP/Grid1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apaarkamal/Coding-Minutes-Competitive-Programming/a0d25194de16399fdea4f9a14b8f9de7b93a8f90/Java Codes/out/production/javaCodes/AtcoderDP/Grid1.class -------------------------------------------------------------------------------- /Java Codes/out/production/javaCodes/AtcoderDP/IndependentSet.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apaarkamal/Coding-Minutes-Competitive-Programming/a0d25194de16399fdea4f9a14b8f9de7b93a8f90/Java Codes/out/production/javaCodes/AtcoderDP/IndependentSet.class -------------------------------------------------------------------------------- /Java Codes/out/production/javaCodes/DFSTreesAndBackEdges/DfsTreesAndBackEdges.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apaarkamal/Coding-Minutes-Competitive-Programming/a0d25194de16399fdea4f9a14b8f9de7b93a8f90/Java Codes/out/production/javaCodes/DFSTreesAndBackEdges/DfsTreesAndBackEdges.class -------------------------------------------------------------------------------- /Java Codes/out/production/javaCodes/DFSTreesAndBackEdges/DfsTreesAndBackEdgesDirectedGraph.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apaarkamal/Coding-Minutes-Competitive-Programming/a0d25194de16399fdea4f9a14b8f9de7b93a8f90/Java Codes/out/production/javaCodes/DFSTreesAndBackEdges/DfsTreesAndBackEdgesDirectedGraph.class -------------------------------------------------------------------------------- /Java Codes/out/production/javaCodes/DFSTreesAndBackEdges/PrintCycleInGraph.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apaarkamal/Coding-Minutes-Competitive-Programming/a0d25194de16399fdea4f9a14b8f9de7b93a8f90/Java Codes/out/production/javaCodes/DFSTreesAndBackEdges/PrintCycleInGraph.class -------------------------------------------------------------------------------- /Java Codes/out/production/javaCodes/DivideAndConquer/TernarySearch.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apaarkamal/Coding-Minutes-Competitive-Programming/a0d25194de16399fdea4f9a14b8f9de7b93a8f90/Java Codes/out/production/javaCodes/DivideAndConquer/TernarySearch.class -------------------------------------------------------------------------------- /Java Codes/out/production/javaCodes/FenwickTrees/FenwickTrees$fenwick.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apaarkamal/Coding-Minutes-Competitive-Programming/a0d25194de16399fdea4f9a14b8f9de7b93a8f90/Java Codes/out/production/javaCodes/FenwickTrees/FenwickTrees$fenwick.class -------------------------------------------------------------------------------- /Java Codes/out/production/javaCodes/FenwickTrees/FenwickTrees.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apaarkamal/Coding-Minutes-Competitive-Programming/a0d25194de16399fdea4f9a14b8f9de7b93a8f90/Java Codes/out/production/javaCodes/FenwickTrees/FenwickTrees.class -------------------------------------------------------------------------------- /Java Codes/out/production/javaCodes/FenwickTrees/InversionCountsUsingFenwick$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apaarkamal/Coding-Minutes-Competitive-Programming/a0d25194de16399fdea4f9a14b8f9de7b93a8f90/Java Codes/out/production/javaCodes/FenwickTrees/InversionCountsUsingFenwick$1.class -------------------------------------------------------------------------------- /Java Codes/out/production/javaCodes/FenwickTrees/InversionCountsUsingFenwick$Pair.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apaarkamal/Coding-Minutes-Competitive-Programming/a0d25194de16399fdea4f9a14b8f9de7b93a8f90/Java Codes/out/production/javaCodes/FenwickTrees/InversionCountsUsingFenwick$Pair.class -------------------------------------------------------------------------------- /Java Codes/out/production/javaCodes/FenwickTrees/InversionCountsUsingFenwick$fenwick.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apaarkamal/Coding-Minutes-Competitive-Programming/a0d25194de16399fdea4f9a14b8f9de7b93a8f90/Java Codes/out/production/javaCodes/FenwickTrees/InversionCountsUsingFenwick$fenwick.class -------------------------------------------------------------------------------- /Java Codes/out/production/javaCodes/FenwickTrees/InversionCountsUsingFenwick.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apaarkamal/Coding-Minutes-Competitive-Programming/a0d25194de16399fdea4f9a14b8f9de7b93a8f90/Java Codes/out/production/javaCodes/FenwickTrees/InversionCountsUsingFenwick.class -------------------------------------------------------------------------------- /Java Codes/out/production/javaCodes/InclusionExclusion/TotalDivisorInclusion.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apaarkamal/Coding-Minutes-Competitive-Programming/a0d25194de16399fdea4f9a14b8f9de7b93a8f90/Java Codes/out/production/javaCodes/InclusionExclusion/TotalDivisorInclusion.class -------------------------------------------------------------------------------- /Java Codes/out/production/javaCodes/LinearRecurrenceAndMatrixExponentiation/BinaryExp.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apaarkamal/Coding-Minutes-Competitive-Programming/a0d25194de16399fdea4f9a14b8f9de7b93a8f90/Java Codes/out/production/javaCodes/LinearRecurrenceAndMatrixExponentiation/BinaryExp.class -------------------------------------------------------------------------------- /Java Codes/out/production/javaCodes/LinearRecurrenceAndMatrixExponentiation/FastMultiplication.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apaarkamal/Coding-Minutes-Competitive-Programming/a0d25194de16399fdea4f9a14b8f9de7b93a8f90/Java Codes/out/production/javaCodes/LinearRecurrenceAndMatrixExponentiation/FastMultiplication.class -------------------------------------------------------------------------------- /Java Codes/out/production/javaCodes/LinearRecurrenceAndMatrixExponentiation/MatrixExp$Mat.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apaarkamal/Coding-Minutes-Competitive-Programming/a0d25194de16399fdea4f9a14b8f9de7b93a8f90/Java Codes/out/production/javaCodes/LinearRecurrenceAndMatrixExponentiation/MatrixExp$Mat.class -------------------------------------------------------------------------------- /Java Codes/out/production/javaCodes/LinearRecurrenceAndMatrixExponentiation/MatrixExp.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apaarkamal/Coding-Minutes-Competitive-Programming/a0d25194de16399fdea4f9a14b8f9de7b93a8f90/Java Codes/out/production/javaCodes/LinearRecurrenceAndMatrixExponentiation/MatrixExp.class -------------------------------------------------------------------------------- /Java Codes/out/production/javaCodes/LinearRecurrenceAndMatrixExponentiation/ModularExp.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apaarkamal/Coding-Minutes-Competitive-Programming/a0d25194de16399fdea4f9a14b8f9de7b93a8f90/Java Codes/out/production/javaCodes/LinearRecurrenceAndMatrixExponentiation/ModularExp.class -------------------------------------------------------------------------------- /Java Codes/out/production/javaCodes/RandomizeAndTime/RandomAndTime.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apaarkamal/Coding-Minutes-Competitive-Programming/a0d25194de16399fdea4f9a14b8f9de7b93a8f90/Java Codes/out/production/javaCodes/RandomizeAndTime/RandomAndTime.class -------------------------------------------------------------------------------- /Java Codes/out/production/javaCodes/SegmentTree/SegmentTree$segmentTree.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apaarkamal/Coding-Minutes-Competitive-Programming/a0d25194de16399fdea4f9a14b8f9de7b93a8f90/Java Codes/out/production/javaCodes/SegmentTree/SegmentTree$segmentTree.class -------------------------------------------------------------------------------- /Java Codes/out/production/javaCodes/SegmentTree/SegmentTree.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apaarkamal/Coding-Minutes-Competitive-Programming/a0d25194de16399fdea4f9a14b8f9de7b93a8f90/Java Codes/out/production/javaCodes/SegmentTree/SegmentTree.class -------------------------------------------------------------------------------- /Java Codes/out/production/javaCodes/SegmentTree/SegmentTreeLazyProp$segmentTree.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apaarkamal/Coding-Minutes-Competitive-Programming/a0d25194de16399fdea4f9a14b8f9de7b93a8f90/Java Codes/out/production/javaCodes/SegmentTree/SegmentTreeLazyProp$segmentTree.class -------------------------------------------------------------------------------- /Java Codes/out/production/javaCodes/SegmentTree/SegmentTreeLazyProp.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apaarkamal/Coding-Minutes-Competitive-Programming/a0d25194de16399fdea4f9a14b8f9de7b93a8f90/Java Codes/out/production/javaCodes/SegmentTree/SegmentTreeLazyProp.class -------------------------------------------------------------------------------- /Java Codes/out/production/javaCodes/StringMatching/BruteForce.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apaarkamal/Coding-Minutes-Competitive-Programming/a0d25194de16399fdea4f9a14b8f9de7b93a8f90/Java Codes/out/production/javaCodes/StringMatching/BruteForce.class -------------------------------------------------------------------------------- /Java Codes/out/production/javaCodes/StringMatching/PolyHash.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apaarkamal/Coding-Minutes-Competitive-Programming/a0d25194de16399fdea4f9a14b8f9de7b93a8f90/Java Codes/out/production/javaCodes/StringMatching/PolyHash.class -------------------------------------------------------------------------------- /Java Codes/out/production/javaCodes/StringMatching/RabinCarb.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apaarkamal/Coding-Minutes-Competitive-Programming/a0d25194de16399fdea4f9a14b8f9de7b93a8f90/Java Codes/out/production/javaCodes/StringMatching/RabinCarb.class -------------------------------------------------------------------------------- /Java Codes/out/production/javaCodes/StringMatching/Trie$Node.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apaarkamal/Coding-Minutes-Competitive-Programming/a0d25194de16399fdea4f9a14b8f9de7b93a8f90/Java Codes/out/production/javaCodes/StringMatching/Trie$Node.class -------------------------------------------------------------------------------- /Java Codes/out/production/javaCodes/StringMatching/Trie.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apaarkamal/Coding-Minutes-Competitive-Programming/a0d25194de16399fdea4f9a14b8f9de7b93a8f90/Java Codes/out/production/javaCodes/StringMatching/Trie.class -------------------------------------------------------------------------------- /Java Codes/out/production/javaCodes/StringMatching/TriePatternMatching$Node.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apaarkamal/Coding-Minutes-Competitive-Programming/a0d25194de16399fdea4f9a14b8f9de7b93a8f90/Java Codes/out/production/javaCodes/StringMatching/TriePatternMatching$Node.class -------------------------------------------------------------------------------- /Java Codes/out/production/javaCodes/StringMatching/TriePatternMatching.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apaarkamal/Coding-Minutes-Competitive-Programming/a0d25194de16399fdea4f9a14b8f9de7b93a8f90/Java Codes/out/production/javaCodes/StringMatching/TriePatternMatching.class -------------------------------------------------------------------------------- /Java Codes/out/production/javaCodes/TheoremsInNumberTheory/ModuloArithmetics.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apaarkamal/Coding-Minutes-Competitive-Programming/a0d25194de16399fdea4f9a14b8f9de7b93a8f90/Java Codes/out/production/javaCodes/TheoremsInNumberTheory/ModuloArithmetics.class -------------------------------------------------------------------------------- /Java Codes/out/production/javaCodes/TheoremsInNumberTheory/Totient.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apaarkamal/Coding-Minutes-Competitive-Programming/a0d25194de16399fdea4f9a14b8f9de7b93a8f90/Java Codes/out/production/javaCodes/TheoremsInNumberTheory/Totient.class -------------------------------------------------------------------------------- /Java Codes/out/production/javaCodes/Trees/AncestorPrinting.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apaarkamal/Coding-Minutes-Competitive-Programming/a0d25194de16399fdea4f9a14b8f9de7b93a8f90/Java Codes/out/production/javaCodes/Trees/AncestorPrinting.class -------------------------------------------------------------------------------- /Java Codes/out/production/javaCodes/main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apaarkamal/Coding-Minutes-Competitive-Programming/a0d25194de16399fdea4f9a14b8f9de7b93a8f90/Java Codes/out/production/javaCodes/main.class -------------------------------------------------------------------------------- /Java Codes/src/AtcoderDP/Candies.java: -------------------------------------------------------------------------------- 1 | package AtcoderDP; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Candies { 6 | 7 | 8 | public static void main(String[] args) { 9 | 10 | 11 | Scanner scn = new Scanner(System.in); 12 | int n = scn.nextInt(), k = scn.nextInt(), mod = 1000000007; 13 | 14 | int[] a = new int[n + 1]; 15 | 16 | for (int i = 1; i <= n; i++) { 17 | a[i] = scn.nextInt(); 18 | } 19 | 20 | int[][] dp = new int[n + 1][k + 1]; 21 | 22 | dp[0][0] = 1; 23 | 24 | // for (int i = 1; i <= n; i++) { 25 | // for (int j = 0; j <= k; j++) { 26 | // // dp[i][j]; 27 | // for (int kk = 0; kk <= min(a[i], j); kk++) { 28 | // dp[i][j] += dp[i - 1][j - kk]; 29 | // } 30 | // } 31 | // } 32 | 33 | for (int i = 1; i <= n; i++) { 34 | 35 | for (int j = 1; j <= k; j++) { 36 | dp[i - 1][j] += dp[i - 1][j - 1]; 37 | } 38 | 39 | for (int j = 0; j <= k; j++) { 40 | dp[i][j] = dp[i - 1][j]; 41 | if (j - a[i] > 0) dp[i][j] -= dp[i - 1][j - a[i] - 1]; 42 | dp[i][j] = ((dp[i][j] % mod) + mod) % mod; 43 | } 44 | } 45 | 46 | 47 | // for (int i = 0; i <= n; i++) { 48 | // for (int j = 0; j <= k; j++) { 49 | // cout << dp[i][j] << " "; 50 | // } cout << '\n'; 51 | // } 52 | 53 | System.out.println(dp[n][k]); 54 | 55 | 56 | 57 | 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /Java Codes/src/AtcoderDP/Coins.java: -------------------------------------------------------------------------------- 1 | package AtcoderDP; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Coins { 6 | 7 | 8 | final static int N = 3001; 9 | 10 | static double[] p = new double[N]; 11 | static double[][] memo = new double[N][N]; 12 | static int n; 13 | 14 | 15 | static double dp(int i, int h, int t) { 16 | if (i == n) return 1.00; 17 | double ans = memo[i][h]; 18 | if (ans != -1.00) return ans; 19 | ans = 0.00; 20 | if (h != 0) { 21 | ans += dp(i + 1, h - 1, t) * p[i]; 22 | } 23 | if (t != 0) { 24 | ans += dp(i + 1, h, t - 1) * (1.00 - p[i]); 25 | } 26 | return ans; 27 | } 28 | 29 | public static void main(String[] args) { 30 | 31 | Scanner scn = new Scanner(System.in); 32 | n = scn.nextInt(); 33 | for (int i = 0; i < n; i++) { 34 | p[i] = scn.nextInt(); 35 | } 36 | 37 | for (int i = 0; i < N; i++) { 38 | for (int j = 0; j < N; j++) { 39 | memo[i][j] = -1.00; 40 | } 41 | } 42 | 43 | double ans = 0; 44 | for (int i = (n / 2 + 1); i <= n; i++) { 45 | ans += dp(0, i, n - i); 46 | } 47 | System.out.println(ans); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /Java Codes/src/AtcoderDP/Deque1.java: -------------------------------------------------------------------------------- 1 | package AtcoderDP; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Deque1 { 6 | 7 | 8 | 9 | final static int N = 3001; 10 | 11 | static int n; 12 | static long[] a = new long[N]; 13 | static long sum = 0; 14 | static long[][][] memo = new long[N][N][2]; 15 | 16 | static long dp(int i, int j, int turn) { 17 | if (i > j) return 0; 18 | // if (i == j) { 19 | // if (turn == 0) return a[i]; 20 | // else return 0; 21 | // } 22 | long ans = memo[i][j][turn]; 23 | if (ans != 1e18) return ans; 24 | if (turn == 0) { 25 | ans = Math.max(a[i] + dp(i + 1, j, 1 - turn), 26 | a[j] + dp(i, j - 1, 1 - turn)); 27 | } 28 | else { 29 | ans = Math.min(dp(i + 1, j, 1 - turn), dp(i, j - 1, 1 - turn)); 30 | } 31 | return ans; 32 | } 33 | 34 | public static void main(String[] args) { 35 | 36 | 37 | 38 | Scanner scn = new Scanner(System.in); 39 | n = scn.nextInt(); 40 | for (int i = 0; i < n; i++) { 41 | a[i] = scn.nextInt(); 42 | sum += a[i]; 43 | } 44 | 45 | for (int i = 0; i < N; i++) { 46 | for (int j = 0; j < N; j++) { 47 | memo[i][j][0] = memo[i][j][1] = (long)1e18; 48 | } 49 | } 50 | 51 | long max_x = dp(0, n - 1, 0); 52 | 53 | long min_y = sum - max_x; 54 | 55 | System.out.println(max_x - min_y); 56 | 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /Java Codes/src/AtcoderDP/Deque2.java: -------------------------------------------------------------------------------- 1 | package AtcoderDP; 2 | 3 | import java.util.Scanner; 4 | 5 | import static java.lang.Math.max; 6 | 7 | public class Deque2 { 8 | 9 | 10 | static final int N = 3001; 11 | 12 | static int n; 13 | static long[] a = new long[N]; 14 | static long[][] memo = new long[N][N]; 15 | 16 | static long dp(int i, int j) { 17 | if (i == j) return a[i]; 18 | long ans = memo[i][j]; 19 | if (ans != -1e18) return ans; 20 | ans = max(a[i] - dp(i + 1, j), a[j] - dp(i, j - 1)); 21 | return ans; 22 | } 23 | 24 | public static void main(String[] args) { 25 | 26 | 27 | Scanner scn = new Scanner(System.in); 28 | n = scn.nextInt(); 29 | for (int i = 0; i < n; i++) { 30 | a[i] = scn.nextInt(); 31 | } 32 | 33 | for (int i = 0; i < N; i++) { 34 | for (int j = 0; j < N; j++) { 35 | memo[i][j] = (long) -1e18; 36 | } 37 | } 38 | 39 | System.out.println(dp(0, n - 1)); 40 | 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Java Codes/src/AtcoderDP/DigitSum.java: -------------------------------------------------------------------------------- 1 | package AtcoderDP; 2 | 3 | import java.util.Scanner; 4 | 5 | public class DigitSum { 6 | 7 | 8 | 9 | 10 | final static int N = 10000; 11 | 12 | static String k; 13 | static int d, apaar = (int)(1e9 + 7); 14 | static int[][][] memo = new int[N][2][100]; 15 | 16 | static int dp(int i, int last, int mod) { 17 | if (i == k.length()) { 18 | return 1; 19 | } 20 | 21 | int ans = memo[i][last][mod]; 22 | if (ans != -1) return ans; 23 | 24 | ans = 0; 25 | 26 | int till = last == 1 ? (k.charAt(i) - '0') : 9; 27 | 28 | for (int digits = 0; digits <= till; digits++) { 29 | ans += dp(i + 1, last == 1 && (digits == till) ? 1 : 0, (mod + digits) % d); 30 | ans %= apaar; 31 | } 32 | 33 | return ans; 34 | } 35 | 36 | public static void main(String[] args) { 37 | 38 | 39 | Scanner scn = new Scanner(System.in); 40 | k = scn.next(); d = scn.nextInt(); 41 | 42 | 43 | for(int i = 0 ; i < N;i++){ 44 | for(int j = 0; j < 2; j++){ 45 | for(int k = 0; k <100; k++){ 46 | memo[i][j][k] = -1; 47 | } 48 | } 49 | } 50 | 51 | 52 | System.out.println((dp(0, 1, 0) - 1 + apaar) % apaar); 53 | 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /Java Codes/src/AtcoderDP/Frog1Recursive.java: -------------------------------------------------------------------------------- 1 | package AtcoderDP; 2 | 3 | import javax.print.attribute.standard.MediaName; 4 | import java.util.Arrays; 5 | import java.util.Scanner; 6 | 7 | import static java.lang.Math.abs; 8 | import static java.util.Collections.min; 9 | 10 | public class Frog1Recursive { 11 | 12 | 13 | static final int N = (int)1e5; 14 | 15 | static int[] h = new int[N], memo = new int[N]; 16 | 17 | static int dp(int n) { 18 | if (n == 0) return 0; 19 | 20 | int ans = memo[n]; 21 | 22 | if (ans != -1) return ans; 23 | 24 | ans = dp(n - 1) + Math.abs(h[n - 1] - h[n]); 25 | 26 | if (n - 2 >= 0) { 27 | ans = Math.min(ans, dp(n - 2) + abs(h[n - 2] - h[n])); 28 | } 29 | return ans; 30 | } 31 | 32 | public static void main(String[] args) { 33 | 34 | 35 | Scanner scn = new Scanner(System.in); 36 | int n = scn.nextInt(); 37 | for (int i = 0; i < n; i++) { 38 | h[i] = scn.nextInt(); 39 | } 40 | 41 | Arrays.fill(memo, -1); 42 | System.out.println(dp(n - 1)); 43 | 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Java Codes/src/AtcoderDP/Frog1Tabulation.java: -------------------------------------------------------------------------------- 1 | package AtcoderDP; 2 | 3 | import java.util.Scanner; 4 | 5 | import static java.lang.Math.abs; 6 | import static java.lang.Math.min; 7 | 8 | public class Frog1Tabulation { 9 | 10 | public static void main(String[] args) { 11 | 12 | Scanner scn = new Scanner(System.in); 13 | int n = scn.nextInt(); 14 | int[] h = new int[n]; 15 | 16 | for (int i = 0; i < n; i++) { 17 | h[i] = scn.nextInt(); 18 | } 19 | 20 | int[] dp = new int[n]; 21 | 22 | dp[0] = 0; 23 | dp[1] = dp[0] + abs(h[1] - h[0]); 24 | 25 | for (int i = 2; i < n; i++) { 26 | dp[i] = min(dp[i - 1] + abs(h[i - 1] - h[i]), dp[i - 2] 27 | + abs(h[i - 2] - h[i])); 28 | } 29 | 30 | System.out.println(dp[n-1]); 31 | 32 | 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Java Codes/src/AtcoderDP/Frog2Tracing.java: -------------------------------------------------------------------------------- 1 | package AtcoderDP; 2 | 3 | import java.util.Scanner; 4 | 5 | import static java.lang.Math.abs; 6 | 7 | public class Frog2Tracing { 8 | 9 | 10 | static final int N = (int) 1e5; 11 | static int n, k; 12 | static int[] h = new int[N]; 13 | static int[] dp = new int[N]; 14 | 15 | static void go(int i) { 16 | System.out.println(i+1); 17 | for (int kk = 1; kk <= k && i - kk >= 0; kk++) { 18 | if (dp[i] == dp[i - kk] + abs(h[i] - h[i - kk])) { 19 | go(i - kk); 20 | return; 21 | } 22 | } 23 | } 24 | 25 | public static void main(String[] args) { 26 | 27 | Scanner scn = new Scanner(System.in); 28 | 29 | n = scn.nextInt(); 30 | k = scn.nextInt(); 31 | 32 | for (int i = 0; i < n; i++) { 33 | h[i] = scn.nextInt(); 34 | } 35 | 36 | 37 | dp[0] = 0; 38 | dp[1] = dp[0] + abs(h[1] - h[0]); 39 | 40 | for (int i = 2; i < n; i++) { 41 | dp[i] = Integer.MAX_VALUE; 42 | for (int kk = 1; kk <= k && i - kk >= 0; kk++) { 43 | dp[i] = Math.min(dp[i], dp[i - kk] + abs(h[i] - h[i - kk])); 44 | } 45 | } 46 | 47 | System.out.println(dp[n-1]); 48 | 49 | // for (int i = 0; i < n; i++) { 50 | // System.out.println(dp[i]); 51 | // } 52 | 53 | //trace back 54 | // go(n - 1); 55 | 56 | 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /Java Codes/src/AtcoderDP/Grid1.java: -------------------------------------------------------------------------------- 1 | package AtcoderDP; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Grid1 { 6 | 7 | public static void main(String[] args) { 8 | 9 | Scanner scn = new Scanner(System.in); 10 | int n = scn.nextInt(), m = scn.nextInt(), mod = (int) (1e9 + 7); 11 | String[] s = new String[n]; 12 | for (int i = 0; i < n; i++) { 13 | s[i] = scn.next(); 14 | } 15 | 16 | int[][] dp = new int[n][m]; 17 | 18 | for (int i = 0; i < n; i++) { 19 | for (int j = 0; j < m; j++) { 20 | if (i == 0 && j == 0) dp[i][j] = 1; 21 | else if (i == 0) dp[i][j] = dp[i][j - 1]; 22 | else if (j == 0) dp[i][j] = dp[i - 1][j]; 23 | else { 24 | dp[i][j] = dp[i][j - 1] + dp[i - 1][j]; 25 | dp[i][j] %= mod; 26 | } 27 | if (s[i].charAt(j) == '#') dp[i][j] = 0; 28 | } 29 | } 30 | 31 | System.out.println(dp[n - 1][m - 1]); 32 | 33 | 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Java Codes/src/AtcoderDP/IndependentSet.java: -------------------------------------------------------------------------------- 1 | package AtcoderDP; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | import java.util.List; 6 | import java.util.Scanner; 7 | 8 | public class IndependentSet { 9 | 10 | 11 | static final int N = (int) (1e5 + 1), mod = (int) (1e9 + 7); 12 | 13 | 14 | static List> gr = new ArrayList<>(); 15 | static { 16 | for(int i = 0 ;i < N; i++){ 17 | gr.add(new ArrayList<>()); 18 | } 19 | } 20 | static int[][] memo = new int[N][2]; 21 | 22 | static int dp(int cur, int paint, int par) { 23 | int ans = memo[cur][paint]; 24 | 25 | if (ans != -1) return ans; 26 | 27 | ans = 1; 28 | for (int child : gr.get(cur)) { 29 | if (child != par) { 30 | if (paint != 0) { 31 | ans *= dp(child, 0, cur) + dp(child, 1, cur); 32 | } else { 33 | ans *= dp(child, 1, cur); 34 | } 35 | ans %= mod; 36 | } 37 | } 38 | 39 | return ans; 40 | } 41 | 42 | public static void main(String[] args) { 43 | 44 | Scanner scanner = new Scanner(System.in); 45 | int n = scanner.nextInt(); 46 | 47 | for (int i = 0; i < n - 1; i++) { 48 | int x = scanner.nextInt(), y = scanner.nextInt(); 49 | gr.get(x).add(y); 50 | gr.get(y).add(x); 51 | } 52 | 53 | 54 | for(int i = 0; i < memo.length; i++){ 55 | Arrays.fill(memo[i], -1); 56 | } 57 | 58 | System.out.println((dp(1, 1, 0) + dp(1, 0, 0)) % mod); 59 | 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /Java Codes/src/AtcoderDP/Knapsack1.java: -------------------------------------------------------------------------------- 1 | package AtcoderDP; 2 | 3 | import java.util.Arrays; 4 | import java.util.Scanner; 5 | 6 | import static java.lang.Math.max; 7 | 8 | public class Knapsack1 { 9 | 10 | 11 | static final int N = 101; 12 | static int W, n; 13 | 14 | static int[] v = new int[N], w = new int[N]; 15 | static int[][] memo = new int[N][100001]; 16 | 17 | 18 | 19 | // it returns the maximum value total value 20 | static int knapsack(int index, int weight) { 21 | if (index >= n) return 0; 22 | 23 | // memoissatioon part 24 | int ans = memo[index][weight]; 25 | if (ans != -1) 26 | return ans; 27 | 28 | // do not take the item 29 | ans = knapsack(index + 1, weight); 30 | // take the item at index 31 | if (w[index] + weight <= W) { 32 | ans = max(ans, v[index] + knapsack(index + 1, w[index] + weight)); 33 | } 34 | return ans; 35 | } 36 | 37 | public static void main(String[] args) { 38 | 39 | 40 | 41 | Scanner scn = new Scanner(System.in); 42 | 43 | n = scn.nextInt(); 44 | W = scn.nextInt(); 45 | 46 | for (int i = 1; i <= n; i++) { 47 | w[i] = scn.nextInt(); 48 | v[i] = scn.nextInt(); 49 | } 50 | for(int[] a: memo) { 51 | Arrays.fill(a, -1); 52 | } 53 | 54 | int ans = knapsack(0, 0); 55 | 56 | System.out.println(ans); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /Java Codes/src/AtcoderDP/Knapsack2.java: -------------------------------------------------------------------------------- 1 | package AtcoderDP; 2 | 3 | import java.util.Scanner; 4 | 5 | import static java.lang.Math.min; 6 | 7 | public class Knapsack2 { 8 | 9 | 10 | 11 | static final int N = 101; 12 | static long W, n; 13 | 14 | static long[] v = new long[N], w = new long[N]; 15 | static long[][] mW = new long[N][100001], dp = new long[N][100001]; 16 | 17 | public static void main(String[] args) { 18 | 19 | Scanner scn = new Scanner(System.in); 20 | 21 | n = scn.nextInt(); 22 | W = scn.nextInt(); 23 | 24 | for (int i = 1; i <= n; i++) { 25 | w[i] = scn.nextLong(); 26 | v[i] = scn.nextLong(); 27 | } 28 | 29 | for (int i = 0; i <= n; i++) { 30 | for (int val = 0; val < 100001; val++) { 31 | mW[i][val] = (long) 1e13; 32 | } 33 | } 34 | 35 | dp[0][0] = 1; 36 | mW[0][0] = 0; 37 | 38 | for (int i = 1; i <= n; i++) { 39 | for (int val = 0; val < 100001; val++) { 40 | // not taking 41 | if (dp[i - 1][val] != 0) { 42 | dp[i][val] = 1; 43 | mW[i][val] = min(mW[i][val], mW[i - 1][val]); 44 | } 45 | // taking item 46 | if (val - v[i] >= 0 && dp[i - 1][(int) (val - v[i])] != 0 47 | && mW[i - 1][(int) (val - v[i])] + w[i] <= W) { 48 | dp[i][val] = 1; 49 | mW[i][val] = min(mW[i][val], mW[i - 1][(int) (val - v[i])] + w[i]); 50 | } 51 | } 52 | } 53 | 54 | int ans = 0; 55 | for (int val = 0; val < 100001; val++) { 56 | if (dp[(int) n][val] != 0) ans = val; 57 | } 58 | 59 | System.out.println(ans); 60 | 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /Java Codes/src/AtcoderDP/LCS.java: -------------------------------------------------------------------------------- 1 | package AtcoderDP; 2 | 3 | import java.util.Arrays; 4 | import java.util.Scanner; 5 | 6 | public class LCS { 7 | 8 | 9 | 10 | static final int N = 3000; 11 | 12 | static StringBuilder s, t, res; 13 | static int n, m; 14 | static int[][] memo = new int[N][N]; 15 | 16 | static int give_lcs(int i, int j) { 17 | if (i == n || j == m) return 0; 18 | 19 | int ans = memo[i][j]; 20 | 21 | if (ans != -1) return ans; 22 | 23 | if (s.charAt(i) == t.charAt(j)) { 24 | ans = give_lcs(i + 1, j + 1) + 1; 25 | } 26 | else ans = Math.max(give_lcs(i + 1, j), give_lcs(i, j + 1)); 27 | 28 | return ans; 29 | } 30 | 31 | static void trace_back(int i, int j) { 32 | if (i == n || j == m) return; 33 | 34 | if (s.charAt(i) == t.charAt(j)) { 35 | res.append(s.charAt(i)); 36 | trace_back(i + 1, j + 1); 37 | } 38 | else { 39 | if (give_lcs(i + 1, j) == give_lcs(i, j)) { 40 | trace_back(i + 1, j); 41 | } 42 | else { 43 | trace_back(i, j + 1); 44 | } 45 | } 46 | } 47 | 48 | public static void main(String[] args) { 49 | 50 | Scanner scn = new Scanner(System.in); 51 | s = new StringBuilder(scn.next()); 52 | t = new StringBuilder(scn.next()); 53 | n = s.length(); 54 | m = t.length(); 55 | 56 | for(int[] a: memo) { 57 | Arrays.fill(a, -1); 58 | } 59 | 60 | int l = give_lcs(0, 0); 61 | 62 | trace_back(0, 0); 63 | 64 | System.out.println(res); 65 | 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /Java Codes/src/AtcoderDP/LongestPath.java: -------------------------------------------------------------------------------- 1 | package AtcoderDP; 2 | 3 | import java.lang.reflect.Array; 4 | import java.util.ArrayList; 5 | import java.util.Arrays; 6 | import java.util.List; 7 | import java.util.Scanner; 8 | 9 | import static java.lang.Math.max; 10 | 11 | public class LongestPath { 12 | 13 | static final int N = (int) (1e5 + 1); 14 | 15 | static List> gr = new ArrayList<>(); 16 | static { 17 | for(int i = 0 ; i < N; i++){ 18 | gr.add(new ArrayList<>()); 19 | } 20 | } 21 | static int[] memo = new int[N]; 22 | 23 | static int dp(int cur) { 24 | int ans = memo[cur]; 25 | if (ans != -1) return ans; 26 | ans = 0; 27 | for (int x : gr.get(cur)) { 28 | ans = max(ans, dp(x) + 1); 29 | } 30 | return ans; 31 | } 32 | 33 | public static void main(String[] args) { 34 | 35 | 36 | Scanner scn = new Scanner(System.in); 37 | int n = scn.nextInt(); 38 | int m = scn.nextInt(); 39 | 40 | for (int i = 0; i < m; i++) { 41 | 42 | int x = scn.nextInt(); 43 | int y = scn.nextInt(); 44 | 45 | gr.get(x).add(y); 46 | } 47 | 48 | Arrays.fill(memo, -1); 49 | 50 | int ans = 0; 51 | for (int i = 1; i <= n; i++) { 52 | ans = max(ans, dp(i)); 53 | } 54 | System.out.println(ans); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /Java Codes/src/AtcoderDP/Slimes.java: -------------------------------------------------------------------------------- 1 | package AtcoderDP; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Slimes { 6 | 7 | static final int N = 400; 8 | 9 | static int n; 10 | static long[] a = new long[N], pref = new long[N]; 11 | static long[][] memo = new long[N][N]; 12 | 13 | static long sum(int l, int r) { 14 | return pref[r] - (l == 0 ? 0 : pref[l - 1]); 15 | } 16 | 17 | static long dp(int l, int r) { 18 | if (l == r) return 0; 19 | long ans = memo[l][r]; 20 | if (ans != 0) { 21 | return ans; 22 | } 23 | 24 | // ans=0 25 | ans = (long) 1e18; 26 | for (int i = l; i < r; i++) { 27 | ans = Math.min(ans, dp(l, i) + dp(i + 1, r) + sum(l, r)); 28 | } 29 | 30 | return ans; 31 | } 32 | 33 | public static void main(String[] args) { 34 | 35 | Scanner scn = new Scanner(System.in); 36 | n = scn.nextInt(); 37 | for (int i = 0; i < n; i++) { 38 | a[i] = scn.nextInt(); 39 | pref[i] = a[i]; 40 | if (i != 0) { 41 | pref[i] += pref[i - 1]; 42 | } 43 | } 44 | 45 | System.out.println(dp(0, n-1)); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /Java Codes/src/AtcoderDP/Stones.java: -------------------------------------------------------------------------------- 1 | package AtcoderDP; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Stones { 6 | 7 | public static void main(String[] args) { 8 | 9 | 10 | Scanner scn = new Scanner(System.in); 11 | int n = scn.nextInt(); 12 | int k = scn.nextInt(); 13 | 14 | int[] a = new int[n]; 15 | for (int i = 0; i < n; i++) { 16 | a[i] = scn.nextInt(); 17 | } 18 | 19 | int[] dp = new int[k + 1]; 20 | dp[0] = 0; 21 | 22 | for (int i = 1; i <= k; i++) { 23 | dp[i] = 0; 24 | for (int j = 0; j < n; j++) { 25 | if (i - a[j] >= 0 && dp[i - a[j]] == 0) { 26 | dp[i] = 1; 27 | break; 28 | } 29 | } 30 | } 31 | 32 | 33 | String ans = (dp[k] != 0 ? "First" : "Second"); 34 | 35 | System.out.println(ans); 36 | 37 | 38 | 39 | }} 40 | -------------------------------------------------------------------------------- /Java Codes/src/AtcoderDP/Sushi.java: -------------------------------------------------------------------------------- 1 | package AtcoderDP; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Sushi { 6 | 7 | 8 | static int n; 9 | 10 | 11 | static final int N = 301; 12 | 13 | static double[][][] memo = new double[N][N][N]; 14 | 15 | static double dp(int zero, int one, int two, int three) { 16 | // cout << zero << " " << one << " " << two << " " << three << '\n'; 17 | 18 | if (one + two + three == 0) 19 | return 0.0; 20 | 21 | double ans = memo[one][two][three]; 22 | if (ans != -1.00) return ans; 23 | ans = n; 24 | if (one != 0) ans += one * dp(zero + 1, one - 1, two, three); 25 | if (two != 0) ans += two * dp(zero, one + 1, two - 1, three); 26 | if (three != 0) ans += three * dp(zero, one, two + 1, three - 1); 27 | ans /= (double) (n - zero); 28 | return ans; 29 | } 30 | 31 | public static void main(String[] args) { 32 | 33 | Scanner scn = new Scanner(System.in); 34 | n = scn.nextInt(); 35 | int[] a = new int[n]; 36 | 37 | int[] cnt = new int[4]; 38 | 39 | 40 | for (int i = 0; i < n; i++) { 41 | a[i] = scn.nextInt(); 42 | cnt[a[i]]++; 43 | } 44 | 45 | for (int i = 0; i < N; i++) { 46 | for (int j = 0; j < N; j++) { 47 | for (int k = 0; k < N; k++) { 48 | memo[i][j][k] = -1.00; 49 | } 50 | } 51 | } 52 | 53 | 54 | double ans = dp(cnt[0], cnt[1], cnt[2], cnt[3]); 55 | System.out.println(ans); 56 | 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /Java Codes/src/AtcoderDP/TabulationVsMemorisation.java: -------------------------------------------------------------------------------- 1 | package AtcoderDP; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | import java.util.List; 6 | 7 | public class TabulationVsMemorisation { 8 | 9 | 10 | static int[] memo; 11 | static int cnt = 0; 12 | static int fib(int n) { 13 | cnt++; 14 | if (n <= 2) return 1; 15 | // memoisation part 16 | if (memo[n] != -1) return memo[n]; 17 | // recursive relation 18 | 19 | memo[n] = fib(n - 1) + fib(n - 2); 20 | return memo[n]; 21 | } 22 | 23 | public static void main(String[] args) { 24 | 25 | 26 | int n = 20; 27 | memo = new int[n]; 28 | Arrays.fill(memo, -1); 29 | System.out.println(fib(n)); 30 | System.out.println(cnt); 31 | 32 | // tabulation 33 | n = 20; 34 | int[] fib = new int[n + 1]; 35 | fib[1] = fib[2] = 1; 36 | 37 | for (int i = 3; i <= n; i++) { 38 | fib[i] = fib[i - 1] + fib[i - 2]; 39 | } 40 | 41 | System.out.println(fib[n]); 42 | 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /Java Codes/src/AtcoderDP/Vacation.java: -------------------------------------------------------------------------------- 1 | package AtcoderDP; 2 | 3 | import java.util.Scanner; 4 | 5 | import static java.lang.Math.max; 6 | 7 | public class Vacation { 8 | 9 | 10 | 11 | static final int N = (int) 1e5; 12 | 13 | static int[][] a = new int[N][3]; 14 | static int[][] dp = new int[N][3]; 15 | 16 | public static void main(String[] args) { 17 | 18 | Scanner scn = new Scanner(System.in); 19 | int n = scn.nextInt(); 20 | 21 | for (int i = 0; i < n; i++) { 22 | for (int j = 0; j < 3; j++) { 23 | a[i][j] = scn.nextInt(); 24 | } 25 | } 26 | 27 | dp[0][0] = a[0][0]; 28 | dp[0][1] = a[0][1]; 29 | dp[0][2] = a[0][2]; 30 | 31 | for (int i = 1; i < n; i++) { 32 | for (int j = 0; j < 3; j++) { 33 | for (int jj = 0; jj < 3; jj++) { 34 | if (j != jj) { 35 | dp[i][j] = max(dp[i][j], dp[i - 1][jj] + a[i][j]); 36 | } 37 | } 38 | } 39 | } 40 | 41 | 42 | int max = Integer.MIN_VALUE; 43 | for(int x: dp[n-1]){ 44 | max = Math.max(max, x); 45 | } 46 | System.out.println(max); 47 | 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /Java Codes/src/AtcoderDP/Walk.java: -------------------------------------------------------------------------------- 1 | package AtcoderDP; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Walk { 6 | 7 | 8 | static final int mod = (int) (1e9 + 7); 9 | static final int sz = 51; 10 | 11 | static class Mat { 12 | int[][] m = new int[sz][sz]; 13 | 14 | void eye() { 15 | for (int i = 0; i < sz; i++) 16 | m[i][i] = 1; 17 | } 18 | 19 | Mat multiply (Mat a){ 20 | Mat r = new Mat(); 21 | for (int i = 0; i < sz; i++) 22 | for (int j = 0; j < sz; j++) 23 | for (int k = 0; k < sz; k++) 24 | r.m[i][j] = (r.m[i][j] + m[i][k] * a.m[k][j]) % mod; 25 | return r; 26 | } 27 | } 28 | 29 | public static void main(String[] args) { 30 | 31 | 32 | Scanner scn = new Scanner(System.in); 33 | Mat a = new Mat(), res = new Mat(); 34 | 35 | int n = scn.nextInt(); 36 | int k = scn.nextInt(); 37 | 38 | for (int i = 0; i < n; i++) { 39 | for (int j = 0; j < n; j++) { 40 | a.m[i][j] = scn.nextInt(); 41 | } 42 | } 43 | 44 | res.eye(); 45 | 46 | while (k > 0) { 47 | if ((k & 1) == 1) { 48 | res = res.multiply(a); 49 | } 50 | a = a.multiply(a); 51 | k /= 2; 52 | } 53 | 54 | 55 | int ans = 0; 56 | 57 | for (int i = 0; i < n; i++) { 58 | for (int j = 0; j < n; j++) { 59 | ans += res.m[i][j]; 60 | ans %= mod; 61 | } 62 | } 63 | 64 | System.out.println(ans); 65 | 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /Java Codes/src/DFSTreesAndBackEdges/DfsTreesAndBackEdges.java: -------------------------------------------------------------------------------- 1 | package DFSTreesAndBackEdges; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | import java.util.Scanner; 6 | 7 | public class DfsTreesAndBackEdges { 8 | 9 | final static int N = (int) (1e5 + 1); 10 | 11 | static List> gr; 12 | static { 13 | for(int i = 0; i < N; i++){ 14 | gr.add(new ArrayList<>()); 15 | } 16 | } 17 | static int[] vis = new int[N]; 18 | static boolean cycle = false; 19 | 20 | static void dfs(int cur, int par) { 21 | vis[cur] = 1; 22 | for (int x : gr.get(cur)) { 23 | if (vis[x] == 0) { 24 | dfs(x, cur); 25 | } 26 | else if (x != par) { 27 | cycle = true; 28 | } 29 | } 30 | } 31 | 32 | public static void main(String[] args) { 33 | 34 | Scanner scn = new Scanner(System.in); 35 | int n = scn.nextInt(), m = scn.nextInt(); 36 | 37 | for (int i = 0; i < m; i++) { 38 | int x = scn.nextInt(), y = scn.nextInt(); 39 | gr.get(x).add(y); 40 | gr.get(y).add(x); 41 | 42 | } 43 | 44 | for (int i = 1; i <= n; i++) { 45 | if (vis[i] == 0) { 46 | dfs(i, 0); 47 | } 48 | } 49 | 50 | if (cycle) { 51 | System.out.println("Yes cycle found"); 52 | } 53 | else { 54 | System.out.println("Not found"); 55 | } 56 | 57 | 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /Java Codes/src/DFSTreesAndBackEdges/DfsTreesAndBackEdgesDirectedGraph.java: -------------------------------------------------------------------------------- 1 | package DFSTreesAndBackEdges; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | import java.util.Scanner; 6 | 7 | public class DfsTreesAndBackEdgesDirectedGraph { 8 | 9 | 10 | final static int N = (int) (1e5 + 1); 11 | 12 | static List> gr; 13 | static { 14 | for(int i = 0; i < N; i++){ 15 | gr.add(new ArrayList<>()); 16 | } 17 | } 18 | static int[] vis = new int[N]; 19 | static boolean cycle = false; 20 | 21 | static void dfs(int cur, int par) { 22 | // visited and in call stack 23 | vis[cur] = 1; 24 | for (int x : gr.get(cur)) { 25 | if (vis[x] == 0) { 26 | dfs(x, cur); 27 | } 28 | else if (x != par && vis[x] == 1) { 29 | // backedge 30 | cycle = true; 31 | } 32 | } 33 | // visited and not in call stack 34 | vis[cur] = 2; 35 | return; 36 | } 37 | 38 | public static void main(String[] args) { 39 | 40 | Scanner scn = new Scanner(System.in); 41 | int n = scn.nextInt(), m = scn.nextInt(); 42 | 43 | for (int i = 0; i < m; i++) { 44 | int x = scn.nextInt(), y = scn.nextInt(); 45 | gr.get(x).add(y); 46 | gr.get(y).add(x); 47 | 48 | } 49 | 50 | for (int i = 1; i <= n; i++) { 51 | if (vis[i] == 0) { 52 | dfs(i, 0); 53 | } 54 | } 55 | 56 | if (cycle) { 57 | System.out.println("Yes cycle found"); 58 | } 59 | else { 60 | System.out.println("Not found"); 61 | } 62 | 63 | 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /Java Codes/src/DFSTreesAndBackEdges/PrintCycleInGraph.java: -------------------------------------------------------------------------------- 1 | package DFSTreesAndBackEdges; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | import java.util.Scanner; 6 | 7 | public class PrintCycleInGraph { 8 | 9 | final static int N = (int) (1e5 + 1); 10 | 11 | static List> gr; 12 | static { 13 | for(int i = 0; i < N; i++){ 14 | gr.add(new ArrayList<>()); 15 | } 16 | } 17 | static int[] vis = new int[N], Par = new int[N]; 18 | static boolean cycle = false; 19 | 20 | 21 | 22 | static void dfs(int cur, int par) { 23 | // visited and in call stack 24 | vis[cur] = 1; 25 | Par[cur] = par; 26 | for (int x : gr.get(cur)) { 27 | if (vis[x] == 0) { 28 | dfs(x, cur); 29 | } 30 | else if (x != par && vis[x] == 1) { 31 | // backedge 32 | cycle = true; 33 | 34 | int u = cur, v = x; 35 | 36 | while (u != v) { 37 | System.out.print(u + " "); 38 | u = Par[u]; 39 | } 40 | System.out.print(u + " "); 41 | System.exit(0); 42 | } 43 | } 44 | // visited and not in call stack 45 | vis[cur] = 2; 46 | } 47 | 48 | 49 | public static void main(String[] args) { 50 | 51 | Scanner scn = new Scanner(System.in); 52 | int n = scn.nextInt(), m = scn.nextInt(); 53 | 54 | for (int i = 0; i < m; i++) { 55 | int x = scn.nextInt(), y = scn.nextInt(); 56 | gr.get(x).add(y); 57 | gr.get(y).add(x); 58 | 59 | } 60 | 61 | for (int i = 1; i <= n; i++) { 62 | if (vis[i] == 0) { 63 | dfs(i, 0); 64 | } 65 | } 66 | 67 | if (cycle) { 68 | System.out.println("Yes cycle found"); 69 | } 70 | else { 71 | System.out.println("Not found"); 72 | } 73 | 74 | 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /Java Codes/src/DivideAndConquer/TernarySearch.java: -------------------------------------------------------------------------------- 1 | package DivideAndConquer; 2 | 3 | public class TernarySearch { 4 | 5 | static double f(double x) { 6 | return - (x * x) + 3.45621278 * x; 7 | } 8 | 9 | public static void main(String[] args){ 10 | 11 | double left = -100, right = 100; 12 | 13 | while (right - left > 0.00000000001) { 14 | 15 | double mid1 = (2 * left + right) / 3; 16 | double mid2 = (left + 2 * right) / 3; 17 | 18 | if (f(mid1) < f(mid2)) { 19 | left = mid1; 20 | } 21 | else { 22 | right = mid2; 23 | } 24 | 25 | } 26 | 27 | System.out.println(left + " " + right); 28 | 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Java Codes/src/FenwickTrees/FenwickTrees.java: -------------------------------------------------------------------------------- 1 | package FenwickTrees; 2 | 3 | public class FenwickTrees { 4 | 5 | static class fenwick { 6 | int[] fn; 7 | int n; 8 | 9 | void init(int n) { 10 | this.n = n + 1; 11 | fn = new int[this.n]; 12 | } 13 | 14 | void add(int x, int y) { 15 | x++; // 1 based index 16 | while (x < n) { 17 | fn[x] += y; 18 | x += (x & (-x));// last set bit 19 | } 20 | } 21 | 22 | // void add(int x, int y) { 23 | // for (x++; x < n; x += (x & (-x))) fn[x] += y; 24 | // } 25 | 26 | int sum(int x) { 27 | x++; // 1 based index 28 | int ans = 0; 29 | while (x > 0) { 30 | ans += fn[x]; 31 | x -= (x & (-x));// last set bit 32 | } 33 | return ans; 34 | } 35 | 36 | int sum(int l, int r) { 37 | return sum(r) - sum(l - 1); 38 | } 39 | 40 | } 41 | 42 | public static void main(String[] args) { 43 | 44 | int[] v = {1, 2, 3, 4, 5, 6, 7}; 45 | fenwick tree = new fenwick(); 46 | tree.init(v.length); 47 | 48 | for (int i = 0; i < v.length; i++) { 49 | tree.add(i, v[i]); 50 | } 51 | 52 | System.out.println(tree.sum(3,5)); 53 | 54 | tree.add(4, -3); 55 | 56 | System.out.println(tree.sum(3,5)); 57 | 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /Java Codes/src/InclusionExclusion/TotalDivisorInclusion.java: -------------------------------------------------------------------------------- 1 | package InclusionExclusion; 2 | 3 | import java.util.Scanner; 4 | 5 | public class TotalDivisorInclusion { 6 | 7 | 8 | public static void main(String[] args) { 9 | 10 | Scanner scn = new Scanner(System.in); 11 | int n = scn.nextInt(); 12 | 13 | int[] primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31}; 14 | int m = primes.length; 15 | 16 | int ans = 0; 17 | 18 | for (int i = 1; i < (1 << m); i++) { 19 | 20 | int lcm = 1; 21 | for (int j = 0; j < m; j++) { 22 | // if jth bit of ith element is set then include it 23 | if (((i >> j) & 1) == 1) { 24 | // cout << primes[j] << " "; 25 | lcm *= primes[j]; 26 | } 27 | } 28 | // cout << '\n'; 29 | 30 | if (Integer.bitCount(i) % 2 == 0) { 31 | ans -= (n / lcm); 32 | } 33 | else { 34 | ans += (n / lcm); 35 | } 36 | } 37 | 38 | 39 | System.out.println(ans); 40 | 41 | 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /Java Codes/src/InteractiveProblems/GuessTheNumber.java: -------------------------------------------------------------------------------- 1 | package InteractiveProblems; 2 | 3 | import java.io.PrintWriter; 4 | import java.util.Scanner; 5 | 6 | public class GuessTheNumber { 7 | 8 | // 0 -> '<' 9 | // 1 -> '>=' 10 | 11 | static PrintWriter out = new PrintWriter(System.out); 12 | static Scanner scn = new Scanner(System.in); 13 | 14 | 15 | static boolean query(int middle) { 16 | // query 17 | out.println(middle); 18 | out.flush(); 19 | 20 | String in = scn.next(); 21 | return (in.equals(">=")); 22 | } 23 | 24 | public static void main(String[] args) { 25 | 26 | 27 | int left = 1, right = 1000000, ans = 0; 28 | 29 | while (left <= right) { 30 | int middle = (left + right) / 2; 31 | if (query(middle)) { 32 | ans = middle; 33 | left = middle + 1; 34 | } 35 | else { 36 | right = middle - 1; 37 | } 38 | } 39 | 40 | out.println("! " + ans); 41 | 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /Java Codes/src/InteractiveProblems/XorGuessing.java: -------------------------------------------------------------------------------- 1 | package InteractiveProblems; 2 | 3 | import java.io.PrintWriter; 4 | import java.util.Scanner; 5 | 6 | public class XorGuessing { 7 | 8 | static PrintWriter out = new PrintWriter(System.out); 9 | public static void main(String[] args) { 10 | 11 | 12 | int ans = 0; 13 | 14 | out.print("? "); 15 | for (int i = 1; i <= 100; i++) { 16 | out.print(i + " "); 17 | 18 | } 19 | out.println(); 20 | out.flush(); 21 | Scanner scn = new Scanner(System.in); 22 | 23 | int x = scn.nextInt(); 24 | 25 | for (int i = 7; i <= 13; i++) { 26 | if (((x >> i) & 1) == 1) { 27 | ans |= (1 << i); 28 | } 29 | } 30 | 31 | 32 | out.print("? "); 33 | for (int i = 1; i <= 100; i++) { 34 | out.print((i << 7)+ " "); 35 | 36 | } 37 | out.println(); 38 | out.flush(); 39 | 40 | x = scn.nextInt(); 41 | 42 | for (int i = 0; i <= 6; i++) { 43 | if (((x >> i) & 1) == 1) { 44 | ans |= (1 << i); 45 | } 46 | } 47 | 48 | out.println("! " + ans); 49 | out.flush(); 50 | } 51 | } 52 | 53 | 54 | -------------------------------------------------------------------------------- /Java Codes/src/LCA/TwoPointers.java: -------------------------------------------------------------------------------- 1 | package LCA; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | import java.util.Scanner; 6 | 7 | public class TwoPointers { 8 | 9 | 10 | static final int N = (int) 1e5; 11 | 12 | static List> gr; 13 | static { 14 | gr = new ArrayList<>(); 15 | for(int i = 0; i < N; i++){ 16 | gr.add(new ArrayList<>()); 17 | } 18 | } 19 | 20 | static int[] dep = new int[N], Par = new int[N]; 21 | 22 | static void dfs(int cur, int par) { 23 | Par[cur] = par; 24 | dep[cur] = dep[par] + 1; 25 | for (int x : gr.get(cur)) { 26 | if (x != par) { 27 | dfs(x, cur); 28 | } 29 | } 30 | } 31 | 32 | static int LCA(int u, int v) { 33 | if (u == v) return u; 34 | 35 | if (dep[u] < dep[v]) { 36 | int a = u; 37 | u = v; 38 | v = a; 39 | } 40 | // depth of u is more than depth of v 41 | 42 | int diff = dep[u] - dep[v]; 43 | 44 | // depth of both nodes same 45 | while (diff-- > 0) { 46 | u = Par[u]; 47 | } 48 | 49 | // until they are equal nodes keep climbing 50 | while (u != v) { 51 | u = Par[u]; 52 | v = Par[v]; 53 | } 54 | 55 | return u; 56 | } 57 | 58 | public static void main(String[] args) { 59 | 60 | Scanner scanner = new Scanner(System.in); 61 | int n = scanner.nextInt(); 62 | 63 | 64 | for (int i = 1; i < n; i++) { 65 | int x =scanner.nextInt(), y=scanner.nextInt(); 66 | gr.get(x).add(y); 67 | gr.get(y).add(x); 68 | } 69 | 70 | dfs(1, 0); 71 | System.out.println(LCA(9, 12)); 72 | 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /Java Codes/src/LinearRecurrenceAndMatrixExponentiation/BinaryExp.java: -------------------------------------------------------------------------------- 1 | package LinearRecurrenceAndMatrixExponentiation; 2 | 3 | public class BinaryExp { 4 | 5 | static int powr(int a, int b) { 6 | int res = 1; 7 | while (b > 0) { 8 | if ((b & 1) == 1) res *= a; 9 | a *= a; 10 | b /= 2; 11 | } 12 | return res; 13 | } 14 | 15 | public static void main(String[] args) { 16 | System.out.println(powr(2, 10)); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Java Codes/src/LinearRecurrenceAndMatrixExponentiation/FastMultiplication.java: -------------------------------------------------------------------------------- 1 | package LinearRecurrenceAndMatrixExponentiation; 2 | 3 | public class FastMultiplication { 4 | 5 | static int multiply(int a, int b, int c) { 6 | int res = 0; 7 | while (b > 0) { 8 | if ((b & 1) == 1) { 9 | res = res + a; 10 | res %= c; 11 | } 12 | a += a; a %= c; 13 | b /= 2; 14 | } 15 | return res; 16 | } 17 | 18 | public static void main(String[] args) { 19 | 20 | System.out.println(multiply(20, 20, 1000000007)); 21 | 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Java Codes/src/LinearRecurrenceAndMatrixExponentiation/MatrixExp.java: -------------------------------------------------------------------------------- 1 | package LinearRecurrenceAndMatrixExponentiation; 2 | 3 | import java.io.File; 4 | 5 | public class MatrixExp { 6 | 7 | final static int mod = 1000000007; 8 | final static int sz = 2; 9 | 10 | static class Mat { 11 | int[][] m = new int[sz][sz]; 12 | 13 | void identity() { 14 | for (int i = 0; i < sz; i++) { 15 | m[i][i] = 1; 16 | } 17 | } 18 | Mat multiply(Mat a) { 19 | Mat res = new Mat(); 20 | for (int i = 0; i < sz; i++) { 21 | for (int j = 0; j < sz; j++) { 22 | for (int k = 0; k < sz; k++) { 23 | res.m[i][j] += m[i][k] * a.m[k][j]; 24 | res.m[i][j] %= mod; 25 | } 26 | } 27 | } 28 | return res; 29 | } 30 | } 31 | 32 | static int Fib(int n) { 33 | Mat res = new Mat(); 34 | res.identity(); 35 | Mat T = new Mat(); 36 | T.m[0][0] = T.m[0][1] = T.m[1][0] = 1; 37 | 38 | if (n <= 2) return 1; 39 | n -= 2; 40 | 41 | // log(n) 42 | while (n > 0) { 43 | if ((n & 1) == 1) {res = res.multiply(T);} 44 | T = T.multiply(T); 45 | n /= 2; 46 | } 47 | 48 | return (res.m[0][0] + res.m[0][1]) % mod; 49 | } 50 | 51 | public static void main(String[] args) { 52 | System.out.println(Fib(10)); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /Java Codes/src/LinearRecurrenceAndMatrixExponentiation/ModularExp.java: -------------------------------------------------------------------------------- 1 | package LinearRecurrenceAndMatrixExponentiation; 2 | 3 | public class ModularExp { 4 | 5 | 6 | static final int mod = 1000000007; 7 | 8 | static long powr(long a, long b) { 9 | long res = 1; 10 | while (b > 0) { 11 | if ((b & 1) ==1 ) { 12 | res *= a; 13 | res %= mod; 14 | } 15 | a *= a; a %= mod; 16 | b /= 2; 17 | } 18 | return res; 19 | } 20 | 21 | public static void main(String[] args) { 22 | System.out.println(powr(2L, 200L)); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Java Codes/src/RandomizeAndTime/RandomAndTime.java: -------------------------------------------------------------------------------- 1 | package RandomizeAndTime; 2 | 3 | import java.util.Random; 4 | 5 | public class RandomAndTime { 6 | public static void main(String[] args) { 7 | Random rd = new Random(); 8 | rd.setSeed(42696); 9 | 10 | long currentTime = System.currentTimeMillis(); 11 | System.out.println(rd.nextFloat()); 12 | System.out.println(rd.nextInt(10)); 13 | System.out.println(rd.nextGaussian()); 14 | 15 | System.out.println(System.currentTimeMillis() - currentTime + " ms taken"); 16 | 17 | 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Java Codes/src/SquareRootDecomposition/SortingBasedOnMos.java: -------------------------------------------------------------------------------- 1 | package SquareRootDecomposition; 2 | 3 | import java.util.Arrays; 4 | import java.util.Comparator; 5 | import java.util.Scanner; 6 | 7 | public class SortingBasedOnMos { 8 | 9 | 10 | 11 | static final int N = (int) 2e5; 12 | 13 | static int[] a = new int[N]; 14 | 15 | static class query { 16 | int l, r, ans, id, block; 17 | } 18 | 19 | static query[] q = new query[N]; 20 | 21 | public static void main(String[] args) { 22 | 23 | Scanner scn = new Scanner(System.in); 24 | int n = scn.nextInt(), m; 25 | 26 | m = (int) (Math.sqrt(n) + 1); 27 | 28 | for (int i = 0; i < n; i++) { 29 | a[i] = scn.nextInt(); 30 | } 31 | 32 | int queries = scn.nextInt(); 33 | 34 | for (int i = 0; i < queries; i++) { 35 | q[i].l = scn.nextInt(); 36 | q[i].r = scn.nextInt(); 37 | q[i].l--; q[i].r--; 38 | q[i].id = i; 39 | q[i].block = q[i].l / m; 40 | } 41 | 42 | Arrays.sort(q, new Comparator() { 43 | @Override 44 | public int compare(query o1, query P) { 45 | if(o1.block!=P.block){ 46 | return o1.block - P.block; 47 | } 48 | return o1.r - P.r; 49 | } 50 | }); 51 | 52 | for (int i = 0; i < queries; i++) { 53 | System.out.println(q[i].id + " " + q[i].l + " " + q[i].r); 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /Java Codes/src/SquareRootDecomposition/SqrtDecomposition.java: -------------------------------------------------------------------------------- 1 | package SquareRootDecomposition; 2 | 3 | import java.util.Scanner; 4 | 5 | public class SqrtDecomposition { 6 | 7 | public static void main(String[] args) { 8 | Scanner scn = new Scanner(System.in); 9 | int n = scn.nextInt(), m; 10 | 11 | m = (int) (Math.sqrt(n) + 1); 12 | 13 | int[] a = new int[n], b = new int[m]; 14 | 15 | for (int i = 0; i < n; i++) { 16 | a[i] = scn.nextInt(); 17 | b[i / m] += a[i]; 18 | } 19 | 20 | int q; 21 | q = scn.nextInt(); 22 | while (q-- > 0) { 23 | int l, r, ans = 0; 24 | l = scn.nextInt(); 25 | r = scn.nextInt(); 26 | 27 | for (int i = l; i <= r;) { 28 | if (i % m == 0 && i + m - 1 <= r) { 29 | // lies completely inside the [l...r] 30 | ans += b[i / m]; 31 | i += m; 32 | } 33 | else { 34 | ans += a[i]; 35 | i++; 36 | } 37 | 38 | } 39 | 40 | System.out.println(ans); 41 | 42 | } 43 | 44 | 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /Java Codes/src/StringMatching/BruteForce.java: -------------------------------------------------------------------------------- 1 | package StringMatching; 2 | 3 | public class BruteForce { 4 | 5 | public static void main(String[] args) { 6 | String text = "ababaababbbbabaaa"; 7 | String pat = "aa"; 8 | 9 | int m = pat.length(), n = text.length(); 10 | 11 | for (int i = 0; i + m <= n; i++) { 12 | if (text.substring(i, i+m).equals(pat)) { 13 | System.out.println(i); 14 | } 15 | } 16 | 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Java Codes/src/StringMatching/PolyHash.java: -------------------------------------------------------------------------------- 1 | package StringMatching; 2 | 3 | public class PolyHash { 4 | 5 | static long poly_hash(String s) { 6 | long hash = 0, p = 31, mod = (int) (1e9 + 7); 7 | long p_power = 1; 8 | 9 | for (int i = 0; i < s.length(); i++) { 10 | hash += (long) (s.charAt(i) - 'a' + 1) * p_power; 11 | p_power *= p; 12 | 13 | hash %= mod; 14 | p_power %= mod; 15 | } 16 | 17 | return hash; 18 | } 19 | 20 | public static void main(String[] args) { 21 | 22 | String s = "abaasdasdasfasasfaba"; 23 | 24 | System.out.println(poly_hash(s)); 25 | 26 | 27 | 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Java Codes/src/StringMatching/RabinCarb.java: -------------------------------------------------------------------------------- 1 | package StringMatching; 2 | 3 | public class RabinCarb { 4 | 5 | static final int p = 31, mod = (int) (1e9 + 7); 6 | 7 | static long poly_hash(String s) { 8 | long hash = 0; 9 | long p_power = 1; 10 | 11 | for (int i = 0; i < s.length(); i++) { 12 | hash += (long) (s.charAt(i) - 'a' + 1) * p_power; 13 | p_power *= p; 14 | 15 | hash %= mod; 16 | p_power %= mod; 17 | } 18 | 19 | return hash; 20 | } 21 | 22 | 23 | static int powr(int a, int b) { 24 | // (a^b)%mod 25 | int res = 1; 26 | while (b > 0) { 27 | if ((b & 1) == 1) res *= a; 28 | b /= 2; 29 | a *= a; 30 | 31 | a %= mod; 32 | res %= mod; 33 | } 34 | return res; 35 | } 36 | 37 | static int inv(int x) { 38 | return powr(x, mod - 2); 39 | } 40 | 41 | public static void main(String[] args) { 42 | 43 | 44 | String text = "ababbabbaba"; 45 | String pattern = "aba"; 46 | 47 | long pat_hash = poly_hash(pattern); 48 | 49 | int n = text.length(), m = pattern.length(); 50 | 51 | long text_hash = poly_hash(text.substring(0, m)); 52 | 53 | if (pat_hash == text_hash) { 54 | System.out.println(0); 55 | } 56 | 57 | 58 | for (int i = 1; i + m <= n; i++) { 59 | // remove last character 60 | text_hash = (text_hash - (text.charAt(i - 1) - 'a' + 1) + mod) % mod; 61 | 62 | text_hash = (text_hash * inv(p)) % mod; 63 | 64 | text_hash = (text_hash + (long) (text.charAt(i + m - 1) - 'a' + 1) * powr(p, m - 1)) % mod; 65 | 66 | 67 | if (text_hash == pat_hash) { 68 | System.out.println(i); 69 | } 70 | } 71 | 72 | 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /Java Codes/src/StringMatching/Trie.java: -------------------------------------------------------------------------------- 1 | package StringMatching; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Trie { 6 | 7 | 8 | static class Node { 9 | Node[] nxt = new Node[26]; 10 | boolean is_end; 11 | Node() { 12 | for (int i = 0; i < 26; i++) 13 | nxt[i] = null; 14 | is_end = false; 15 | } 16 | } 17 | 18 | static Node root; 19 | 20 | static void insert_trie(String s) { 21 | Node cur = root; 22 | for (int i = 0; i < s.length(); i++) { 23 | int imap = s.charAt(i) - 'a'; 24 | // new node 25 | if (cur.nxt[imap] == null) { 26 | cur.nxt[imap] = new Node(); 27 | } 28 | // goto that node 29 | cur = cur.nxt[imap]; 30 | } 31 | // cur -> last node 32 | cur.is_end = true; 33 | } 34 | 35 | // O(|s|) 36 | static boolean search_trie(String s) { 37 | Node cur = root; 38 | for (int i = 0; i < s.length(); i++) { 39 | int imap = s.charAt(i) - 'a'; 40 | if (cur.nxt[imap] == null) 41 | return false; 42 | cur = cur.nxt[imap]; 43 | } 44 | return cur.is_end; 45 | } 46 | 47 | public static void main(String[] args) { 48 | 49 | 50 | root = new Node(); 51 | 52 | Scanner scn = new Scanner(System.in); 53 | int n = scn.nextInt(); 54 | for (int i = 0; i < n; i++) { 55 | String s = scn.next(); 56 | insert_trie(s); 57 | } 58 | 59 | if (search_trie("apaar")) { 60 | System.out.println("Found"); 61 | } 62 | else { 63 | System.out.println("Not Found"); 64 | } 65 | 66 | 67 | 68 | 69 | 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /Java Codes/src/StringMatching/TriePatternMatching.java: -------------------------------------------------------------------------------- 1 | package StringMatching; 2 | 3 | import java.util.Scanner; 4 | 5 | public class TriePatternMatching { 6 | 7 | 8 | 9 | static class Node { 10 | Node[] nxt = new Node[26]; 11 | boolean is_end; 12 | Node() { 13 | for (int i = 0; i < 26; i++) 14 | nxt[i] = null; 15 | is_end = false; 16 | } 17 | } 18 | 19 | static Node root; 20 | 21 | static void insert_trie(String s) { 22 | Node cur = root; 23 | for (int i = 0; i < s.length(); i++) { 24 | int imap = s.charAt(i) - 'a'; 25 | // new node 26 | if (cur.nxt[imap] == null) { 27 | cur.nxt[imap] = new Node(); 28 | } 29 | // goto that node 30 | cur = cur.nxt[imap]; 31 | } 32 | // cur -> last node 33 | cur.is_end = true; 34 | } 35 | 36 | // O(|s|) 37 | static boolean search_trie(String s) { 38 | Node cur = root; 39 | for (int i = 0; i < s.length(); i++) { 40 | int imap = s.charAt(i) - 'a'; 41 | if (cur.nxt[imap] == null) 42 | return false; 43 | cur = cur.nxt[imap]; 44 | } 45 | return cur.is_end; 46 | } 47 | 48 | public static void main(String[] args) { 49 | 50 | root = new Node(); 51 | 52 | String text = "ababba"; 53 | String pat = "aba"; 54 | 55 | int n = text.length(); 56 | 57 | for (int i = 0; i < n; i++) { 58 | insert_trie(text.substring(i)); 59 | } 60 | 61 | if (search_trie(pat)) { 62 | System.out.println("Found"); 63 | } 64 | else { 65 | System.out.println("Not Found"); 66 | } 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /Java Codes/src/StronglyConnectedComponents/TopologicalOrder.java: -------------------------------------------------------------------------------- 1 | package StronglyConnectedComponents; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Collections; 5 | import java.util.List; 6 | import java.util.Scanner; 7 | 8 | public class TopologicalOrder { 9 | 10 | static final int N = (int) (1e5 + 1); 11 | 12 | static List> gr = new ArrayList<>(); 13 | static { 14 | for(int i = 0; i < N; i++){ 15 | gr.add(new ArrayList<>()); 16 | } 17 | } 18 | static List order = new ArrayList<>(); 19 | static int[] vis = new int[N]; 20 | 21 | static void dfs(int cur, int par) { 22 | vis[cur] = 1; 23 | for (int x : gr.get(cur)) { 24 | if (vis[x] == 0) { 25 | dfs(x, cur); 26 | } 27 | } 28 | // I am here because I came back from the subtree 29 | order.add(cur); 30 | return; 31 | } 32 | 33 | 34 | public static void main(String[] args) { 35 | 36 | Scanner scn = new Scanner(System.in); 37 | int n = scn.nextInt(); 38 | int m = scn.nextInt(); 39 | 40 | for (int i = 0; i < m; i++) { 41 | int x = scn.nextInt(); 42 | int y = scn.nextInt(); 43 | 44 | gr.get(x).add(y); 45 | } 46 | 47 | for (int i = 1; i <= n; i++) { 48 | if (vis[i] == 0) dfs(i, 0); 49 | } 50 | 51 | Collections.reverse(order); 52 | for(int i: order){ 53 | System.out.println(i); 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /Java Codes/src/TheoremsInNumberTheory/ModuloArithmetics.java: -------------------------------------------------------------------------------- 1 | package TheoremsInNumberTheory; 2 | 3 | public class ModuloArithmetics { 4 | 5 | final static int P = 1000000007, N = 10000; 6 | 7 | static int[] fact = new int[N]; 8 | 9 | static int addm(int x, int y) { 10 | return (x + y) % P; 11 | } 12 | 13 | static int subm(int x, int y) { 14 | return ((x - y) % P + P) % P; 15 | } 16 | 17 | static int mulm(int x, int y) { 18 | return (x * y) % P; 19 | } 20 | 21 | static int powrm(int x, int y) { 22 | int res = 1; 23 | while (y > 0) { 24 | if ((y & 1) == 1){ 25 | res = mulm(res, x); 26 | } 27 | y /= 2; 28 | x = mulm(x, x); 29 | } 30 | return res; 31 | } 32 | 33 | static int inv(int x) { 34 | return powrm(x, P - 2); 35 | } 36 | 37 | static int divm(int x, int y) { 38 | return mulm(x, inv(y)); 39 | } 40 | 41 | static void calculate_factorials() { 42 | fact[0] = 1; 43 | for (int i = 1; i < N; i++) { 44 | fact[i] = mulm(fact[i - 1], i); 45 | } 46 | } 47 | 48 | static int ncr(int n, int r) { 49 | return mulm(mulm(fact[n], inv(fact[r])), inv(fact[n - r])); 50 | } 51 | 52 | public static void main(String[] args) { 53 | 54 | 55 | System.out.println(addm(3, 5)); 56 | System.out.println(subm(3, 5)); 57 | System.out.println(mulm(324, 234)); 58 | System.out.println(divm(56, 2)); 59 | calculate_factorials(); 60 | System.out.println(fact[5]); 61 | System.out.println(ncr(5,2)); 62 | System.out.println(ncr(5778,123)); 63 | 64 | 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /Java Codes/src/Trees/AncestorPrinting.java: -------------------------------------------------------------------------------- 1 | package Trees; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | import java.util.Scanner; 6 | 7 | public class AncestorPrinting { 8 | 9 | final static int N = 100000; 10 | 11 | static List> gr = new ArrayList<>(N); 12 | static { 13 | for(int i = 0; i < N; i++){ 14 | gr.add(new ArrayList<>()); 15 | } 16 | } 17 | static int[] Par = new int[N]; 18 | 19 | static void dfs(int cur, int par) { 20 | Par[cur] = par; 21 | for (int x : gr.get(cur)) { 22 | if (x != par) { 23 | // x is child node 24 | dfs(x, cur); 25 | } 26 | } 27 | } 28 | 29 | public static void main(String[] args) { 30 | 31 | Scanner scn = new Scanner(System.in); 32 | int n = scn.nextInt(); 33 | for (int i = 0; i < n - 1; i++) { 34 | int x = scn.nextInt(), y = scn.nextInt(); 35 | gr.get(x).add(y); 36 | gr.get(y).add(x); 37 | } 38 | 39 | dfs(10, 0); 40 | 41 | int x = 6; 42 | // print all ancestors of 5 43 | 44 | while (x > 0) { 45 | System.out.println(x); 46 | x = Par[x]; 47 | } 48 | 49 | 50 | } 51 | } 52 | --------------------------------------------------------------------------------