Found 26504 Articles for Server Side Programming

Maximum sum of a path in a Right Number Triangle in C++

Narendra Kumar
Updated on 30-Jan-2020 12:18:47

233 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

Maximum sum by adding numbers with same number of set bits in C++

Narendra Kumar
Updated on 30-Jan-2020 12:15:06

301 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

Maximum sum after repeatedly dividing N by a divisor in C++

Narendra Kumar
Updated on 03-Jun-2020 08:05:39

145 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

Maximum subset with bitwise OR equal to k in C++

Narendra Kumar
Updated on 27-Feb-2020 05:39:05

481 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

Maximum subarray sum modulo m in C++

Narendra Kumar
Updated on 03-Jun-2020 08:06:42

476 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

Maximum subarray sum in O(n) using prefix sum in C++

Narendra Kumar
Updated on 30-Jan-2020 12:05:28

754 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

Maximum subarray sum in array formed by repeating the given array k times in C++

Narendra Kumar
Updated on 03-Jun-2020 08:08:33

197 Views

In this problem, we are given an array and a number k. Our task is to create a program that will find the maximum subarray sum in an array formed by repeating the given array k time in c++.Problem description − Here, we will find the maximum sum of the subarray formed from array formed by repeating the given array k times.Let’s take an example to understand the problem, Input − array = {3, 5, 1} k = 2Output − 18Explanation −array formed by repeating k times, array = {3, 5, 1, 3, 5, 1} Maximum subarray sum = 3+5+1+3+5+1 ... Read More

Maximum subarray sum by flipping signs of at most K array elements in C++

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

450 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 subarray sum by flipping signs of at most k array elements in C++.Code description − Here, we will have to find at most k elements to flip in the array which will make the sum of subarray created from this array maximum.Let’s take an example to understand the problem, Input − array = {1, -2, 7, 0} k = 2Output − 10Explanation − we will flip only one element which is -2, it makes the array ... Read More

Maximum Subarray Sum after inverting at most two elements in C++

Narendra Kumar
Updated on 03-Jun-2020 08:13:04

288 Views

In this problem, we are given an array. Our task is to create a program that will find the maximum subarray sum after inverting at most two elements in C++.Problem description − Here, we can have to find the subarray that will produce the maximum sum on inverting the sign of any two numbers of the array.Let’s take an example to understand the problem, Input − array = {-5, 1, 3, 8, -2, 4, 7}Output − 30Explanation − we will consider elements from index 0 to 6 and invert values -5 and -2 to get the array with max sum.To ... Read More

Maximum steps to transform 0 to X with bitwise AND in C++

Narendra Kumar
Updated on 03-Jun-2020 08:14:40

158 Views

In this problem, we are given an integer X. Our task is to find the total number of steps that are taken to transform from 0 to X.Valid transformation − One step is counted when one transformation takes place from A to B. The condition for the transform to take place is A != B and A & B = A (& is bitwise AND). So, 1 step is transforming from A to B and we have to create a program that will count the maximum number of steps to transform 0 to X.Let’s take an example to understand the ... Read More

Advertisements