This repository contains essential data structures and algorithms implemented in Java for competitive programming.
🔗 I'm SumitXorY / sumit_chauhan on coding platforms:
📘 Codeforces | 🍛 CodeChef
Feel free to use the codes — and if you find any bugs, don’t hesitate to report them!
🧱 Data Structures
- Next Smaller & Bigger Element using Stack (O(n))
- Disjoint Set (Union-Find)
- Centroid Decomposition
- Lowest Common Ancestor (LCA)
- Virtual Tree
- Fenwick Tree / Binary Indexed Tree (BIT)
- Segment Tree - Basic
- Segment Tree - Range Updates (Add)
- Segment Tree - Range Updates (Assign)
- Kth Order Statistic on Subarray (No Updates)
📌 Algorithms
🧵 Strings
🧮 Math
⚡ Fast I/O for Java: Template
Inspired by uwi, this I/O template greatly improves Java input/output speed for contests.
- Avoids slow
ScannerandBufferedReader. - No more TLE due to I/O after switching to this.
- Write your logic inside
solve()—main()calls it.
int x = ni(); // next int
long y = nl(); // next long
double d = nd(); // next double
String s = ns(); // next string
char c = nc(); // next char
int[] arr = na(n); // array of size n
char[][] grid = nm(n, m); // character matrixpn(obj); // println
p(obj); // printint maxVal = max(arr); // max from array
int minVal = min(arr); // min from array
int g = gcd(x, y); // gcd of two numbersIncludes a custom Pair class supporting hashing and sorting — usable in HashSets, HashMaps, and TreeMaps.