Found 7197 Articles for C++

Maximum sum increasing subsequence from a prefix and a given element after prefix is must in C++

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

165 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 | DP-14 in C++

Ayush Gupta
Updated on 22-Jul-2020 08:20:57

156 Views

In this tutorial, we will be discussing a program to find maximum Sum Increasing Subsequence.For this we will be provided with an array containing N integers. Our task is to pick up elements from the array adding to the maximum sum such that the elements are in sorted orderExample Live Demo#include using namespace std; //returning the maximum sum int maxSumIS(int arr[], int n) {    int i, j, max = 0;    int msis[n];    for ( i = 0; i < n; i++ )       msis[i] = arr[i];    for ( i = 1; i < n; ... Read More

Maximum sum in circular array such that no two elements are adjacent in C++

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

352 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 in a 2 x n grid such that no two elements are adjacent 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 from three arrays such that picking elements consecutively from same is not allowed in C++

Ayush Gupta
Updated on 15-Oct-2020 12:34:25

340 Views

In this problem, we are given three arrays arr1[], arr2[], and arr3[] all of size N. Our task is to create a program to find the Maximum sum from three arrays such that picking elements consecutively from same is not allowed in C++.Problem DescriptionWe will find the maximum sum by choosing N elements. i=th element can be chosen of the sum from the i-th element of the array i.e. ith sum is from arr1[i]/ arr2[i]/ arr3[i]. Also, keep in mind that we cannot choose two consecutive elements that can be chosen from the same array.Let’s take an example to understand ... Read More

Maximum sum from a tree with adjacent levels not allowed in C++

Ayush Gupta
Updated on 15-Oct-2020 13:45:55

154 Views

In this problem, we are given a binary tree consisting of positive numbers. Our task is to create a program to find the Maximum sum from a tree with adjacent levels not allowed in C++.Code DescriptionHere, we will find the maximum sum of node of the tree in such a way that the sum does not contain nodes from two adjacent levels of the tree.Let’s take an example to understand the problem, Output21ExplanationTaking root as starting level, sum = 5 + 3 + 8 + 1 = 17 Taking sub-child of root as starting level, sum = 2 + 6 ... Read More

Build Array Where You Can Find The Maximum Exactly K Comparisons in C++

Arnab Chakraborty
Updated on 21-Jul-2020 09:24:12

177 Views

Suppose we have three integers n, m, and k. If we have following algorithm to find the maximum element of an array of positive integers −max_val := -1 max_ind := -1 search_cost := 0 n := size of arr for initialize i := 0, when i < n, update (increase i by 1), do:    if max_val < arr[i], then:       max_val := arr[i]       max_ind := i       (increase search_cost by 1) return max_indWe have to make the array arr which has the following criteria: arr has exactly n integers. all elements arr[i] are in range 1 to m(including) (0

Number of Ways to Paint N × 3 Grid in C++ program

Arnab Chakraborty
Updated on 21-Jul-2020 09:15:13

166 Views

Suppose we have a grid whose size is n x 3 and we want to paint every cell of the grid with exactly one of the three colors. Here the colors that will be used are Red, Yellow and Green.Now there is a constraint, that is no two adjacent cells have the same color. We have n number of rows of the grid. Finally, we have to find the number of ways we can paint this grid. The answer may be very large so return it modulo 10^9 + 7.So, if the input is like 1, then the output will ... Read More

The Maze III in C++

Arnab Chakraborty
Updated on 21-Jul-2020 09:13:05

506 Views

Suppose there is a maze with empty spaces and walls and there is also a ball in that maze. The ball can go through empty spaces by rolling up (u), down (d), left (l) or right (r) directions, but it keeps rolling until hitting a wall. When the ball stops, it could choose the next direction. One hole is also in that maze. The ball will drop into the hole if it rolls on to the hole.So if we have the ball position, the hole position and the maze, we have to find out how the ball could drop into ... Read More

Substring with Concatenation of All Words in C++ Program

Arnab Chakraborty
Updated on 21-Jul-2020 09:09:36

221 Views

Suppose we have a string, s, and we also have a list of words, words present in the array are all of the same length. We have to find all starting indices of substring(s) in s that is a concatenation of each word in words exactly once and without any intervening characters.So if the input is like “barfoothefoobarman” and words are [“foo”, “bar”], then the output will be [0, 9]. This is because the substring starting at index 0 and 9 are “barfoo” and “foobar”.To solve this, we will follow these steps −Define a method called ok(), this will take ... Read More

Advertisements