Saturday, 4 Dec 2021
Category: prefix-sum

Given an array arr[] of size N and an integer X, the task is to find the length of the longest subsequence such that the prefix sum at every element of the subsequence remains greater than zero.Example:Input: arr[] = {-2, -1, 1, 2, -2}, N = 5Output: 3Explanation: The sequence can be made of elements […]

#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 = 1;          for (int i = 0; i

Given an array, arr[] and a positive integer K. The task is to find the position say i of the element in arr[] such that prefix sum till i-1, i and suffix sum till i+1 are in Geometric Progression with common ratio K. Examples:Input: arr[] = { 5, 1, 4, 20, 6, 15, 9, 10 }, […]

#include using namespace std;void find_prefixsum(int M, int N, vector& arr,                    vector& dp){        for (int i = 0; i

const MAX = 100005;                        function isPerfect(N) {                        let sum = 1;                        for (let i = 2; i * i

Minimize hamming distance in Binary String by setting only one K size substring bits Given two binary strings S and T of length N and a positive integer K. Initially, all characters of T are ‘0’. The task is to find the minimum Hamming distance after choosing a substring of size K and making all […]

Given an array arr[] consisting of  N integers, the task for every ith element of the array is to find the number of non co-prime pairs from the range [1, arr[i]].Examples:Input: N = 2, arr[] = {3, 4}Output: 2 4Explanation:All non-co-prime pairs from the range [1, 3] are (2, 2) and (3, 3).All non-co-prime pairs from […]

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

#include using namespace std;  int findIndex(int arr[], int N){            int maxValue = INT_MIN;          for (int i = 0; i < N; i++) {        maxValue = max(maxValue, arr[i]);    }          map St;          for (int i = 1; i

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