Core Algorithmic Techniques in Java for Competitive Programming

Sorting, searching, dynamic programing, and graph algorithms form the backbone of competitive programming. This article presents a curated list of such patterns, each accompanied by a concise Java implementation. The examples draw inspiration from the ACwing problem set, covering topics from basic sorting to advanced combinatorial mathematics. ...

Posted on Mon, 15 Jun 2026 17:07:00 +0000 by zhaohongli

LeetCode 54: Spiral Matrix and 445: Add Two Numbers II

LeetCode 54: Spiral Matrix Problem Given an m x n matrix, return all elements of the matrix in spiral order. Example 1 Input: matrix = [[1,2,3],[4,5,6],[7,8,9]] Output: [1,2,3,6,9,8,7,4,5] Example 2 Input: matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]] Output: [1,2,3,4,8,12,11,10,9,5,6,7] Approach Traverse from left to right along the top r ...

Posted on Sun, 14 Jun 2026 16:12:36 +0000 by thebusinesslad

Essential Algorithm Templates in C++

Number Theory Primality Testing via Trial Division #include <iostream> using namespace std; bool isPrime(int num) { if (num < 2) return false; for (int d = 2; d <= num / d; ++d) if (num % d == 0) return false; return true; } int main() { int queries; cin >> queries; while (queries--) { ...

Posted on Sat, 13 Jun 2026 17:12:21 +0000 by sebjlan

Solving the Two Sum Problem in Python

Problem Statement Given an array of integers, find two distinct indices such that the corresponding elements sum to a specified target value. Each input is guaranteed to have exactly one solution, and elements cannot be reused. Example: For input array [2, 7, 11, 15] and target 9, return [0, 1] since 2 + 7 = 9. Solution Approach A brute-force s ...

Posted on Thu, 11 Jun 2026 18:03:52 +0000 by Helljumper

Mastering Two-Pointer Patterns for Algorithmic Problems

283. Move Zeroes The objective is to reorganize an array such that all non-zero elements are positioned before any zeros. This operation must be performed in-place without allocating additional space for another array. Instead of using a secondary buffer, we can utilize a tracking pointer to mark the position where the next non-zero element sho ...

Posted on Thu, 11 Jun 2026 17:56:17 +0000 by coldfused

Core Linked List Manipulation Patterns: Swapping, Removal, and Cycle Detection

Sentinel Node Strategy A dummy or sentinel node simplifies edge cases where the head pointer might change. By initializing a new node that points to the original head, operations such as deletion or swapping can be treated uniformly with out special casing the start of the list. auto* sentry = new ListNode(0); sentry->next = head; 24. Swap ...

Posted on Thu, 11 Jun 2026 17:33:36 +0000 by designguy79

Large Integer Primality Testing and Factorization Algorithms

Miller-Rabin Primality TestThe Miller-Rabin algorithm is a probabilistic method used to determine if a large number is prime. It builds upon Fermat's Little Theorem, which states that for a prime $ p $ and integer $ a $ such that $ 1 \le a < p $, the congruence $ a^{p-1} \equiv 1 \pmod p $ holds. However, relying solely on this theorem allows f ...

Posted on Thu, 11 Jun 2026 16:38:34 +0000 by jl

Understanding the Core Principles of Dynamic Programming

Dynamic programming (DP) is an optimization paradigm that solves complex problems by decomposing them into overlapping subproblems whose solutions are cached to avoid recomputation. It relies on two key properties: optimal substructure and overlapping subprobelms. Optimal substructure means an optimal solution can be built from optimal solution ...

Posted on Wed, 10 Jun 2026 17:41:35 +0000 by aircooled57

Implementing Self-Balancing AVL Trees in C++

A self-balancing AVL tree maintains near-perfect binary search tree height after each insertion or deletion by ensuring that for any given node, the height difference between its left and right subtrees is at most one. This property prevents the performance degradation associated with skewed binary search trees. The core implementation revolves ...

Posted on Mon, 08 Jun 2026 17:58:20 +0000 by bryson

License Key Formatting Algorithm

Problem Description Design an algorithm to reformat license keys according to specific rules. A license key consists of alphanumeric characters and dashes (-). These dashes seperate the characters into groups. Given a required group size K, rearrange the key so that every group except possibly the first contains exactly K characters. The first ...

Posted on Mon, 08 Jun 2026 17:09:19 +0000 by Rithotyn