Backtracking problems made easy
Backtracking problems seems to be one of the tricky part while studying DSA. But once we figure out a pattern in it, then its even easier than array problems.
A general code structure of a backtracking problem solution looks like :
void Backtrack(int start, vector<int>&arr, int &ans)
{
//Base case
for(int i=start; i<arr.size(); i++)
{
//include the current element at this position if possible in the ans
Backtrack(start+1) // here take care whethere to pass start or start+1 dending on the usecase
//backtrack by removing current element
}
}
To avoid duplicates in backtracking, we generally sort the array/list, then keep track of the duplicates by arr[i] == arr[i-1].
Here are some good backtracking problems which makes our understanding about the topic more firm :
Read other posts