The notebook folder contains general-purpose algorithms, and the solutions folder contains the solved problems. Feel free to follow my progress on my main online judge profiles:
Β· #divide-&-conquer Β· #brute-force Β· #greedy Β· #ad-hoc Β· #graphs Β· #geometry Β· #dynamic-programming Β· #math Β· #searching Β· #miscellaneous Β·
- two pointers
- π₯΅ codeforces/1041-D
- π codeforces/381-A
- π codeforces/6-C
- π³ uva/967:
pope - π³ codeforces/279-B
- π³ codeforces/676-C
- union-find disjoint sets (UFDS)
- π uva/10608
- π uva/11966
- π³ codeforces/25-D
- π codeforces/1249-B2
- segment tree
- π codeforces/339-D
- lazy propagation
- π³ uri/2658 β build a segment tree for RSQ, but store an array of size 9 in tree[v], where tree[v][n] indicates the frequency that the note n appears in that interval
- π³ uva/11402 β build a segment tree for RSQ, but keep in lazy[v] the type of pending operation to be performed in that interval of A
- binary search
- π³ codeforces/1324-D:
given the A and B arrays, count the quantity of pairs i < j such that A[i]+A[j] > B[i]+B[j]β diff being the A-B array, count, for all i in [0 .. N-2], how many times -diff[i] < diff[j], for all j in [i+1 .. N-1] - π³ codeforces/1284-B
- π³ codeforces/91-B:
given the array a, compute the array d, where d[i] = j-(i+1) for the max j such that a[j] < a[i]β apply binary search on preprocessed array mn, filled from right to left - π spoj/PAIRS1:
given the A array, count the quantity of pairs i < j such that A[i]-A[j] == Kβ search on the sorted array A by the A[n]+K, for all n in [0 .. N-1] - on answer
- π³ codeforces/1223-C
- π₯΅ codeforces/460-C
- π³ spoj/AGGRCOW:
given an array A of size N (2 <= N <= 10^5), print the largest minimum distance between any two of C (C <= N) elements choosen any - π³ uri/2973
- π³ uva/12097
- π³ codeforces/1324-D:
- combinatorics
- π codeforces/131-B
- π codeforces/459-B:
find the max difference between numbers of a given array and the number of ways to get it - fibonacci numbers
- π³ uva/10334 β compute (n+2)-th fibonnaci number
- combinations
- binomial coefficient
- π codeforces/844-B
- π₯΅ uri/2972:
calculate the sum of C(N, k)%2 for all k in [0 .. n], i.e., how many odd combinations of k heads between n coins there areβ just compute 2^qtyBitsOn(n)
- multi-combinations
- π³ codeforces/630-G β C(n+5-1,5) * C(n+3-1,3)
- binomial coefficient
- ad-hoc
- π uva/10812
- π uva/11875
- π codeforces/16-C
- π uva/10110:
check if the number of divisors of n is oddβ check if n is a perfect square - π uva/10346
- π codeforces/1042-A
- π³ codeforces/573-A
- π spoj/BISHOPS:
how many bishops can be placed on a n x n chessboard without threatening each other? - π uva/11723
- π uva/12791
- arithmetic progression
- π³ uva/11254
- sequences
- finding pattern
- π₯΅ uva/11718 β compute K * N^(K-1) * sumA using fast power mod
- π³ uva/10161
- π codeforces/1324-A
- π spoj/EIGHTS
- π³ codeforces/1092-D1 β remove adjacent ones whose absolute difference is even (using a stack)
- π codeforces/1221-C
- number theory
- π³ uri/2291:
print n-th divine number, the one that is equal to the sum of the sum of each divisor from 1 to nβ think like sieve - π³ uva/547:
find the longest DDF sequenceβ pre-process a array f, where f[i] is the sum of digits of all positive factors of i; think like sieve - prime numbers
- π spoj/PRIONPRI:
prime checking - sieve of eratosthenes
- π spoj/AMR11E:
print the n-th number that has at least 3 distinct prime factorsβ use modified sieve - π codeforces/230-B:
check if a large n has exactily three distinct positive divisorsβ check if sqrt(n) is prime - π³ codeforces/576-A
- π³ spoj/PRIME1:
print all primes p in [m .. n], 0 <= m <= n <= 10^9, n-m <= 10^5β sieve + prime checking - π³ uva/10539:
compute the quantity of non-prime numbers in [lo .. hi] which are divisible by only a single prime number, 0 < lo <= hi < 10^12β generate all powers of each prime number
- π spoj/AMR11E:
- prime factorization
- π₯΅ uri/2661:
compute the number of divisors of n that can be written as the product of two or more distinct prime numbers (without repetition), 1 <= n <= 10^12β note that the product of any combination of prime factors of a number will always be a divisor of that number - π³ uva/10139:
do n! is divisible by m?β check if the prime factors of m are contained in the prime factors of n! - π uva/10042
- π₯΅ uri/2661:
- π spoj/PRIONPRI:
- greatest common divisor (GCD)
- π³ codeforces/75-C:
find the gcd(a, b) that is in a query range [lo .. hi]β note that all divisors of gcd(a, b) are also divisors of a and b - π codeforces/822-A
- π codeforces/854-A:
given n, determine maximum possible proper (a < b) and irreducible fraction a/b such that a+b == n
- π³ codeforces/75-C:
- module arithmetic
- π³ uri/2291:
- game theory
- minimax
- π³ uva/10111:
given a state of a tic tac toe board, check if X will win independent of the O movementβ minimax + memo + backtracking - π³ uva/12484:
alberto and wanderley take one of two cards at the edges of the cards sequence, alberto want maximize itβ fill memo table in row-major order - optimized
- π₯΅ uva/847:
multiplication gameβ note that if Stan always multiply by 9 and Ollie by 2, it's still an optimal solution
- π₯΅ uva/847:
- π³ uva/10111:
- minimax
- π uva/10943
- π³ uva/10721
- π uva/10337
- π uva/10003
- π³ codeforces/166-E
- π spoj/TRT β don't memoize the current day
- π uva/11450
- π³ codeforces/1061-C β use space saving + all divisors in O(sqrt(n)) to optimize
- π³ uva/10651 β use bitmasks
- 0-1 knapsack
- π spoj/SCUBADIV
- π uri/2498
- π uva/990:
with recovering - π³ uri/1487
- π spoj/KNAPSACK
- π³ spoj/FISHER:
get the (min) total value and total weight of the optimal knapsack - π³ uva/10261:
as if there were 2 knapsacks to fill at the same time; with recovering
- longest common subsequence (LCS)
- π spoj/IOIPALIN:
given a string, determine the minimal quantity of characters to be inserted into it in order to obtain a palindromeβ compute length of string minus lcs between string and reversed string - π spoj/ADASEQEN
- π spoj/IOIPALIN:
- digit
- π³ uri/1707:
given two numbers x and y, compute the sum of the decimal digits of the odd numbers in the range [min(x,y) .. max(x,y)] - π spoj/CPCRC1C
- π³ uri/1707:
- coin change
- π³ uva/166:
coin change with limited amount of coins + greedy coin change with unlimited amount of coins; I don't know why, but it works without memo - π uva/11407 β consider the coins as the perfect squares
- π codeforces/189-A
- π³ uva/166:
- subset sum
- π uri/1203:
check if any subset of A has a sum equal to K - π³ uva/10616:
given an array of size N, count how many subsets of size M have their sum divisible by Dβ use module arithmetic - π³ uva/11658:
find the smallest sum s of a subset of A, s >= Sβ scan as %d.%d - π uva/624:
print the subset of A with the max sum k, k <= K - with repetition
- π uri/1203:
- traveling salesman problem (TSP)
- longest increasing subsequence (LIS)
- max 1D range sum
- π³ uva/787:
max 1D range product queryβ compute for each sub-range without 0 - kadane
- π codeforces/1285-B
- π uva/12640:
find the max range sum considering the possibility of a sub-range of length 0 - π uva/10684
- π spoj/MAXSUMSU
- π³ uva/787:
- max 2D range sum
- π uva/11152
- π codeforces/659-D
- π codeforces/1047-B
- traversal
- π uva/11831 β consider grid as an implicit graph and walk through it, or just rotate the robot, for each received instruction
- π³ uva/11906 β consider grid as an implicit graph and walk through it avoiding redundant positions (nr, nc)
- π³ code-jam/2020-1A-pascal-walk-TLE
- bipartite checking
- π uva/10004
- bridges or articulation points
- π₯΅ uva/12363:
given an undirected graph, check if there is a unique path between 2 query verticesβ there is a unique path between s and t if the path between them is formed only by bridge edges; for optimize, keep sets for the vertices that are on the same connected component in the pruned graph (with only bridge edges) - π uva/796
- π₯΅ uva/12363:
- flood fill
- topological sorting
- π uva/11060
- strongly connected components (SCC)
- π³ uva/11504 β count the number of SCCs without incoming edge from a vertex of another SCC
- depth-first search (DFS)
- π³ uri/2965
- edge classification
- π codeforces/510-B:
check if a given implicit undirected graph has a cycle of length >= 4 - π codeforces/104-C:
check if the given undirected graph can be represented as a set of three or more rooted trees, whose roots are connected by a simple cycleβ check if the graph is connective and has only one cycle
- π codeforces/510-B:
- specials
- π³ uva/12442:
given a graph with all vertices with out-degree 1, find the vertice that reaches the most vertices - directed acyclic graph (DAG)
- π uva/988:
counting paths from 0 to V-1
- π uva/988:
- tree
- π spoj/LABYR1:
compute the diameter of a given implicit tree - π codeforces/115-A:
given a forest, find the length of the longest path - π³ spoj/ONP:
infix to postfix conversionβ see that the given expression is the in-order traversal in a binary tree, then print post-order traversal recursively without building the tree
- π spoj/LABYR1:
- π³ uva/12442:
- shortest path
- all-pairs
- π uri/2372
- single-source
- unweighted graph
- π³ uva/12797 β apply a BFS for each subset of letters possible (only 2^10) using bitmask
- weighted graph
- π³ uva/11833 β build the graph already with the given constraints
- π₯΅ live-archive/3652:
dijkstra on a given implicit graph as a 2D grid - π₯΅ uva/11367 β find the shortest path on state-space graph, where each vertex represent a city and a level of car fuel
- π³ uva/10806:
find the global shortest path from 0 to V-1 (round trip), without repeting edges
- negative-weighted and cycle graph
- π³ at-coder/ABC137-E:
longest distance from 0 to V-1, checking for positive cycle that are part of that path (0 to V-1)
- π³ at-coder/ABC137-E:
- unweighted graph
- all-pairs
- minimum spanning tree (MST)
- π codeforces/1220-A
- π uri/2968
- π spoj/SBANK
- π codeforces/37-A
- π uva/10141
- π uri/3024
- π₯΅ uri/1368
- π uri/2242
- π codeforces/151-A
- π code-jam/2019-QR-foregone-solution
- π uri/2963
- π uri/2879
- π spoj/EC_CONB
- π uva/12798
- π code-jam/2020-1C-overexcited-fan
- π uri/2884
- π uva/12015
- π spoj/ADAQUEUE
- π uva/11799
- π codeforces/1285-A
- implementation
- π uri/1215
- π codeforces/158-C
- π uva/105:
skyline - π³ uri/2971
- π³ codeforces/1296-C β maps each position of the robot with the string index, and check if a new position has already been mapped before
- π uri/1975
- π codeforces/1284-A
- π³ codeforces/1254-A
- π codeforces/492-B
- π code-jam/2020-QR-vestigium
- π³ codeforces/519-C
- π codeforces/811-B
- π uri/1261
- π uri/1763
- π codeforces/81-A
- π codeforces/373-A
- π uri/1260
- π codeforces/110-A
- π codeforces/266-A
- π uri/2593
- π spoj/GNY07D
- π spoj/BUSYMAN:
compute the maximum number of activities (each with start and end times) that you can do without overlapping them (one at a time)β sort the activites by increasing end time - π codeforces/545-D
- π spoj/GERGOVIA
- π code-jam/2020-QR-parenting-partnering-returns
- π codeforces/1257-A
- π codeforces/1324-C β note that it's unnecessary jump to the left, so to minimize d, just jump between the nearest 'R' cells
- π³ code-jam/2020-1A-pattern-matching
- π codeforces/1092-B
- π codeforces/1324-B:
given a sequence of integers, is there a subsequence palindrome of size 3?β check if there are two equal non-adjacent numbers using brute force - π codeforces/984-A
- π uva/10954:
add all - π spoj/SNGINT:
given an integer n, find the smallest positive integer m > 0 such that the product of digits of m equals n - π³ codeforces/1237-C2 β sort the points, remove pairs with equal x and y first, then pairs with equal x and finally the rest
- π³ codeforces/204-B:
given the two-sided values of N cards, what is the minimum number of turns in the cards so that at least half of them are the same front?; they are initially facing upwards - π codeforces/275-C
- π spoj/CADYDIST
- π code-jam/2020-QR-nesting-depth
- interval covering
- π³ uva/10382 β reduce the problem using pythagoras to one line
- bipartite matching
- π uva/11292
- restrict coin change
- π codeforces/1255-A
- fast longest increasing subsequence (LIS)
- π³ uva/481
- loading balance
- iterative
- π uva/12801
- π uva/10360
- π codeforces/633-A:
check if a diophantine equation has positive solution - π uva/628
- π uva/725
- π³ uva/12792:
simulationβ note that if you 'watch' a unique card, the full deck will become sorted as soon as this card reaches its original position - all permutations
- π uva/750
- all subsets
- π uva/12455 β use bitmasks
- recursive backtracking
- π uva/10344:
check if some arithmetic expression of 5 given numbers will result in 23β check all combination of operators for each permutation of the given numbers - π³ uva/10503:
given domino pieces, check if it is possible to arrive at a target piece from an initial piece using N intermediate pieces (possibly rotating them)β DFS + backtracking - π live-archive/2883:
chess with a single horse against up to 8 pawns; print the length of the minimum sequence of horse moves if it wins - π spoj/MKJUMPS:
given an unweighted implicit graph, count the longest possible pathβ DFS + backtracking - π code-jam/2020-QR-indicium-TLE:
generate M, where M[i][j] is any value in [1 .. N] (2 <= N <= 5), but without repeting a value in same line or column, and with the sum of the main diagonal equal to K - sudoku
- π uva/989
- pruned permutations
- n-queens
- π³ uva/11195 β use bitmasks
- π uva/10344:
- π³ codeforces/768-B β knowing the size of the final array with a little math, use the same idea to query a range in a segment tree