Maximum Sum in a 2 x N Grid with No Adjacent Elements in C++

Ayush Gupta
Updated on 15-Oct-2020 12:32:24

366 Views

In this problem, we are given a rectangular grid of size 2 x n. Our task is to create a program to find the Maximum sum in a 2 x n grid such that no two elements are adjacent in C++.Problem DescriptionTo find the maximum sum, we cannot select elements that are adjacent to the current element, vertically, horizontally or diagonally.Let’s take an example to understand the problem, InputrectGrid[2][] =389 411Output13Explanationall possible sums areIf we start from rectGrid[0][0] i.e. 3, then we can add only 9 or 1. The maxSum is 12.If we start from rectGrid[1][0] i.e. 4, then we ... Read More

Maximum Sum in Circular Array with No Adjacent Elements in C++

Ayush Gupta
Updated on 15-Oct-2020 12:30:13

354 Views

In this problem, we are given a circular array cirArr[]. Our task is to create a program to find the Maximum sum in circular array such that no two elements are adjacent in C++.Problem DescriptionFor the circular array, we need to find the maximum sum sum of elements of the array such that adjacent elements cannot be taken i.e. we need to take alternate elements.Circular Array is a special type of array in which the last element of the array is connected to the first element.Let’s take an example to understand the problem, InputcirArr[] = {4, 1, 5, 3, 2}Output9ExplanationThe ... Read More

Maximum Sum Increasing Subsequence in C++

Ayush Gupta
Updated on 15-Oct-2020 12:26:32

168 Views

In this problem, we are given an array arr[] of N integers and two index values x and y. Our task is to create a program to find the Maximum sum increasing subsequence from a prefix and a given element after prefix is must in C++.Problem DescriptionWe will find the maximum sum of increasing sequence till index x and including the element at index y.Let’s take an example to understand the problem, Inputarr[] = {1, 5, 9, 131, 6, 100, 11, 215}, x = 4, y = 6Output26ExplanationWe will take the subsequence till index 3 and then at last include ... Read More

Maximum Sum Increasing Subsequence Using Binary Indexed Tree in C++

Ayush Gupta
Updated on 15-Oct-2020 12:21:46

145 Views

In this problem, we are given an array arr[] of N elements. Our task is to create a program to find the maximum Sum Increasing Subsequence using Binary Indexed Tree in C++.Let’s take an example to understand the problem, Inputarr[] = {4, 1, 9, 2, 3, 7}Output13ExplanationMaximum increasing subsequence is 1, 2, 3, 7. Sum = 13Solution ApproachTo solve the problem, we will use the Binary Indexed Tree in which we will insert values and map them to binary indexed tree. Then find the maximum value.ExampleProgram to illustrate the working of our solution,  Live Demo#include using namespace std; int ... Read More

Maximum Sum of Absolute Difference of Any Permutation in C++

Ayush Gupta
Updated on 15-Oct-2020 12:19:26

596 Views

In this problem, we are given an array. Our task is to create a program to find the Maximum sum of absolute difference of any permutation in C++.Problem DescriptionWe will be finding all permutation of the elements of the given array. And then finding the sum of the absolute difference of adjacent elements of the array. Lastly we will return the maximum of all sums.Let’s take an example to understand the problem, Inputarr[] = {9, 1, 6, 3}Output17ExplanationAll permutations of the array with sum of absolute difference of adjacent elements. {9, 1, 6, 3}, sum= |9-1| + |1-6| + |6-3| ... Read More

Maximum Sum of Difference of Adjacent Elements in C++

Ayush Gupta
Updated on 15-Oct-2020 12:17:54

310 Views

In this problem, we are given a number N. Our task is to create a program to find the Maximum sum of difference of adjacent elements in C++.Problem DescriptionWe will find the Maximum sum of the absolute difference between the adjacent elements of all permutation arrays.Let’s take an example to understand the problem, InputN = 4Output7ExplanationAll permutations of size 4 are : {1, 2, 3, 4} = 1 + 1 + 1 = 3 {1, 2, 4, 3} = 1 + 2 + 1 = 4 {1, 3, 2, 4} = 2 + 1 + 2 = 5 {1, 3, ... Read More

Maximum Sum of Distinct Numbers with LCM Equal to N in C++

Ayush Gupta
Updated on 15-Oct-2020 12:16:27

189 Views

In this problem, we are a number N. Our task is to create a program to find the Maximum sum of distinct numbers such that LCM of these numbers is N in C++.Problem DescriptionWe need to find the sum of all factors of the number N. And add all distinct to find the maximum sum.Let’s take an example to understand the problem, InputN = 12Output28ExplanationAll distinct factors of N are 1, 2, 3, 4, 6, 12. Sum = 1 + 2 + 3 + 4 + 6 + 12 = 28Solution ApproachA simple solution will be finding all the factors ... Read More

Maximum Sum of Elements from Each Row in the Matrix in C++

Ayush Gupta
Updated on 15-Oct-2020 12:11:37

905 Views

In this problem, we are given a two matrix mat[][]. Our task is to create a program to find the maximum sum of elements from each row in the matrix in C++.Problem DescriptionHere, we will find the maximum sum by taking one element from each row of the matrix in such a way that the element at the current row is greater than the last row element to be considered as a sum. We will find the maximum sum of elements that follows the above condition and print -1 if it is not possible.Let’s take an example to understand the ... Read More

Maximum Sum of Non-Adjacent Elements in C++

Ayush Gupta
Updated on 15-Oct-2020 12:07:39

231 Views

In this problem, we are given an array arr[]. Our task is to create a program to find the Maximum sum such that no two elements are adjacent in C++.Problem DescriptionWe need to find the maximum sum of the sequence from the array such that no 2 numbers from the sum sequence are adjacent in the array.Let’s take an example to understand the problem, Inputarr[] = {5, 1, 3, 7, 9, 2, 5}Output22ExplanationTaking sum sequence from index 0 with alternate elements : 5 + 3 + 9 + 5 = 22 Taking sum sequence from index 1 with alternate elements ... Read More

Maximum Sum Such That No Two Elements Are Adjacent in C++

Ayush Gupta
Updated on 15-Oct-2020 12:05:22

209 Views

In this problem, we are given an array arr[]. Our task is to create a program to find the Maximum sum such that no two elements are adjacent in C++.Problem DescriptionWe need to find the maximum sum of the sequence from the array such that no 2 numbers from the sum sequence are adjacent in the array.Let’s take an example to understand the problem, Inputarr[] = {5, 1, 3, 7, 9, 2, 5}Output22ExplanationTaking sum sequence from index 0 with alternate elements : 5 + 3 + 9 + 5 = 22 Taking sum sequence from index 1 with alternate elements ... Read More

Advertisements