Techno Blender
Digitally Yours.
Browsing Tag

# Subsequence

## Removing Subsequence from concatenated Array

Improve Article Save Article Like Article Improve Article Save Article Like Article Given array A formed by the concatenation of string “ABC” one or multiple times. The task for this problem is to remove all occurrences of subsequence “ABC” by performing the following operation a minimum number of times. In one operation swap any two elements of array A. print the number of operations required and elements on which operations are performed in each step.Examples:Input: A = {‘A’, ‘B’, ‘C’}Output: 11 3Explanation:…

## Maximum sum Subsequence with index difference K

Given an array of integers arr and an integer k, we need to find the maximum sum of a subsequence such that the index difference between any two elements of the subsequence is k.Examples:Input: arr = , k = 2Output: 9Explanation: The maximum sum subsequence with index difference 2 is with sum 9Input: arr = and k = 3Output: 16Explanation: The maximum sum subsequence with index difference 3 is with sum 16Approach: The approach is to use a dynamic programming method.Initialize an array dp of size n (where n is the length…

## Longest non-decreasing Subsequence with adjacent differences

Given an array arr of N elements, the task is to find the length of the longest non-decreasing subsequence such that the differences between adjacent elements are non-decreasing. For indices i, j, and k: i < j < k, ai − aj ≤ aj − akExamples:Input: N = 9, arr = Output: 6Explanation: Longest non-decreasing Subsequence is . Here, the subsequence satisfies the given condition, (3 – 1) <= (5 – 3), (4 – 3) <= (5 – 4), (7 – 5) <= (8 – 7), (8 – 7) <= (10 – 8), and (10 – 8) <= (9 – 6). The length of such…

## Min operations to empty an array by erasing any increasing subsequence

Improve Article Save Article Like Article Improve Article Save Article Like Article Given array A of size N, the task is to find the number of operations to empty the array by performing the following operation one or more times. In one operation choose any strictly increasing subsequence and delete it from A.Examples:Input: A = {2, 1, 4, 5, 3}Output: 2Explanation: Following operations are performed to empty the array:Choosing increasing subsequence {2, 4, 5} and removing it from A, A becomes {1, 3}Choosing…

## Convert Array a[] to b[] through Subsequence addition

Given an array a equal = {1} and a target array b, the task is to check whether you can transform a to b or not by performing an operation on array a. The operation is defined as choosing any subsequence of array a and insert the sum of the numbers of the subsequence to the array a at any position.Examples:Input: b = {5, 1, 3, 2, 1}Output: YesExplanation: Initially, a = By choosing the subsequence , and inserting 1 in the array, a changes to By choosing the subsequence , and inserting 1+1 = 2 in the middle of the array, a…

## Longest Subsequence with difference between characters equals to K

Improve Article Save Article Like Article Improve Article Save Article Given a string S consisting of lowercase letters. Find the longest subsequence of S such that the difference between the maximum and minimum occurring characters in the subsequence is exactly K.Examples:Input: S = ‘abcdeg’ and K = 4Output: abcdeInput: S = ‘daaaabbbadddddeeee’, K = 1Output: ddddddeeeeApproach: This can be solved with the following idea:Iterate through all possible minimum and maximum character pairs (which are K characters apart).…

## Find the Second Longest Increasing Subsequence

#include <bits/stdc++.h>using namespace std;  int secondLis(int arr, int n){                  vector<int> dpL(n, 1);                      vector<int> dpC(n, 1);      for (int i = 0; i < n; i++) {        for (int j = 0; j < i; j++) {                                    if (arr <= arr)                continue;              if (dpL + 1 > dpL) {                                                                                                                dpL = dpL + 1;                dpC = dpC;…

## Find length of the longest non-intersecting anagram Subsequence

Improve Article Save Article Like Article Improve Article Save Article Given a string S of length N, find the length of the two longest non-intersecting subsequences in S that are anagrams of each other.Input: S = “aaababcd”Output: 3Explanation: Index of characters in the 2 subsequences are:{0, 1, 3} = {a, a, b} {2, 4, 5} = {a, a, b} The above two subsequences of S are anagrams.Frequency of ‘a’ = 4, so 2 ‘a’s can be used in both the anagrams.Frequency of ‘b’ = 2, so 1 ‘a’ can be used in both the anagrams.Hence 2 + 1…

## Maximum Subsequence sum with difference among consecutive numbers less than K

Given an array arr, find the maximum sum that can be achieved by picking a subsequence such that the difference between consecutive elements in the subsequence is less than or equal to a given number ‘K’.Examples:Input: arr = {1, 8, 9, 4, 6, 7}, K = 2Output: 24. Explanation: The maximum sum can be obtained by taking the subsequence {8, 9, 7}. As 8 + 9 + 7 = 24Input: arr = {1, -2, 3, 14, 6, -17, 16, 25}, K = 5Output: 30.Explanation: The maximum sum can be obtained by taking the subsequence {14, 16}. As 14 + 16 = 30Naive…

## Maximize the sum by choosing a Subsequence

Improve Article Save Article Like Article Improve Article Save Article Given an array X of length N along with an integer K. Then the task is to choose a sub-sequence to maximize the sum by selecting the starting index i (1 ≤ i ≤ N ) and make the subsequence of all the elements, which are at a continuous distance (i + K) till we are not out of the array X or at the last index. Examples:Input: N = 5, K = 2, X = {2, 5, 5, 8, 2}Output: 13Explanation: The operation is performed as: Let select index i = 2 and then A2 = 5…