Programming Articles

Page 1422 of 2547

Print all possible combinations of r elements in a given array of size n in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 2K+ Views

In this problem, we are given an array of size n and a positive integer r. Our task is to print all possible combinations of the elements of the array of size r.Let’s take an example to understand the problem −Input: {5, 6, 7, 8} ; r = 3 Output : {5, 6, 7}, {5, 6, 8}, {5, 7, 8}, {6, 7, 8}To solve this problem an approach would be fixing elements and then recuring or looping over others to find all combinations. In this, we have to fix first n-r+1 elements only and loop or recur over the rest.Example#include ...

Read More

Maximum size subset with given sum in C++

Narendra Kumar
Narendra Kumar
Updated on 11-Mar-2026 851 Views

Problem statementGiven an array of N elements and sum. We need to find size of maximum size subset whose sum is equal to given sumExampleIf input array is arr = { 2, 3, 5, 10 } and sum = 20 then output will be 4 as −2 + 3 + 5 + 10 = 20 which is equal to given sumAlgorithmWe can use dynamic programming to solve this problem.To count the maximal subset, we use another DP array (called as ‘count array’) where count[i][j] is maximal of.count[i][j-1]. Here current element is not considered.scount[i- X][j-1] + 1. Here X is value ...

Read More

stable_sort() in C++ STL

Ajay yadav
Ajay yadav
Updated on 11-Mar-2026 271 Views

The stable_sort method of STL first sorts the components with name as the key in ascending order and afterward the components are arranged with their segment as the key. Moreover, The stable_sort() calculation is viewed as steady in light of the fact that the overall request of comparable components is kept up. Here is the source code of the C++ program which exhibits the stable_sort() calculation demonstrated as follows;Example#include using namespace std; int main(){    int arr[] = { 11, 15, 18, 19, 16, 17, 13, 20, 14, 12, 10 };    int n = sizeof(arr) / sizeof(arr[0]);    stable_sort(arr, arr + n);    cout

Read More

Maximum spiral sum in Binary Tree in C++

Narendra Kumar
Narendra Kumar
Updated on 11-Mar-2026 234 Views

In this problem, we are given a binary tree. Our task is to create a program that will find the maximum spiral sum in a binary tree in C++.Spiral sum of a binary tree is the sum of nodes that are encountered in the spiral traversal of the binary tree.In the spiral traversal of a tree, the nodes are traversed from the root to the leaf node. The traversal takes place from left to right then for the next level from right to left and so on for the further levels.Example −Output −5Explanation −We will consider the spiral traversal until the ...

Read More

Minimum number with digits as and 7 only and given sum in C++

Narendra Kumar
Narendra Kumar
Updated on 11-Mar-2026 214 Views

Problem statementLucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. The task is to find minimum lucky number has the sum of digits equal to n.ExampleIf sum = 22 then lucky number is 4477 as 4 + 4 + 7 + 7 = 22Algorithm1. If sum is multiple of 4, then result has all 4s. 2. If sum is multiple of 7, then result has all 7s. 3. If sum is not multiple of 4 or 7, then we can subtract one of them till sum becomes multiple of other.Example#include ...

Read More

Find Maximum dot product of two arrays with insertion of 0's in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 429 Views

Suppose we have two arrays of positive integers of size m and n. The m > n. We have to maximize the dot product by inserting zeros in the second array. One thing we have to keep in mind that we will not change the ordering of the elements in the given arrays. Suppose the arrays are A = [2, 3, 1, 7, 8], and another array B = [3, 6, 7]. The output will be 107. We can maximize the dot product after inserting 0s at first and third position of the second array. So the product will be ...

Read More

Sorting string in Descending order C++

Ajay yadav
Ajay yadav
Updated on 11-Mar-2026 765 Views

The sorting in ascending or descending order, however, can duly be performed by using string sort method and other means too in the C++ programming. But here, the string compare (first words with the second) and copy (copy the first word in a temp variable) method involved in the inner and outer traversing loop to put the words in descending order as following.Example#include using namespace std; int main(){    char str[3][20]={"Ajay","Ramesh","Mahesh"};    char t[20];    int i, j;    for(i=1; i

Read More

Find maximum height pyramid from the given array of objects in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 830 Views

Suppose we have an array of n objects. Each object has width W[i]. We have to arrange them in a pyramidal way like −Total width of ith is less than (i + 1)thTotal number of objects in the ith is less than (i + 1)thFor example, if the weights are like [40, 100, 20, 30], then output will be 2. So top level is 30, then lower level 20, 40 and 100To solve this, we will use the greedy approach. The idea is to use place the objects with lower width at the top, the next object at the level ...

Read More

Sort an Array of string using Selection sort in C++

Ajay yadav
Ajay yadav
Updated on 11-Mar-2026 1K+ Views

The Selection Sort algorithm sorts an exhibit by more than once finding the base component from the unsorted part and putting it toward the start. In each emphasis of determination sort, the base component from the unsorted subarray is picked and moved to the arranged subarray.Example#include #include using namespace std; #define MAX_LEN 50 void selectionSort(char arr[][50], int n){    int i, j, mIndex;    // Move boundary of unsorted subarray one by one    char minStr[50];    for (i = 0; i < n-1; i++){       // Determine minimum element in unsorted array       ...

Read More

Find maximum sum possible equal sum of three stacks in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 279 Views

Suppose we have three stacks of positive numbers. We have to find the possible equal maximum sum of stacks with removal of top elements allowed. The stacks are represented as an array. The first index of the array represents the top element of the stack. Suppose the stack elements are like [3, 10], [4, 5] and [2, 1]. The output will be 0. The sum can only be equal after removing all elements from all stacks.To solve this, we will follow this idea. The idea is to compare the sum of each stack, if they are not equal, then remove ...

Read More
Showing 14211–14220 of 25,466 articles
Advertisements