Essential Algorithm Templates for Competitive Programming
Sorting Algorithms
Quick Sort (Manual Implementation)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 100010;
ll arr[MAXN];
int n;
void quickPartition(int left, int right) {
if (left >= right) return;
int pivotIdx = (left + right) / 2;
ll pivotVal = arr[pivotIdx];
int i = left ...
Posted on Sun, 09 Aug 2026 16:16:01 +0000 by godyn
Validating Balanced Parentheses Sequences
Problem Definition
Given a string s containing only the characters (, ), {, }, [, and ], determine if the input string is valid. An input string is valid if:
Open brackets must be closed by the same type of brackets.
Open brackets must be closed in the correct order.
Every close bracket has a corresponding open bracket of the same type.
Examp ...
Posted on Thu, 06 Aug 2026 16:39:19 +0000 by blackcell
Binary Tree Traversal Algorithms: Preorder, Inorder, Postorder, and Level Order
Binary tree traversal is a fundamental operation in computer science, visiting each node in the tree in a specific order. This article covers four essential traversal methods with both recursive and iterative implementations.
Preorder Traversal (Root-Left-Right)
Preorder traversal visits the root node first, then the left subtree, followed by t ...
Posted on Wed, 05 Aug 2026 16:31:07 +0000 by centered effect
Heavy-Light Decomposition for Tree Data Management
Introduction
Heavy-light decomposition (HLD) is a sophisticated algorithmic technique used to partition tree structures into linear sequences, enabling efficient query and update operations. This method is particularly effective for handling subtree and path queries on trees.
Core Definitions
Heavy Child: For any node, its heavy child is the c ...
Posted on Tue, 04 Aug 2026 16:56:15 +0000 by cemeteryridge
Memory Architecture and CPU Coordination: Core Mechanisms Explained
Memory subsystems and central processing units form the cornerstone of computational architecture. Their synchronized operation enables program execution, with each component being indispensable. The processor relies on memory for instruction and data storage, while memory serves no purpose without a processing unit to manipulate its contents.
...
Posted on Mon, 03 Aug 2026 17:00:51 +0000 by costamesakid
Implementing Student-Course Mapping with C++ Vector Containers
Course Registration Query Using Vector ArraysWhen handling dynamic data where the number of items per entity varies, std::vector provides an ideal solution. Consider a scenario where we need to maintain course enrollment records and retrieve a specific student's course list on demand.Problem AnalysisThe input provides course information includi ...
Posted on Mon, 03 Aug 2026 16:44:23 +0000 by vaanil
A Comprehensive Guide to Python Lists
Understanding Python Lists
Lists represent one of the most versatile and frequently used data structures in Python. A list is an ordered, mutable collection of elements that can hold items of any data type. Unlike arrays in some programming languages, Python lists can dynamically resize as you add or remove elements, making them incredibly flex ...
Posted on Fri, 31 Jul 2026 16:55:27 +0000 by santopernola
C++ STL Container: List Internals
List Object Structure
The std::list in C++ Standard Template Library is implemented as a doubly-linked circular list. Each node in the list inherits from a common base class called _List_node_base.
The list container itself contains a single header node, which is allocated on the stack. Individual elements are dynamically allocated on the heap ...
Posted on Thu, 30 Jul 2026 16:04:42 +0000 by joon
String Hashing Techniques and Applications
Properties of String Hashing
Different hash values guarantee different strings.
Identical hash values don't guarantee identical strings (though probability is high).
Modulus Selection
Prime moduli are preferable based on number theory. For example, (ax + b) mod p distributes with interval gcd(a, p). The modulus must prevent overflow in 64-bi ...
Posted on Wed, 29 Jul 2026 16:20:42 +0000 by lalabored
Implementing a Doubly Circular Linked List in C
A doubly circular linked list supports core operations such as initialization, destruction, emptiness checking, traversal, insertion, deletion, search, and modification. Insertion and deletion can further be categorized into head/tail vraiants.
This implementation is organized across three files:
List.h: Declares the node structure and functio ...
Posted on Sat, 25 Jul 2026 17:02:53 +0000 by Basdub