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
Generating All Permutations of an Array
Given an array nums containing distinct integers, return all possible permutations. You may return the answer in any order.
Example 1:
<strong>Input:</strong> nums = [1,2,3]
<strong>Output:</strong> [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
Example 2:
<strong>Input:</strong> nums = [0,1]
<strong&g ...
Posted on Wed, 29 Jul 2026 16:21:02 +0000 by onicsoft
Generating Combinations with Backtracking
The task is to generate all possible combinations of r distinct numbers from the set {1, 2, ..., n}. A combination is an unordered selection, meaning {1, 2, 3} is the same as {3, 2, 1}. We need to print each combination on a new line, with numbers sorted in ascending order and each number occupying exactly three characters of space. The combina ...
Posted on Thu, 16 Jul 2026 16:27:06 +0000 by NuMan
Mastering Backtracking: Generating Increasing Subsequences and Permutations
This article delves into advanced backtracking techniques for solving common algorithmic problems, specifically focusing on generating increasing subsequences and permutations, including handling duplicates.
Generating Increasing Subsequences (Problem 491)
Given an integer array, the task is to find all increasing subsequences with a length of ...
Posted on Tue, 14 Jul 2026 17:10:59 +0000 by Dominator69
Backtracking Algorithm: Fundamentals, Combinations, and Pruning
Backtracking Algorithm
Understanding Backtracking
Backtracking solves problems by exploring all possible solutions in a systematic way, often represented as a tree structure. The algorithm recursively searches through subsets, where the size of the original set determines the tree's width, and the recursion depth determines its height. Since re ...
Posted on Mon, 13 Jul 2026 17:21:59 +0000 by ashbai
Understanding Recursion Termination Conditions for Binary Tree Path Problems
LeetCode 257: Binary Tree All Paths
Termination Condition Considerations
The statement if (root == nullptr) return; serves multiple critical purposes in recursive tree algorithms:
Primary Function Guard
When placed in the main function provided by LeetCode, this check handles the empty tree case. If an empty tree is passed to the main function, ...
Posted on Sun, 12 Jul 2026 16:49:45 +0000 by knickerlas