Minimal Coprime Groups Partitioning via Depth-First Search

Suppose you are given an integer array arr. The goal is to split it into the fewest possible subsets such that every pair of elements inside the same subset is coprime (their greatest common divisor equals 1). We can solve this problem using a DFS backtracking approach. Below are two distinct strategies, each corresponding to a different way of ...

Posted on Tue, 08 Sep 2026 16:30:37 +0000 by PhilVaz

Recursive Techniques for Generating Subsets and Permutations

Recursion can be categorized into path-aware and path-unaware forms. Most tree-related recursions are path-aware. Fundamentally, recursion implements depth-first search (DFS). To solve problems recursively, treat the recursive function as a black box that handles a subproblem, then reuse it. Beginners often try to fully expand the recursion, wh ...

Posted on Sat, 05 Sep 2026 16:48:55 +0000 by denoteone

Word Chain Problem from NOIP2000 Advanced Group

The word chain problem involves constructing the longest possilbe sequence ("dragon") from a given set of words, starting with a specified character. Each word may be used at most twice in the chain. When two words are joined, overlapping parts are merged into one—e.g., beast and astonish form beastonish. Importantly, no word in the c ...

Posted on Mon, 17 Aug 2026 16:03:01 +0000 by ryankentp

Backtracking Algorithm Practice: Combination Sum III and Letter Combinations

Problem 216: Combination Sum III Description: Given two integers k and n, find all possible combinations of k numbers from 1 to 9 that add up to n. Each number can only be used once in a combination. Approach This problem requires finding subsets of size k from the set [1,2,3,4,5,6,7,8,9] where the sum equals n. The parameter k represents the d ...

Posted on Sat, 15 Aug 2026 16:09:44 +0000 by heerajee

Backtracking Algorithms for Combinatorial Generation

Generating the complete power set of a collection involves binary decisions at each element. The recursive approach branches twice: once including the current element and once excluding it. public void enumerateSubsets(int[] data, List<Integer> buffer, int idx) { if (idx == data.length) { System.out.println(buffer); re ...

Posted on Fri, 14 Aug 2026 16:51:09 +0000 by TheHyipSite

Binary Search for Rotated Arrays, Arrow Balloons, and Palindrome Partitioning

153. Minimum in Rotated Sorted Array Approach A rotated sorted array is formed by shifting elements from the end to the beginning. For instance, rotating [0,1,2,4,5,6,7] four times yields [4,5,6,7,0,1,2]. To find the minimum element efficiently, use binary search. Compare the middle element with the rightmost element: If arr[mid] < arr[righ ...

Posted on Wed, 05 Aug 2026 16:35:41 +0000 by fuzzy1

Generating Permutations and Combinations Using Depth-First Search

Permutations This article demonstrates a method for generating all permutations of a set of numbers using depth-first search (DFS). #include <iostream> #include <vector> #include <algorithm> using namespace std; const int MAX_SIZE = 100010; int size, sequence[MAX_SIZE]; bool visited[MAX_SIZE]; void generatePermutations(int ...

Posted on Tue, 04 Aug 2026 17:03:36 +0000 by nominator

Backtracking Algorithms for Combination Problems in LeetCode

Overview of Backtracking Backtracking is a systematic way to explore all potential solutions by building combinations incrementally and backtracking when a path fails to meet constraints. It's particularly useful for problems like combinations, permutations, subsets, and other combinatorial searches. Common problem types solved with backtrackin ...

Posted on Sat, 01 Aug 2026 17:00:22 +0000 by saras

C++ Algorithm Solutions for Competitive Programming Challenges

1. Gymnastic Team Formation Given the small input constraints, a brute-force approach with backtracking and pruning is suitable. The solution uses depth-first search (DFS) to explore valid permutations while eliminating invalid paths early. #include <iostream> using namespace std; int constraints[11] = {0}; bool used[11] = {false}; int v ...

Posted on Fri, 31 Jul 2026 16:00:17 +0000 by rlalande

Generating All Permutations of a String

Recursive Permutation Generation This approach generates all permutations through recursive swaps. The algorithm fixes each character at the first position and recursively permutes the remaining substring. public class PermutationGenerator { public static void main(String[] args) { String text = "abc"; permute( ...

Posted on Wed, 29 Jul 2026 16:44:08 +0000 by abie10