Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Articles - Page 461 of 719
219 Views
In this problem, we are given a binary tree. Our task is to create a program that will find the maximum sum of non-leaf nodes among all levels of the given binary tree in c++.Problem description − we will calculate the sum of all non-leaf nodes of the tree and every level and then print the maximum sum.Let’s take an example to understand the problem, Input −Output − 9Explanation − the sum of non-leaf nodes at each level −Level 1: 4 Level 2: 1+2 = 3 Level 3: 9 (4, 7 are leaf nodes) Level 4: 0To solve this problem, ... Read More
248 Views
In this problem, we are given an array and an integer k. Our task is to create a program that will find the maximum sum of lengths of non-overlapping subarrays with k as the max element in c++.Problem description − here, we have an array and an integer k. We have to find all possible non-overlapping sub-arrays that can be created from this array. And sum the lengths of all the subarrays created.Let’s take an example to understand the problem, Input − array = {3, 7, 1, 2, 3, 1, 6, 3, 2, 5} k = 3Output − 7Explanation − ... Read More
505 Views
In this problem, we are given an array arr. Our task is to create a program that will find the maximum sum of i*arr[i] among all rotations of the given array in C++.Program description − Here, we will find the maximum sum of out the sums of all the elements of the array multiplied by their index {i * arr[i]} in the rotation.Let’s take an example to understand the problem, Input − array arr = {4, 8, 1, 5}Output − 37Explanation −All rotations with the sum of i*arr[i] : {4, 8, 1, 5} = 4*0 + 8*1 + 1*2 + ... Read More
542 Views
In this problem, we are given a matrix. Our task is to create a program that finds the maximum sum of the hourglass in a matrix in C++.Program description − Here, we will find the maximum sum of all hourglasses that can be created for the given matrix elements.Hour glass is a 7 element shape made in the matrix in the following form, X X X X X X XLet’s take an example to understand the problem, Input −array ={ {2 4 0 0} {0 1 1 0} {4 2 1 0} {0 3 0 ... Read More
245 Views
Problem statementGiven a right triangle of numbers, find the largest of the sum of numbers that appear on the paths starting from the top towards the base, so that on each path the next number is located directly below or below-and-one-place-to-the-rightExampleIf given input is: 3 4 5 1 10 7 Then maximum sum is 18 as (3 + 5 + 10).AlgorithmThe idea is to find largest sum ending at every cell of last row and return maximum of these sums.We can recursively compute these sums by recursively considering above two cellsSince there are overlapping sub-problems, we use dynamic programming to ... Read More
308 Views
Problem statementGiven an array of N numbers, the task is to find the maximum sum that can be obtained by adding numbers with the same number of set bitsExampleIf input array is {2, 5, 8, 9, 10, 7} then output would be 14 −Number of set bits in 2 is 1Number of set bits in 5 is 2Number of set bits in 8 is 1Number of set bits in 9 is 2Number of set bits in 10 is 2Number of set bits in 7 is 3Then sum of (5 + 9 + 10) is 24 whose number of set bits ... Read More
153 Views
In this problem, we are given an integer N. Our task is to create a program that will find the maximum sum after repeatedly dividing N by a divisor in C++.Program description − we will divide the number N recursively until it becomes one and then sum up all the divisors and find the maximum of all divisors.Let’s take an example to understand the problem, Input − N = 12Output − 22Explanation − let’s divide the number recursively and find the sum.Division 1: 12/2 = 6 Division 2: 6/2 = 3 Division 3: 3/3 = 1 Sum = 12+6+3+1 = ... Read More
489 Views
Problem statementGiven an array of non-negative integers and an integer k, find the subset of maximum length with bitwise OR equal to k.ExampleIf given input array is = [1, 4, 2] and k = 3 then output is: [1, 2] The bitwise OR of 1 and 2 equals 3. It is not possible to obtain a subset of length greater than 2.AlgorithmBelow are the properties of bitwise OR −0 OR 0 = 0 1 OR 0 = 1 1 OR 1 = 1for all the positions in the binary representation of k with the bit equal to 0, the corresponding ... Read More
489 Views
In this problem, we are given an array of size n and an integer m. our task is to create a program that will find the maximum subarray sum modulo m in C++.Program description − Here, we will find the maximum value obtained by dividing the sum of all elements of subarray divided by m.Let’s take an example to understand the problem, Input − array = {4, 9 ,2} m = 6Output − 5Explanation − All subarrays and their remainders on dividing{4}: 4%6 = 4 {9}: 9%6 = 3 {2}: 2%6 = 2 {4, 9}: 13%6 = 1 {9, 2}: ... Read More
764 Views
Problem statementGiven an Array of Positive and Negative Integers, find out the Maximum Subarray Sum in that ArrayExampleIf input array is − {-12, -5, 4, -1, -7, 1, 8, -3} then output is 9AlgorithmCalculate the prefix sum of the input array.Initialize− min_prefix_sum = 0, res = -infiniteMaintain a loop for i = 0 to n. (n is the size of the input array).cand = prefix_sum[i] – miniIf cand is greater than res (maximum subarray sum so far), then update res by cand.If prefix_sum[i] is less than min_prefix_sum (minimum prefix sum so far), then update min_prefix_sum by prefix_sum[i].Return resExample Live Demo#include ... Read More