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 ReadingGiven an array arr[] consisting of permutation of the first N natural numbers, the task is to find the expected number of swaps to sort the given array where the probability of swapping any inversion pair is equal and the probability of swapping any non-inversion pair is 0.Examples:Attention reader! Don’t stop learning now. Get hold […]

Continue Reading#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#include using namespace std; int DirX[] = { 2, 1, -1, -2, -2, -1, 1, 2 };int DirY[] = { 1, 2, 2, 1, -1, -2, -2, -1 }; bool isSafe(int i, int j, int n, vector& Board){ return (i >= 0 and j >= 0 and i < n and j < n and Board[i][j] == 0);} bool isPossible = […]

Continue Reading#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#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 ReadingGiven a positive integer N representing N stairs and a person it at the first stair, the task is to print all ways to reach the Nth stair with the jump of 1 or 2 units at a time.Examples:Input: N = 3Output: 112Explanation:Nth stairs can be reached in the following ways with the jumps of 1 […]

Continue Reading#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 Readingimport 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#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