Saturday, 4 Dec 2021
Category: Memoization

Given 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 […]

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

import java.io.*;import java.util.*;class GFG {            static HashMap memo;                public static int dfs(int[] V,                          int left, int K)    {                        int q = 0;                                String key = Arrays.toString(V);        key += Integer.toString(left);                if (memo.containsKey(key))            return memo.get(key);                else if (left == 0) {                        for (int i = 1; i < K; ++i)                if (V[i] > 0) {                                        V[i]–;                                                            q = Math.max(                        q, 1 + dfs(V, K – i, K));                                        V[i]++;                }        }                        else {            for (int i = 1; […]

Length of longest subset consisting of A 0s and B 1s from an array of strings | Set 2Given an array arr[] consisting of N binary strings, and two integers A and B, the task is to find the length of the longest subset consisting of at most A 0s and B 1s.Examples:Input: arr[] = […]