Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by Ayush Gupta
Page 12 of 44
Maximum sum of smallest and second smallest in an array in C++
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#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
Read MoreMaximum sum path in a matrix from top to bottom in C++ Program
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#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 MoreMaximum sum possible for a sub-sequence such that no two elements appear at a distance < K in the array in C++
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#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]; if ...
Read MoreMaximum sum subarray such that start and end values are same in C++
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#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 MoreMaximum sum subsequence with at-least k distant elements in C++
In this tutorial, we will be discussing a program to find maximum sum subsequence with at-least k distant elements.For this we will be provided with an array containing integers and a value K. Our task is to find the subsequence having maximum sum such that all the elements are at least K elements apart.Example#include using namespace std; //finding maximum sum subsequence int maxSum(int arr[], int N, int k) { int MS[N]; MS[N - 1] = arr[N - 1]; for (int i = N - 2; i >= 0; i--) { if (i + ...
Read MoreMaximum students to pass after giving bonus to everybody and not exceeding 100 marks in C++
In this tutorial, we will be discussing a program to find maximum students to pass after giving bonus to everybody and not exceeding 100 marks.For this we will be provided with an array containing marks of N students. Our task is to get more student pass the exam (50 marks required) by giving each student the same amount of bonus marks without any student exceeding 100 marks.Example#include #include using namespace std; int check(int n, int marks[]) { int* x = std::max_element(marks, marks+5); int bonus = 100-(int)(*x); int c = 0; for(int i=0; i= 50) c ...
Read MoreMaximum subarray size, such that all subarrays of that size have sum less than k in C++
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#include using namespace std; //finding maximum length subarray int bsearch(int prefixsum[], int n, int k) { int ans = -1; //performing binary search int left = 1, ...
Read MoreMaximum subarray sum in an array created after repeated concatenation in C++
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#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 (max_so_far ...
Read MoreMaximum sub-matrix area having count of 1's one more than count of 0's in C++
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#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 MoreMaximum subsequence sum such that no three are consecutive
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#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], arr[0] ...
Read More