# Category Archives: Backtracking

## Print all possible K-length subsequences of first N natural numbers with sum N

Given two positive integers N and K, the task is to print all possible K-length subsequences from first N natural numbers whose sum of elements… Read More »

## Replace specified matrix elements such that no two adjacent elements are equal

Given a matrix arr[][] of dimensions N * M, consisting of ‘O’ or ‘F’, where ‘O’ denotes obstacles and ‘F’ denotes free spaces, the task… Read More »

## Count subsets having product divisible by K

Given an array arr[] of size N and an integer K, the task is to count the number of subsets from the given array with… Read More »

## Generate all distinct subsequences of array using backtracking

Given an array arr[] consisting of N positive integers, the task is to generate all distinct subsequences of the array. Examples: Input: arr[] = {1,… Read More »

## Lexicographically largest string possible for a given cost of appending characters

Given an integer W and an array a[] of size 26 where ai denotes the cost of using the ith alphabet, the task is to… Read More »

## Generate a combination of minimum coins that sums to a given value

Given an array arr[] of size N representing the available denominations and an integer X. The task is to find any combination of the minimum… Read More »

## Check if K can be obtained by performing arithmetic operations on any permutation of an Array

Given an array arr[] of N integers and an integer K, the task is to check if the expression formed for any permutation of the… Read More »

## Generate all N digit numbers having absolute difference as K between adjacent digits

Given two integers N and K, the task is to generate all positive integers with length N having an absolute difference of adjacent digits equal… Read More »

## Count of permutations of an Array having each element as a multiple or a factor of its index

Given an integer, N, the task is to count the number of ways to generate an array, arr[] of consisting of N integers such that… Read More »

## Generate all possible permutations of a Number divisible by N

Given a numerical string S, the task is to print all the permutations of the string which are divisible by N. Examples: Input: N =… Read More »

## Queries to find the Minimum Weight from a Subtree of atmost D-distant Nodes from Node X

Given an N-ary Tree rooted at 1, and an array val[] consisting of weights assigned to every node, and a matrix Q[][], consisting of queries… Read More »

## N-Queen Problem | Local Search using Hill climbing with random neighbour

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, the… Read More »

## Print all possible ways to write N as sum of two or more positive integers

Given an integer N, the task is to print all the possible ways in which N can be written as the sum of two or… Read More »

## Check if any King is unsafe on the Chessboard or not

Given a matrix board[][] consisting of the characters K or k, Q or q, B or b, N or n, R or r, and P… Read More »

## Split a Numeric String into Fibonacci Sequence

Given a numeric string S representing a large number, the task is to form a Fibonacci Sequence of at least length 3 from the given… Read More »