Programming Articles

Page 1203 of 2547

Program to Find Out the Amount of Rain to be Caught between the Valleys in C++

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

Suppose we have a 2D matrix, where the elements represent the height of a terrain. Let's imagine a situation where it will rain and all the spaces in the valleys get filled up.We have to find out the amount of rain that will be caught between the valleys.So, if the input is like666864586666then the output will be 3 as we can hold 3 units of water in between 4 and 5 squares.To solve this, we will follow these steps −Define a structure Data, that contains x and y coordinate and height hDefine a priority queue pq, it stores data items ...

Read More

Evaluation of Prefix Expressions in C++

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

In this article, we will discuss the evaluation of prefix Expression. Prefix ExpressionIn this notation, operator is prefixed to operands, i.e. operator is written ahead of operands. For example, +ab. This is equivalent to its infix notation a + b. Prefix notation is also known as Polish Notation.For more read.Example:* + 6 9 - 3 1Prefix expressions are evaluated faster than infix expressions. Also, there are no brackets in prefix expressions which make it evaluate quicker.Algorithm to evaluate Prefix Expression:The evaluation of prefix expression requires a stack data structure. We will push the operators in the stack and then solve the ...

Read More

Count pairs from two sorted arrays whose sum is equal to a given value x in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 11-Mar-2026 554 Views

We are given two arrays containing positive numbers and a value x. The goal is to find pairs of elements of arrays such that pairs of type (A, B) has A+B=x and A belongs to the first array and B belongs to the second array.Let us understand with examplesInput − arr_1[] = {1, 2, 5, 3, 4}; arr_2[] = {7, 0, 1, 3}; x=6Output −Count of pairs from two sorted arrays whose sum is equal to a given value x are − 2Explanation − The pairs are (5, 1) - (arr_1[2], arr_2[2]) and (3, 3) - (arr_1[3], arr_2[3])Input − arr_1[] ...

Read More

Evaluation of Expression Tree in C++

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

In this problem, we are given an expression tree that consist of binary operations like +, - , /, *. We need to do the evaluation of the expression tree and then return the result.Expression Tree is a special type of binary tree in which each node either consist of an operator or operand which are distributed as−Leaf nodes of the tree are values on which the operation is to be performed. Non-leaf nodes consist of the binary operator denoting the operation to be performed. Let’s take an example to understand the problem, Input: Output: 1Explanation: Decoding the expression tree, Exp           ...

Read More

Count pairs from two arrays whose modulo operation yields K in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 11-Mar-2026 297 Views

We are given two arrays containing positive numbers and a value K. The goal is to find unique pairs of elements of arrays such that pairs of type (A, B) has A%B=K or B%A=K and A belongs to the first array and B belongs to the second array.Let us understand with examplesInput − arr_1[] = {1, 2, 5, 3, 4}; arr_2[] = {7, 1, 3}; k=2Output − Count of pairs from two arrays whose modulo operation yields K are − 2Explanation − The pairs are (5, 7) - (arr_1[2], arr_2[1]) 7%5=2 and (5, 3) - (arr_1[2], arr_2[2]) 5%3=2Input − arr_1[] ...

Read More

Evaluation of Risk in Investments in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 239 Views

In this problem, we are given two arrays each denoting an investment plan. Our task is to perform the evaluation of Risk in Investment and find which of the two investments is more promising.Both the investments I1[][] and I2[][] has a set of outcomes and the probability of that investment outcome.Using these values, we need to find the risk in each investment and then print the better investment out of the two investments.For this, we will be using statistical mathematics and find some values that will help us conclude to the better investment.We will find these values, Mean or average amount ...

Read More

Count pairs from two BSTs whose sum is equal to a given value x in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 11-Mar-2026 343 Views

We are given two binary search trees as input and a variable x. The goal is to find pairs of nodes from each tree such that the sum of value of nodes is equal to x. Take node 1 from BST_1 and node 2 from BST_2 and add data part of both. If sum=x. Increment count.Let us understand with examples.Input Output − Count of pairs from two BSTs whose sum is equal to a given value x are − 1Explanation − The pair is (8, 6)Input Output −Count of pairs from two BSTs whose sum is equal to a given value x ...

Read More

Evaluation order of operands in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 287 Views

There are some rules in programming that govern how an operation is performed.The order of evaluation of operation and the associativity of operations (which is left to right is defined).Here is a program to show the evaluation order of operands,Example#include using namespace std; int x = 2; int changeVal() {    x *= x;    return x; } int main() {        int p = changeVal() + changeVal();    cout

Read More

Even numbers at even index and odd numbers at odd index in C++

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

In this problem, we are given an array arr[] of size n consisting of n/2 even values and n/2 odd values. Our task is to create a program to place even numbers at even index and odd numbers at odd index. Let’s take an example to understand the problem, Input: arr[] = {5, 1, 6, 4, 3, 8}Output: arr[] = {6, 1, 5, 4, 3, 8}Solution Approach −A solution would be traversing the array and then find the end number which is not at even position and replacing it with the next off place value. This is a promising solution but the solution ...

Read More

Count of subsequences having maximum distinct elements in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 11-Mar-2026 434 Views

We are given an array arr[] containing integers only. The goal is to find the number of subsequences of arr[] such that they have maximum number distinct elements. If the array is [ 4, 1, 2, 3, 4 ] then two subsequences will be [ 4, 1, 2, 3 ] and [ 1, 2, 3, 4 ].Let us understand with examplesInput − arr[]= { 1, 3, 5, 4, 2, 3, 1 }Output − Count of subsequences having maximum distinct elements are − 4Explanation − The maximum distinct elements are 1, 2, 3, 4 and 5. Count is 5. Subsequences will ...

Read More
Showing 12021–12030 of 25,466 articles
Advertisements