- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 7347 Articles for C++
![Ayush Gupta](https://www.tutorialspoint.com/assets/profiles/134135/profile/60_13095-1568350695.jpg)
95 Views
In this tutorial, we will be discussing a program to find maximum sum subarray such that start and end values are same.For this we will be provided with an array containing integers. Our task is to find the subarray with the maximum sum such that the elements are both its ends are equal.Example Live Demo#include using namespace std; //finding the maximum sum int maxValue(int a[], int n) { unordered_map first, last; int pr[n]; pr[0] = a[0]; for (int i = 1; i < n; i++) { pr[i] = pr[i - 1] + a[i]; ... Read More
![Ayush Gupta](https://www.tutorialspoint.com/assets/profiles/134135/profile/60_13095-1568350695.jpg)
142 Views
In this tutorial, we will be discussing a program to find maximum sum rectangle in a 2D matrix.For this we will be provided with a matrix. Our task is to find out the submatrix with the maximum sum of its elements.Example Live Demo#include using namespace std; #define ROW 4 #define COL 5 //returning maximum sum recursively int kadane(int* arr, int* start, int* finish, int n) { int sum = 0, maxSum = INT_MIN, i; *finish = -1; int local_start = 0; for (i = 0; i < n; ++i) { sum += arr[i]; ... Read More
![Ayush Gupta](https://www.tutorialspoint.com/assets/profiles/134135/profile/60_13095-1568350695.jpg)
77 Views
In this tutorial, we will be discussing a program to find maximum sum possible for a sub-sequence such that no two elements appear at a distance < K in the array.For this we will be provided with an array containing N intergers and a value K. Our task is to find the maximum sum of the subsequence including elements not nearer than K.Example Live Demo#include using namespace std; //returning maximum sum int maxSum(int* arr, int k, int n) { if (n == 0) return 0; if (n == 1) return arr[0]; ... Read More
![Ayush Gupta](https://www.tutorialspoint.com/assets/profiles/134135/profile/60_13095-1568350695.jpg)
219 Views
In this tutorial, we will be discussing a program to find maximum sum path in a matrix from top to bottom.For this we will be provided with a matrix of N*N size. Our task is to find the maximum sum route from top row to bottom row while moving to diagonally higher cell.Example Live Demo#include using namespace std; #define SIZE 10 //finding maximum sum path int maxSum(int mat[SIZE][SIZE], int n) { if (n == 1) return mat[0][0]; int dp[n][n]; int maxSum = INT_MIN, max; for (int j = 0; j < n; j++) ... Read More
![Ayush Gupta](https://www.tutorialspoint.com/assets/profiles/134135/profile/60_13095-1568350695.jpg)
102 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
![Ayush Gupta](https://www.tutorialspoint.com/assets/profiles/134135/profile/60_13095-1568350695.jpg)
155 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
![Ayush Gupta](https://www.tutorialspoint.com/assets/profiles/134135/profile/60_13095-1568350695.jpg)
126 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
![Ayush Gupta](https://www.tutorialspoint.com/assets/profiles/134135/profile/60_13095-1568350695.jpg)
104 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
![Ayush Gupta](https://www.tutorialspoint.com/assets/profiles/134135/profile/60_13095-1568350695.jpg)
152 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
![Ayush Gupta](https://www.tutorialspoint.com/assets/profiles/134135/profile/60_13095-1568350695.jpg)
68 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