# Recursion

### Print all ways to reach the Nth stair with the jump of 1 or 2 units at a time

Given a positive integer N representing N stairs and a person it at the first stair, the task is to…

Read More

### Minimize sum of node values by filling given empty Tree such that each node is GCD of its children

#include using namespace std;  class Node {public:    int val;    Node *left, *right;    Node(int val)    {        this->val = val;        left = NULL;        right = NULL;    }};  class Tree {public:    unordered_map depth;              int findDepth(Node*…

Read More

### Maximum sum of values of N items in 0-1 Knapsack by reducing weight of at most K items in half

import java.io.*;import java.util.*;  class GFG {          static int maximum(int value[],                       int weight[], int weight1,                       int flag, int K, int index)    {                  if (index >= value.length) {              return…

Read More

### Count of valid arrays of size P with elements in range [1, N] having duplicates at least M distance apart

#include using namespace std;  int calculate(int position, int used, int unused, int P,              int M, vector& dp){        if (position == P) {                        return unused…

Read More

### Minimize the count of characters to be added or removed to make String repetition of same substring

function getMin(x, y, z) {            return Math.min(Math.min(x, y), z);        }                                function editDistance(str1, str2, m, n)        {                                 let dp = new Array(m + 1).fill(new Array(n +…

Read More

### Maximum sum of K elements selected from a Matrix

#include using namespace std;int maximumSum(vector& prefixSum,               vector& dp,               int K, int N, int rem, int id){        int ans = INT_MIN;        if (rem == 0)        return…

Read More

### Sum of all nodes with smaller values at a distance K from a given node in a BST

#include using namespace std;  struct TreeNode {      int data;    TreeNode* left;    TreeNode* right;          TreeNode(int data)    {        this->data = data;        this->left = NULL;        this->right = NULL;    }};  void kDistanceDownSum(TreeNode* root,                      int k, int&…

Read More

### Maximum points by traversing from top left of Matrix to bottom right by two persons

#include using namespace std;  const static int MAXR = 20, MAXC = 20;int cache[MAXC][MAXR][MAXC][MAXR],    dp[MAXC][MAXR][MAXC][MAXR];int n, m;vector grid;  int maxMoney(int x1, int y1,…

Read More

### Print all the paths from root to leaf, with a specified sum in Binary tree

import java.util.*;  class GFG {      static List result        = new ArrayList();          static class Node {        int data;        Node left, right;    };          static Node newNode(int data)    {        Node temp = new…

Read More

### Difference between the shortest and second shortest path in an Unweighted Bidirectional Graph

#include using namespace std;  void get_edges(int s, vector& edges, vector p){    if (s == -1)        return;    get_edges(p[s], edges, p);    edges.push_back(s);}  void dist_helper(vector graph, vector& d,                 int v1,…

Read More