C++ Articles - Page 474 of 719

Maximum spiral sum in Binary Tree in C++

Narendra Kumar
Updated on 03-Jun-2020 08:17:12

184 Views

In this problem, we are given a binary tree. Our task is to create a program that will find the maximum spiral sum in a binary tree in C++.Spiral sum of a binary tree is the sum of nodes that are encountered in the spiral traversal of the binary tree.In the spiral traversal of a tree, the nodes are traversed from the root to the leaf node. The traversal takes place from left to right then for the next level from right to left and so on for the further levels.Example −Output −5Explanation −We will consider the spiral traversal until the ... Read More

Maximum size subset with given sum in C++

Narendra Kumar
Updated on 21-Jan-2020 11:30:13

784 Views

Problem statementGiven an array of N elements and sum. We need to find size of maximum size subset whose sum is equal to given sumExampleIf input array is arr = { 2, 3, 5, 10 } and sum = 20 then output will be 4 as −2 + 3 + 5 + 10 = 20 which is equal to given sumAlgorithmWe can use dynamic programming to solve this problem.To count the maximal subset, we use another DP array (called as ‘count array’) where count[i][j] is maximal of.count[i][j-1]. Here current element is not considered.scount[i- X][j-1] + 1. Here X is value ... Read More

Maximum segment value after putting k breakpoints in a number in C++

Narendra Kumar
Updated on 03-Jun-2020 08:19:11

275 Views

In this problem, we are given a string that denotes a large number and an integer k roar denotes the number of breakpoints. Our task is to create a program that will find the maximum segment value after putting L breakpoints in a number.Here, we have to find the maximum number that can be generated after putting k breakpoint in the number given by the string.Let's take an example to understand the problemInput − string = “45972”, k = 3Output − 97Explanation −All possible number is: 45    9    7    2 4    59    7    2 ... Read More

Maximum removal from array when removal time >= waiting time in C++

Narendra Kumar
Updated on 03-Jun-2020 08:20:38

134 Views

In this problem, we are given an array of N elements. Our task is to find the maximum removal from the array when removal time >= waiting time.So, here we will be removing the elements of the array. The value of the element of the array denotes the removal time(time taken to remove the element from the array).The element has a waiting time which is the time it will have to wait till it will get removed.The element can be removed from the only if the removal time is greater than the time it has to wait.We have to find ... Read More

Maximum profit from sale of wines in C++

Narendra Kumar
Updated on 21-Jan-2020 11:17:53

218 Views

Problem statementGiven n wines in a row, with integers denoting the cost of each wine respectively. Each year you can sale the first or the last wine in the row. The price of wines increases over time. Let the initial profits from the wines be P1, P2, P3…Pn. On the Yth year, the profit from the ith wine will be Y*Pi. For each year, your task is to print start or end denoting whether first or last wine should be sold. Also, calculate the maximum profit from all the wines.ExampleIf wine prices are {2, 4, 6, 2, 5} then output ... Read More

Maximum Primes whose sum is equal to given N in C++

Narendra Kumar
Updated on 03-Jun-2020 08:21:44

196 Views

In this problem, we are given a number n. Our task is to find the maximum count of primes whose sum is equal to given N.Here, we will find the maximum number of prime numbers that when added will be equal to the number.The prime number are those number which can be divide by either themselves or one.let's take an example to understand the problem −Input − N = 9Output − 4Explanation −9 can be repressed as the sum of prime numbers in the following ways: 2, 2, 2, 3 3, 3, 3 2, 2, 5 2, 7 Out of ... Read More

Maximum prefix-sum for a given range in C++

Narendra Kumar
Updated on 21-Jan-2020 11:10:19

736 Views

Problem statementGiven an array of n integers and q queries, each query having a range from l to r. Find the maximum prefix-sum for the range l – r.ExampleIf input array is arr[] = {-1, 2, 3, -5} and queries = 2 and ranges are: l = 0, r = 3 l = 1, r = 3 then output will be 4 and 5.The range (0, 3) in the 1st query has [-1, 2, 3, -5], since it is prefix, we have to start from -1. Hence, the max prefix sum will be -1 + 2 + 3 = 4The ... Read More

Maximum possible XOR of every element in an array with another array in C++

Narendra Kumar
Updated on 03-Jun-2020 08:23:31

418 Views

In this problem, we are given two arrays A and B of n elements each. Our task is to create a program to find the maximum possible XOR of every element in an array with another array.We have to compute the maximum XOR for each element of array A with array B i.e. for each element of array A we will select an element in array B which will have the maximum XOR value.Let's take an example to understand the problem −Input −array A = {3, 6 ,11, 9} array B = {8, 2, 4, 1}Output −11 14 15 13Explanation−Let’s ... Read More

Maximum Perimeter Triangle from array in C++

Narendra Kumar
Updated on 21-Jan-2020 10:58:36

321 Views

Problem statementGiven an Array of non-negative integers. Find out three elements from the array which form a triangle of maximum perimeterExampleIf input array is {5, 1, 3, 5, 7, 4} then maximum perimeter is (7 + 5 + 5) = 17AlgorithmSort the array in non-increasing order. So, the first element will be maximum and the last will be minimumIf the first 3 elements of this sorted array forms a triangle, then it will be the maximum perimeter triangleExample Live Demo#include using namespace std; int getMaxPerimeter(int *arr, int n) {    sort(arr, arr + n, greater());    int maxPerimeter = 0; ... Read More

Maximum path sum in matrix in C++

Narendra Kumar
Updated on 03-Jun-2020 08:25:46

794 Views

In this problem, we are given a 2D matrix of size M*N. Our task is to create a program that will find the maximum path sum in the matrix.Here, the maximum path sum in the matrix is defined as the sum of all elements for one row to the last row. The allowed moves for traversing the path are downward move and diagonal move. The start and endpoints can be any element of the first and last row of the matrix respectively.Let's take an example to understand the problemInput −matrix [][] =    3 5 9    1 7 2 ... Read More

Advertisements