Server Side Programming Articles - Page 1713 of 2650

Maximum sum Bi-tonic Sub-sequence in C++

Ayush Gupta
Updated on 15-Oct-2020 13:56:52

208 Views

In this problem, we are given an array arr[]. Our task is to create a program to find the maximum sum Bi-tonic subsequence in C++.Bi-tonic subsequence is a special sequence whose elements first increase and then decrease.Let’s take an example to understand the problem, Inputarr[] = {4, 2, 3, 7, 9, 6, 3, 5, 1}Output33ExplanationThe Bi-tonic subsequence which gives the largest sum is {2, 3, 7, 9, 6, 5, 1} Sum = 2 + 3 + 7 + 9 + 6 + 5 + 1 = 33Solution ApproachTo find the maximum sum bitonic subsequence, we will create two arrays, incSeq[] ... Read More

Maximum sum bitonic subarray in C++

Ayush Gupta
Updated on 15-Oct-2020 14:00:02

619 Views

In this problem, we are given an array arr[]. Our task is to create a program to find the maximum sum bitonic subarray in C++.Bitonic Subarray is a special subarray in which the element strictly increase first and then strictly decreases after reaching a certain point.Let’s take an example to understand the problem, Inputarr[] = {4, 2, 3, 7 ,9, 6, 3, 5, 1}Output30ExplanationThe bitonic subarray is [2, 3, 7, 9, 6, 3]. Sum = 2 + 3 + 7 + 9 + 6 + 3 = 30Solution ApproachThe solution is similar to that in the bitonic subsequence problem. We ... Read More

Maximum sum and product of the M consecutive digits in a number in C++

Revathi Satya
Updated on 22-May-2024 11:55:01

362 Views

In this article, we are given a string representing a number. Our task is to create a program in C++ to find the maximum sum and product of M consecutive digits from the given number. We find all sequences of M consecutive digits and return the maximum sum and product. In mathematics, consecutive numbers are defined as those numbers that follow each other in increasing order from the smallest to the largest, with no missing numbers in between. This problem can be iterated through the string representation of the number, in other words, we consider consecutive segments of the length ... Read More

Maximum sum alternating subsequence in C++

Ayush Gupta
Updated on 10-Jul-2020 14:20:34

153 Views

In this tutorial, we will be discussing a program to find maximum sum alternating subsequence.For this we will be provided with an array of integers. Our task is to find the maximum sum of an alternating subsequence i.e sequence which is first decreasing, then increasing, then decreasing and so on.Example Live Demo#include using namespace std; //returning maximum sum alternating series int maxAlternateSum(int arr[], int n) {    if (n == 1) return arr[0];    int dec[n];    memset(dec, 0, sizeof(dec));    int inc[n];    memset(inc, 0, sizeof(inc));    dec[0] = inc[0] = arr[0];    int flag = 0 ;    for (int i=1; i

Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST in C++

Ayush Gupta
Updated on 10-Jul-2020 14:18:26

212 Views

In this tutorial, we will be discussing a program to find maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST.For this we will be provided with a binary tree. Our task is to print the sum of a sub-tree which is also a binary search tree.Example Live Demo#include using namespace std; //creating binary tree node struct Node {    struct Node* left; struct Node* right; int data;    Node(int data) {       this->data = data;       this->left = NULL;       this->right = NULL;    } }; struct Info ... Read More

Maximum subsequence sum such that no three are consecutive

Ayush Gupta
Updated on 10-Jul-2020 14:15:14

363 Views

In this tutorial, we will be discussing a program to find maximum subsequence sum such that no three are consecutive.For this we will be provided with a series of positive integers. Our task is to find the maximum sum without taking in their consecutive positive integers in the sum value.Example Live Demo#include using namespace std; //returning maximum subsequence without involving //three consecutive numbers int maxSumWO3Consec(int arr[], int n) {    int sum[n]; if (n >= 1) sum[0] = arr[0];    if (n >= 2) sum[1] = arr[0] + arr[1];    if (n > 2) sum[2] = max(sum[1], max(arr[1] + arr[2], ... Read More

Maximum sub-matrix area having count of 1’s one more than count of 0’s in C++

Ayush Gupta
Updated on 10-Jul-2020 14:13:32

116 Views

In this tutorial, we will be discussing a program to find maximum sub-matrix area having count of 1’s one more than count of 0’s.For this we will be provided with a matrix containing 0’s and 1’s. Our task is to get the sub-matrix of maximum area containing more 1’s than 0’sExample Live Demo#include using namespace std; #define SIZE 10 //finding length of longest sub-matrix int lenOfLongSubarr(int arr[], int n, int& start, int& finish) {    unordered_map um;    int sum = 0, maxLen = 0;    for (int i = 0; i < n; i++) {       sum ... Read More

Maximum subarray sum in an array created after repeated concatenation in C++

Ayush Gupta
Updated on 10-Jul-2020 14:08:47

131 Views

In this tutorial, we will be discussing a program to find maximum subarray sum in an array created after repeated concatenation.For this we will be provided with an array and an integer K. Our task is to find the subarray with the maximum elements when the given array is repeated K times.Example Live Demo#include using namespace std; //returning sum of maximum subarray int maxSubArraySumRepeated(int a[], int n, int k) {    int max_so_far = INT_MIN, max_ending_here = 0;    for (int i = 0; i < n*k; i++) {       max_ending_here = max_ending_here + a[i%n];       if ... Read More

Maximum Subarray Sum Excluding Certain Elements in C++

Ayush Gupta
Updated on 10-Jul-2020 14:07:10

151 Views

In this tutorial, we will be discussing a program to find maximum Subarray Sum Excluding Certain Elements.For this we will be provided with two arrays of size M and N. Our task is to find a sub-array in the first array such that no element inside the subarray is present inside the second array and the elements of subarray sum up to be maximum.Example Live Demo#include using namespace std; //checking if element is present in second array bool isPresent(int B[], int m, int x) {    for (int i = 0; i < m; i++)    if (B[i] == x) ... Read More

Maximum subarray size, such that all subarrays of that size have sum less than k in C++

Ayush Gupta
Updated on 10-Jul-2020 14:05:05

168 Views

In this tutorial, we will be discussing a program to find maximum subarray size, such that all subarrays of that size have sum less than k.For this we will be provided with an array of size N and an integer k. Our task is to find the length of subarray such that all the sub-arrays of that length in the given array sum to be less than or equal to k.Example Live Demo#include using namespace std; //finding maximum length subarray int bsearch(int prefixsum[], int n, int k) {    int ans = -1;    //performing binary search    int left = ... Read More

Advertisements