Wednesday, 1 Dec 2021
Category: Combinatorial

Given an array vec[] of size N of non-negative integers. The task is to count the number of subsequences with the sum equal to S – 2 where S is the sum of all the elements of the array.Examples:Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA […]

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 == ‘)’) {            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 == […]

#include #include #include #include using namespace std;  struct nodeInfo {    int sat;     int deg;     int vertex; };struct maxSat {    bool operator()(const nodeInfo& lhs,                    const nodeInfo& rhs) const    {                                        return tie(lhs.sat, lhs.deg, lhs.vertex)               > tie(rhs.sat, rhs.deg, rhs.vertex);    }};  class Graph {          int n;          vector adj;  public:        Graph(int numNodes)    {        n = numNodes;        adj.resize(n, vector());    }    ~Graph() { adj.clear(); }          void addEdge(int u, int v);              void DSatur();};  void Graph::addEdge(int u, int v){    adj[u].push_back(v);    adj[v].push_back(u);}  void Graph::DSatur(){    int u, i;    vector used(n, false);    vector c(n), […]

#include using namespace std;int possibleCombinations(int arr1[], int arr2[], int N){        unordered_map mp;                    for (int i = 0; i < N; i++) {        mp[arr1[i]] = arr2[i];    }            int count = 1;            vector visited(N + 1, 0);    for (int i = 1; i

Given three integers a, b and c which denotes the frequencies of three different characters ‘A‘, ‘B‘, and ‘C‘ respectively, and can be used to form strings of length 3. The task is to count the total number of possible combinations of A, B and C such that it forms a string having at least […]

Given two integers N and K, the task is to find the count of binary strings of at most N length that can be formed such that the count of consecutive 1‘s is always a multiple of K.Example:Input: N = 3, K = 2Output: 6Explanation: Binary strings of atmost N length containing consecutive 1’s as […]

Given a string S of N characters, the task is to calculate the total number of non-empty substrings such that at most one character occurs an odd number of times.Example: Input: S = “aba”Output: 4Explanation: The valid substrings are “a”, “b”, “a”, and “aba”. Therefore, the total number of required substrings are 4.Input: “aabb”Output: 9Explanation: The […]

Given two arrays, arr[ ] C of containers and arr[ ] B of balls, the task is to find if it’s possible to completely fill every container with the given balls, if each container can only store balls of the same type. In array C, C[i] stores the maximum number of balls that the i-th […]

#include using namespace std;  int dp, choose;  int calc(int l, int r, string& s){          if (abs(r – l) % 2 == 0) {        return 0;    }      if (l > r) {        return dp[l][r] = 1;    }          if (dp[l][r] != -1) {        return dp[l][r];    }          if ((r – l) == 1) {        if (s[l] == s[r]) {            dp[l][r] = 1;        }        else {            dp[l][r] = 0;        }        return dp[l][r];    }          int ans = 0;    for (int k = […]

#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 += […]