Programming Articles - Page 1721 of 3366

Maximum sum of nodes in Binary tree such that no two are adjacent in C++

Ayush Gupta
Updated on 09-Sep-2020 13:10:07

242 Views

In this tutorial, we will be discussing a program to find maximum sum of nodes in Binary tree such that no two are adjacent.For this we will be provided with a binary tree. Our task is to find the subset having maximum sum such that no two nodes in subset are directly connected.Example Live Demo#include using namespace std; //binary tree node structure struct node {    int data;    struct node *left, *right; }; struct node* newNode(int data) {    struct node *temp = new struct node;    temp->data = data;    temp->left = temp->right = NULL;    return temp; ... Read More

Maximum sum of nodes in Binary tree such that no two are adjacent | Dynamic Programming In C++

Ayush Gupta
Updated on 09-Sep-2020 13:08:04

138 Views

In this tutorial, we will be discussing a program to find maximum sum of nodes in Binary tree such that no two are adjacent using Dynamic Programming.For this we will be provided with a binary tree. Our task is to find the subset having maximum sum such that no two nodes in the subset are directly connected using Dynamic Programming.Example Live Demo#include using namespace std; //finding diameter using dynamic programming void dfs(int node, int parent, int dp1[], int dp2[], list* adj, int tree[]){    int sum1 = 0, sum2 = 0;    for (auto i = adj[node].begin(); i != adj[node].end(); ... Read More

Maximum sum of increasing order elements from n arrays in C++

Ayush Gupta
Updated on 09-Sep-2020 13:05:50

178 Views

In this tutorial, we will be discussing a program to find maximum sum of increasing order elements from n arrays.For this we will be provided with N arrays of M size. Our task is to find the maximum sum by selecting one element from each array such that element from the previous array is smaller than the next one.Example Live Demo#include #define M 4 using namespace std; //calculating maximum sum by selecting //one element int maximumSum(int a[][M], int n) {    for (int i = 0; i < n; i++)       sort(a[i], a[i] + M);    int sum ... Read More

Maximum product of indexes of next greater on left and right in C++

Ayush Gupta
Updated on 09-Sep-2020 13:03:39

179 Views

In this tutorial, we will be discussing a program to find maximum product of indexes of next greater on left and right.For this we will be provided with an array of integers. Our task is to find the element with maximum Left-Right product (L(i)*R(i) where L(i) is closest index on left side and greater than current element and R(i) is closest index on right side and greater than current element).Example Live Demo#include using namespace std; #define MAX 1000 //finding greater element on left side vector nextGreaterInLeft(int a[], int n) {    vector left_index(MAX, 0);    stack s;    for (int ... Read More

Maximum product of an increasing subsequence in C++

Ayush Gupta
Updated on 09-Sep-2020 13:01:04

116 Views

In this tutorial, we will be discussing a program to find maximum product of an increasing subsequence.For this we will be provided with an array of integers. Our task is to find the maximum product of any subsequence of the array with any number of elements.Example Live Demo#include #define ll long long int using namespace std; //returning maximum product ll lis(ll arr[], ll n) {    ll mpis[n];    //initiating values    for (int i = 0; i < n; i++)       mpis[i] = arr[i];    for (int i = 1; i < n; i++)       ... Read More

Maximum product of an increasing subsequence of size 3 in C++

Ayush Gupta
Updated on 09-Sep-2020 12:59:02

143 Views

In this tutorial, we will be discussing a program to find maximum product of an increasing subsequence of size 3.For this we will be provided with an array of positive integers. Our task is to find a subsequence of three elements with the maximum product.Example#include using namespace std; //returning maximum product of subsequence long long int maxProduct(int arr[] , int n) {    int smaller[n];    smaller[0] = -1 ;    setS ;    for (int i = 0; i < n ; i++) {       auto j = S.insert(arr[i]);       auto itc = j.first;   ... Read More

Maximum product of a triplet (subsequence of size 3) in array in C++

Ayush Gupta
Updated on 09-Sep-2020 12:57:09

143 Views

In this tutorial, we will be discussing a program to find maximum product of a triplet (subsequence of size 3) in array.For this we will be provided with an array of integers. Our task is to find the triplet of elements in that array with the maximum productExample Live Demo#include using namespace std; //finding the maximum product int maxProduct(int arr[], int n){    if (n < 3)       return -1;    int max_product = INT_MIN;    for (int i = 0; i < n - 2; i++)       for (int j = i + 1; j ... Read More

Maximum product of 4 adjacent elements in matrix in C++

Ayush Gupta
Updated on 09-Sep-2020 12:54:21

286 Views

In this tutorial, we will be discussing a program to find maximum product of 4 adjacent elements in matrix.For this we will be provided with a square matrix. Our task is to find the maximum product of four adjacent elements which can be top, down, right, left, or diagonal.Example Live Demo#include using namespace std; const int n = 5; //finding maximum product int FindMaxProduct(int arr[][n], int n) {    int max = 0, result;    for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {     ... Read More

Maximum product from array such that frequency sum of all repeating elements in product is less than or equal to 2 * k in C++

Ayush Gupta
Updated on 09-Sep-2020 12:50:50

142 Views

In this tutorial, we will be discussing a program to find maximum product from array such that frequency sum of all repeating elements in product is less than or equal to 2 * k.For this we will be provided with an array and an integer k. Our task is to find the maximum product from the array given that the frequency sum of all digits must be smaller than or equal to 2 * k.Example Live Demo#include using namespace std; #define ll long long int //returning maximum product value ll maxProd(int arr[], int n, int k) {    ll product ... Read More

Maximum Product Cutting | DP-36 in C++

Ayush Gupta
Updated on 09-Sep-2020 12:48:26

182 Views

In this tutorial, we will be discussing a program to find maximum Product Cutting | DP-36.For this we will be provided with a rope of N meters. Our task is to cut the rope in different integer lengths such that the product of their lengths is the maximumExample Live Demo#include using namespace std; //finding maximum of two, three integers int max(int a, int b) {    return (a > b)? a : b; } int max(int a, int b, int c) {    return max(a, max(b, c)); } //returning maximum product int maxProd(int n) {    if (n == 0 ... Read More

Advertisements