Category: Recursion

Print all Balanced Brackets Strings that can be formed by replacing wild card ‘?’

import java.io.*;import java.util.*;class Main {            static void print(char ch[])    {        for (Character c : ch) {            System.out.print(c);        }        System.out.println();    }            static boolean check(char ch[])    {                Stack S = new Stack();                        if (ch[0] == ‘)’) {            return false;        }                for (int i = 0; i < ch.length; i++) {                                    if (ch[i] == '(') {                S.add('(');            }                        else {                                                                if (S.size() == 0)                    return false;                                                else                    S.pop();            }        }                        if (S.size() == 0)            return true;                        else            return false;    }            static void count(char ch[], int index)    {                if (index == […]

Continue Reading

Convert given Binary Tree to Symmetric Tree by adding minimum number of nodes

  #include using namespace std;  class Node {public:    int val;    Node *left, *right;    Node(int val)    {        this->val = val;        left = right = NULL;    }};  Node* buidSymmetericTree(Node* root1,                         Node* root2){        if (root1 == NULL and root2 == NULL) {        return NULL;    }          if (root1 == NULL) {                          Node* node = new Node(root2->val);        root1 = node;    }          if (root2 == NULL) {                          Node* node = new Node(root1->val);        root2 = node;    }                  if (root1->val != root2->val) {        int temp = root1->val […]

Continue Reading

Check if all elements of given Linked List corresponds to a downward path from any node in given Binary Tree

  #include “bits/stdc++.h”using namespace std;  struct listNode {    int val;    struct listNode* next;          listNode(int data)    {        this->val = data;        next = NULL;    }};  struct treeNode {    int val;    treeNode* right;    treeNode* left;          treeNode(int data)    {        this->val = data;        this->left = NULL;        this->right = NULL;    }};  bool isPathUtil(listNode* curList,                treeNode* curTree){            if (curList == NULL)        return true;          if (curTree == NULL)        return false;      if (curList->val == curTree->val) {                          return isPathUtil(curList->next,                          curTree->left)               || isPathUtil(curList->next,                             curTree->right);    }    else {                  return false;    }}  bool isPath(listNode* head, treeNode* root){              if (root == NULL)        return false;              if (head == […]

Continue Reading

Find direction of path followed from root by a linked list in a Binary Tree

#include using namespace std;  struct ListNode {    int data;    ListNode* next;    ListNode(int data)    {        this->data = data;        this->next = NULL;    }};  struct TreeNode {    TreeNode* left;    TreeNode* right;    int val;      TreeNode(int x)        : left(NULL), right(NULL), val(x)    {    }};  ListNode* makeList(int arr[], int n){    ListNode* h = NULL;    ListNode* root;    for (int i = 0; i < n; i++) {        int data = arr[i];        ListNode* node = new ListNode(data);          if (h == NULL) {            h = node;            root = h;        }        else {            root->next […]

Continue Reading

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* cur)    {        int mx = 0;        if (cur->left) {            mx = findDepth(cur->left);        }        if (cur->right) {            mx = max(mx, findDepth(cur->right));        }                          return depth[cur] = mx + 1;    }              int dfs(Node* cur, bool flag, int parValue)    {        if (parValue != -1) {            if (flag)                cur->val = parValue;            else                cur->val = parValue * 2;        }        int […]

Continue Reading

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 0;        }                          if (flag == K) {                          int skip = maximum(value,                               weight, weight1,                               flag, K, index + 1);              int full = 0;                                                              if (weight[index]

Continue Reading

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 == 0 ? 1 : 0;    }              if (dp[position][used][unused] != -1)        return dp[position][used][unused];          int result = 0;              if (unused > 0) {                        result += calculate(position + 1, used + 1,                            unused – 1, P, M, dp)                  * unused;    }              if (used > M) {                        result += […]

Continue Reading