Ayush Gupta

Ayush Gupta

433 Articles Published

Articles by Ayush Gupta

Page 12 of 44

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

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 211 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#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 More

Maximum sum path in a matrix from top to bottom in C++ Program

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 324 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#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

Maximum sum possible for a sub-sequence such that no two elements appear at a distance < K in the array in C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 164 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#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 More

Maximum sum subarray such that start and end values are same in C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 238 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#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

Maximum sum subsequence with at-least k distant elements in C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 215 Views

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 More

Maximum students to pass after giving bonus to everybody and not exceeding 100 marks in C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 198 Views

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 More

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

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 214 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#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 More

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

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 164 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#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 More

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

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 162 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#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 subsequence sum such that no three are consecutive

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 408 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#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
Showing 111–120 of 433 articles
« Prev 1 10 11 12 13 14 44 Next »
Advertisements