Found 7197 Articles for C++

Maximum sum of smallest and second smallest in an array in C++

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

165 Views

In this tutorial, we will be discussing a program to find maximum sum of smallest and second smallest in an array.For this we will be provided with an array containing integers. Our task is to find the maximum sum of smallest and second smallest elements in every possible iteration of array.Example Live Demo#include using namespace std; //returning maximum sum of smallest and //second smallest elements int pairWithMaxSum(int arr[], int N) {    if (N < 2)       return -1;    int res = arr[0] + arr[1];    for (int i=1; i

Maximum Sum of Products of Two Arrays in C++

Ayush Gupta
Updated on 09-Sep-2020 13:18:27

225 Views

In this tutorial, we will be discussing a program to find maximum Sum of Products of Two Arrays.For this we will be provided with two arrays of same size. Our task is to find the maximum sum by multiplying exactly one element from first element with one element from the second array.Example Live Demo#include using namespace std; //calculating maximum sum by //multiplying elements int maximumSOP(int *a, int *b) {    int sop = 0;    int n = sizeof(a)/sizeof(a[0]);    sort(a,a+n+1);    sort(b,b+n+1);    for (int i = 0; i

Maximum sum of pairwise product in an array with negative allowed in C++

Ayush Gupta
Updated on 09-Sep-2020 13:16:30

193 Views

In this tutorial, we will be discussing a program to find maximum sum of pairwise product in an array with negative allowed.For this we will be provided with an array containing integers. Our task is to find the maximum sum while performing pairwise multiplications.Example Live Demo#include #define Mod 1000000007 using namespace std; //finding the maximum sum long long int findSum(int arr[], int n) {    long long int sum = 0;    //sorting the array    sort(arr, arr + n);    int i = 0;    while (i < n && arr[i] < 0) {       if (i ... Read More

Maximum sum of pairs with specific difference in C++

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

176 Views

In this tutorial, we will be discussing a program to find maximum sum of pairs with specific difference.For this we will be provided with an array containing integers and a value K. Our task is to pair elements having difference less than K and finally find the maximum sum of the elements in disjoint sets.Example Live Demo#include using namespace std; //returning maximum sum of disjoint pairs int maxSumPairWithDifferenceLessThanK(int arr[], int N, int K){    sort(arr, arr+N);    int dp[N];    dp[0] = 0;    for (int i = 1; i < N; i++) {       dp[i] = dp[i-1]; ... Read More

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

226 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

124 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

167 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

164 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

108 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

138 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

Advertisements