Remove the Nth Node From the End of a Linked List in One Pass

The task is to delete the node that is n positions away from the tail of a singly-linked list and return the new head. The optimal approach performs the operasion in a single travesral, leveraging a two-pointer technique and a sentinel node. Key Idea Use a sentinel (dummy) node to eliminate edge-case handling for the head. Advance the lead poi ...

Posted on Wed, 04 Nov 2026 16:38:45 +0000 by laserlight

Detecting and Locating Cycles in Singly Linked Lists

A singly linked list contains a cycle when traversing the next pointers leads back to a previously visited node. This occurs when the tail node’s next field points not to NULL, but to some node earlier in the list—including possibly itself. Cycle Detection Using Floyd’s Tortoise and Hare Algorithm To determine whether a cycle exists, initialize ...

Posted on Thu, 29 Oct 2026 16:24:26 +0000 by dingus

Algorithm Problem-Solving Techniques

Prefix Sum Algorithm Example 1: Interstellar Cake Problem Approach: Brute Force to Prefix Sum + Binary Search Optimization #include <algorithm> #include <iostream> using namespace std; int result; const int N = 200005; long long n, q, arr[N], countFactor[N], prefixSum[N], x; int main() { scanf("%lld", &n); ...

Posted on Thu, 15 Oct 2026 16:12:57 +0000 by ridckie_rich

Two Pointer and Sliding Window Techniques for Competitive Programming

Two Pointers Two pointers is a fundamental technique that uses two references to traverse data structures simultaneously. There are two primary variants: Collision Pointers (Converging Pointers): Both pointers start from opposite ends and move toward each other. Fast and Slow Pointers: Both pointers move in the same direction but at different s ...

Posted on Mon, 05 Oct 2026 16:20:51 +0000 by maxf

Find Kth Node from End in Linked List

Problem Overview Given a singly linked list, the task is to return the value of the k-th node from the end of the list. For example, given a list with nodes 1 → 2 → 3 → 4 → 5 and k = 2, the function should return 4, which is the value of the second node from the end. #### Solution 1: Calculate Length and Traverse This method involves two main s ...

Posted on Sat, 03 Oct 2026 16:25:07 +0000 by bfinucan

Codeforces Round 1054 (Div. 3) Solutions

A. Be Positive Count how many elements are negative. If the amount is odd, flip one more element so the product becomes positive. void solve() { int n; cin >> n; int neg = 0; for (int i = 0; i < n; ++i) { int v; cin >> v; if (v < 0) ++neg; } cout << (neg & 1 ? neg + 1 : neg) <&l ...

Posted on Tue, 29 Sep 2026 16:55:02 +0000 by cunoodle2

Sliding Window, Spiral Matrix Generation, and Prefix Sum Techniques

LeetCode 209: Minimum Size Subarray Sum Problem Statement Given an array of n positive integers and a positive integer target, find the minimum length of a contiguous subaray whose sum is greater than or equal to target. Return 0 if no such subarray exists. Sliding Window Solution The sliding window technique provides an elegant O(n) solution. ...

Posted on Sun, 27 Sep 2026 16:36:03 +0000 by ReDucTor

Two-Pointer Techniques for Array Manipulation Algorithms

In-Place Element Removal When tasked with filtering out specific values from an array in-place, allocating additional memory is often restricted. The two-pointer method provides an elegant solution by separating the reading and writing processes. Strategy: Read and Write Pointers We initialize two distinct indices: a write_idx to track the posi ...

Posted on Mon, 21 Sep 2026 16:38:55 +0000 by jacobelias

Optimizing In-Place Zero Duplication Using a Two-Pass Two-Pointer Technique

Problem Definition Given a fixed-length array of integers, the objective is to duplicate every occurrence of the value zero. When a zero is duplicated, all subsequent elements must be shifted one position to the right. Any values that would extend beyond the original array boundaries are discarded. The transformation must be executed directly w ...

Posted on Sat, 22 Aug 2026 16:22:53 +0000 by lorddraco98

Algorithm Problem Solutions: Snowflakes, Sequences, and Graph Theory

Problem 1: Unique Snowflake Collection Problem Statement: At n different times, snowflakes of various shapes fall (represented by distinct integers). We want to collect snowflakes from time a to time b such that no duplicate shapes are collected, and the total number of snowflakes collected is maximized. Solution Approach: Two Pointers Techniqu ...

Posted on Thu, 06 Aug 2026 16:35:07 +0000 by mispris006