# Category Archives: Backtracking

## Count all possible paths between two vertices

Count the total number of ways or paths that exist between two vertices in a directed graph. These paths doesn’t contain a cycle, the simple… Read More »

## Backtracking | Set 9 (Magnet Puzzle)

The puzzle game Magnets involves placing a set of domino-shaped magnets (or electrets or other polarized objects) in a subset of slots on a board… Read More »

## Check if a given string is sum-string

Given a string of digits, determine whether it is a ‘sum-string’. A string S is called a sum-string if a rightmost substring can be written… Read More »

## Combinational Sum

Given an array of positive integers arr[] and a sum x, find all unique combinations in arr[] where the sum is equal to x. The… Read More »

## Combinations where every element appears twice and distance between appearances is equal to the value

Given a positive number n, we need to find all the combinations of 2*n elements such that every element from 1 to n appears exactly… Read More »

## Top 20 Backtracking Algorithm Interview Questions

N Queens Problem Warnsdorff’s Algorithm Word Break Problem Remove Invalid Parenthesis Match a pattern and string using regular expression Find Path from corner cell to… Read More »

## Warnsdorff’s algorithm for Knight’s tour problem

Problem : A knight is placed on the first block of an empty board and, moving according to the rules of chess, must visit each… Read More »

## Printing all solutions in N-Queen Problem

The N Queen is the problem of placing N chess queens on an N×N chessboard so that no two queens attack each other. For example,… Read More »

## Print all palindromic partitions of a string

Given a string s, partition s such that every string of the partition is a palindrome. Return all possible palindrome partitioning of s. Example :… Read More »

## Word Break Problem using Backtracking

Given a valid sentence without any spaces between the words and a dictionary of valid English words, find all possible ways to break the sentence… Read More »

## Partition of a set into K subsets with equal sum

Given an integer array of N elements, the task is to divide this array into K non-empty subsets such that the sum of elements in… Read More »

## Remove Invalid Parentheses

An expression will be given which can contain open and close parentheses and optionally some characters, No other operator will be there in string. We… Read More »

## Find shortest safe route in a path with landmines

Given a path in the form of a rectangular matrix having few landmines arbitrarily placed (marked as 0), calculate length of the shortest safe route… Read More »

## Longest Possible Route in a Matrix with Hurdles

Given an M x N matrix, with a few hurdles arbitrarily placed, calculate the length of longest possible route possible from source to destination within… Read More »

## Match a pattern and String without using regular expressions

Given a string, find out if string follows a given pattern or not without using any regular expressions. Examples: Input: string – GraphTreesGraph pattern -… Read More »