Found 7197 Articles for C++

Maximum sum of nodes in Binary tree such that no two are adjacent using Dynamic Programming in C++ program

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

470 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++ program

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

176 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

Maximum Sum Increasing Subsequence using Binary Indexed Tree in C++ program

sudhir sharma
Updated on 09-Dec-2020 13:18:16

160 Views

In this problem, we are given an array arr[] of n integers. Our task is to create a program to find the maximum sum increasing subsequence using binary indexed tree in C++.Problem Description − We need to find an increasing subsequence with the maximum sum using the elements of the array.Increasing Subsequence − subsequence in which the value of the current element is greater than the element at the previous position.Binary Indexed Tree − It is a data structure which is a type of tree. We can add or remove elements from the tree in an effective way.Let’s take an ... Read More

Maximum Sum Increasing Subsequence using DP in C++ program

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

237 Views

In this problem, we are given an array arr[] of size n. Our task is to create a program to find the maximum Sum Increasing Subsequence using DP in C++.Problem Description − To find the maximum sum increasing subsequence, we will be creating a subsequence in which the next element is greater than the current element.Let’s take an example to understand the problem, Inputarr[] = {4, 2, 3, 6, 5, 9}Output20ExplanationIncreasing subsequence with maximum sum: {2, 3, 6, 9} = 2 + 3 + 6 + 9 = 20Solution ApproachTo solve the problem using a dynamic Program Approach. We will ... Read More

Maximum sum by picking elements from two arrays in order in C++ Program

sudhir sharma
Updated on 09-Dec-2020 13:08:19

862 Views

In this problem, we are given two arrays arr1[] and arr2[], and two numbers N and M.N gives the number of elements taken from arr1.M gives the number of elements taken from arr2.We need to select one of the elements from arr1[i] to arr2[i], for whichmakes the sum maximum, but maximum N can be taken from arr1 and M from arr2.Our task is to create a program to find the maximum sum by picking elements from two arrays in order in C++.Let’s take an example to understand the problem, Inputarr1[] = {5, 1, 6, 2, 8, 9} arr2[] = {8, ... Read More

Maximum sum alternating subsequence in C++ program

sudhir sharma
Updated on 09-Dec-2020 13:05:53

442 Views

In this problem, we are given an array arr[] of n integers. Our task is to create a program to find the Maximum sum alternating subsequence starting from the first element of the array.An alternating subsequence is a subsequence in which elements are increasing and decreasing in an alternating order i.e. first decreasing, then increasing, then decreasing. Here, the reverse alternating subsequence is not valid for finding the maximum sum.Let’s take an example to understand the problem, Inputarr[] = {5, 1, 6, 2, 4, 8, 9}Output27ExplanationStarting element: 5, decrease: 1, increase: 6, decrease: 2, increase:4, N.A. Here, we can use ... Read More

Maximum subsequence sum such that no three are consecutive in C++ Program

sudhir sharma
Updated on 09-Dec-2020 13:00:51

193 Views

In this problem, we are given an array arr[] consisting of n positive integers. Our task is to create a program to find the maximum subsequence sum such that no three are consecutive.Problem Description − Here, we need to find the sum of sequences created from the array such that there are no three consecutive elements.Consecutive elements of an array are those elements that have followed the same order of index.arr[0], arr[1], arr[2], …Let’s take an example to understand the problem, Inputarr[] = {5, 9, 12, 15}Output32ExplanationSum = 5 + 12 + 15 = 32Solution ApproachA simple solution to the ... Read More

Maximum subarray sum in an array created after repeated concatenation in C++ Program

sudhir sharma
Updated on 09-Dec-2020 12:54:07

209 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 subarray sum in an array created after repeated concatenation.Problem Description − We will find the maximum sum of the subarray is taken from the array which is created after repeating arr, k times.ExampleLet’s take an example to understand the problem.Inputarr[] = {−9, −5, 14, 6} k = 2Output26ExplanationNew array after repeating : {−9, −5, 14, 6, −9, −5, 14, 6} Subarray with maximum sum = {14, 6, −9, −5, 14, 6} Sum = ... Read More

Maximum Subarray Sum Excluding Certain Elements in C++ program

sudhir sharma
Updated on 09-Dec-2020 12:51:34

179 Views

In this problem, we are given two arrays arr1[] of size n and arr2[] of size m. Our task is to create a program to find the maximum subarray sum excluding certain elements.Problem Description − We need to find the maximum subarray sum from elements of the array arr1[] that are not present in arr2[].Let’s take an example to understand the problem, Inputarr1[] = {4, 5, 7, 2, 9}, arr2[] = {1, 9, 2, 7}Output9Explanationarr1 after removal of elements of arr2[] = {4, 5} Both can form a subarray, hence sum = 4+5 = 9.Solution ApproachA simple solution to the ... Read More

Maximum subarray size, such that all subarrays of that size have sum less than k in C++ program

sudhir sharma
Updated on 09-Dec-2020 12:49:26

274 Views

In this problem, we are given an array arr[] consisting of n positive integers and an integer k. Our task is to create a program to find the Maximum subarray size, such that all subarrays of that size have sum less than k.Problem Description − we need to find the largest size of a subarray, such that all subarray created of the size from the elements of the array will have the sum of elements less than or equal to k.Let’s take an example to understand the problem, Inputarr[n] = {4, 1, 3, 2}, k = 9Output3ExplanationAll subarrays of size ... Read More

Advertisements