Category: prefix-sum

Count of non decreasing Arrays with ith element in range [A[i], B[i]]

#include using namespace std;  int totalValidArrays(int a[], int b[],                     int N){        int dp[N + 1][b[N – 1] + 1];          int pref[N + 1][b[N – 1] + 1];          memset(dp, 0, sizeof(dp)),        memset(pref, 0, sizeof(pref));          dp[0][0] = 1;          for (int i = 0; i

Continue Reading

Prefix Factorials of a Prefix Sum Array

Given an array arr[] consisting of N positive integers, the task is to find the prefix factorials of a prefix sum array of the given array i.e.,  .Examples:Input: arr[] = {1, 2, 3, 4}Output: 1 6 720 3628800Explanation:The prefix sum of the given array is {1, 3, 6, 10}. Therefore, prefix factorials of the obtained […]

Continue Reading

Longest Non-Increasing Subsequence in a Binary String

Longest Non-Increasing Subsequence in a Binary StringGiven a binary string S of size N, the task is to find the length of the longest non-increasing subsequence in the given string S.Examples:Input: S = “0101110110100001011”Output: 12 Explanation: The longest non-increasing subsequence is “111111100000”, having length equal to 12.Input: S = 10101Output: 3Approach: The given problem can be […]

Continue Reading