Maximum Sum Subsequence with At Least K Distant Elements in C++ Program

sudhir sharma
Updated on 09-Dec-2020 13:43:39

327 Views

In this problem, we are given an array arr[] of size n and a number k. Our task is to create a program to find the maximum sum subsequence with atleast k distant elements.Problem Description − We need to find the sum of subarrays such that the elements of the subarray are taken from the array whose index is at k distance and the sum is maximized.Let’s take an example to understand the problem, Inputarr[] = {2, 3, 7, 9, 2, 8, 3}Output15ExplanationAll subsequences that satisfy the given condition, {2, 9, 3}, Sum = 14 {3, 2}, Sum = 5 ... Read More

Maximum Sum Subarray with Same Start and End Values in C++

sudhir sharma
Updated on 09-Dec-2020 13:41:11

706 Views

In this problem, we are given an array arr[] of size n consisting of positive values. Our task is to create a program to find the maximum sum subarray such that start and end values are the same.Problem Description − Here, we need to find a subarray such that the elements at index i (starting index of subarray) and j (ending index of subarray) are the same i.e. arr[i] = arr[j]. And the sum of elements of the subarray is maximized.Let’s take an example to understand the problem, Inputarr[] = {2, 1, 3, 5, 6, 2, 4, 3}Output23ExplanationAll subarrays which ... Read More

Maximum Sum Possible for a Subsequence in C++

sudhir sharma
Updated on 09-Dec-2020 13:38:21

332 Views

In this problem, we are given an array arr[] of size n and an integer k. Our task is to create a program to find the maximum sum possible for a subsequence such that no two elements appear at a distance < K in the array.Problem Description − We need to find the maximum sum of sub−seqeunce that considers elements that are k distance from each other.Let’s take an example to understand the problem, Inputarr[] = {6, 2, 5, 1, 9, 11, 4} k = 2Output16ExplanationAll possible sub−sequences of elements that differ by k or more. {6, 1, 4}, sum ... Read More

Maximum Sum Path in a Matrix from Top to Bottom and Back in C++

sudhir sharma
Updated on 09-Dec-2020 13:36:20

338 Views

In this problem, we are given a matrix mat[][] of size nXm. Our task is to create a program to find the maximum sum path in a matrix from top to bottom and back.Problem Description − We need to find the maximum path sum from topleft to bottom−right and then back.Valid movesFrom mat[0][0] to mat[n−1][m−1]: Right (mat[i][j] to mat[i][j+1]) and Down (mat[i][j] to mat[i+1][j]). From mat[n−1][m−1] to mat[0][0]: left (mat[i][j] to mat[i][j−1]) and up (mat[i][j] to mat[i−1][j]).One important thing is that both paths cannot be the same. There should be one or more elements different in both paths.Let’s take an ... Read More

Maximum Sum of Products of Two Arrays in C++

sudhir sharma
Updated on 09-Dec-2020 13:32:47

280 Views

In this problem, we are given two arrays arr1[] and arr2[] of size n. Our task is to create a program to find the maximum Sum of Products of Two Array.Problem Description − We need to find the maximum sum of products of two arrays. We need to find the maximum sum of the product of one element from arr1 and other elements from arr2.Let’s take an example to understand the problem, Inputarr1[] = {3, 5, 6} arr2[] = {1, 4, 2}Output37ExplanationMaximum sum of products: 6*4 + 5*2 + 3*1 = 24 + 10 + 3 = 37Solution ApproachA simple ... Read More

Maximum Sum of Smallest and Second Smallest in an Array in C++

sudhir sharma
Updated on 09-Dec-2020 13:30:46

542 Views

In this problem, we are given an array arr[]. Our task is to create a program to find the maximum sum of smallest and second smallest in an array.Problem Description − We need to find the sum of the smallest and second smallest elements of a sum subarray of the array. And return the maximum of all such subarray sums.ExampleLet’s take an example to understand the problem, Inputarr[] = {3, 5, 4, 2, 9, 1, 6}Output11ExplanationTheir out of all subarrays possible, {2, 9} has a maximum sum of smallest elements. Sum = 2 + 9 = 11Solution ApproachA simple solution ... Read More

Maximum Sum of Pairwise Product in an Array with Negatives in C++

sudhir sharma
Updated on 09-Dec-2020 13:29:22

491 Views

In this problem, we are given an array arr[] that contains n integers values (negative values allowed). Our task is to create a program to find the maximum sum of pairwise products in an array with negative allowed.Problem Description − We need to create pairs using the elements of the array such that the sum of the product of elements of pairs is maximum.Let’s take an example to understand the problem, Inputarr[] = {−5, 2, 3, 7, −1, 1, −3, 12}Output104ExplanationThe pairs to be considered: (−5, −3), (2, 3), (−1, 1), (7, 12) Sum of product = (−5 * −3) ... Read More

Maximum Sum of Pairs with Specific Difference in C++

sudhir sharma
Updated on 09-Dec-2020 13:25:11

255 Views

In this problem, we are given an array arr[] of n integers and a number d. Our task is to create a program to find the maximum sum of pairs with specific difference in c++.Problem Description − We will find pairs in such a way that the difference of elements of pairs is less than d. The sum of all such pairs should be maximum.Let’s take an example to understand the problem, Inputarr[] = {5, 9, 11, 7, 2, 12, 3} d = 5Output47ExplanationPairs that contribute to maximum sum: (3, 5), (7, 9), (11, 12). Sum = 3 + 5 ... Read More

Maximum Sum of Nodes in Binary Tree with No Two Adjacent Using Dynamic Programming in C++

sudhir sharma
Updated on 09-Dec-2020 13:23:31

496 Views

In this problem, we are given a Binary Tree with each node having a value. Our task is to create a program to find the Maximum sum of nodes in Binary tree such that no two are adjacent using Dynamic Programming.Problem Description − We will be choosing the subsets of the binary tree to make the sum maximum in such a way that the nodes are not connected directly.Let’s take an example to understand the problem, InputOutput24ExplanationElements to be taken under consideration are: 8 + 5 + 9 + 2 = 24Solution ApproachA solution to the problem is by using ... Read More

Maximum Sum of Increasing Order Elements from N Arrays in C++

sudhir sharma
Updated on 09-Dec-2020 13:20:14

204 Views

In this problem, we are given a 2-D matrix of size nXm. Our task is to create a program to find the maximum sum of increasing order elements from n arrays.Program Description − Here, we need to find the maximum sum of elements by taking one element from each row in such a way that the element from ith row is less than the element from (i+1)th row. And so on. If no such sum is possible, return −1 denoting no result possible.Let’s take an example to understand the problem, Inputmat[][] = {    {4, 5, 1, 3, 6},   ... Read More

Advertisements