sudhir sharma

sudhir sharma

975 Articles Published

Articles by sudhir sharma

Page 32 of 98

Find m-th summation of first n natural numbers in C++

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

In this problem, we are given two integers m and n. Our task is to Find m-th summation of the first n natural numbers. Problem Description: we will find sum of sum of n natural numbers m times. The sum is given by the formula, if (m > 1),           sum(n, m) = sum( sum(n, (m-1)), 1 )if (m = 1)          sum(n, m) = sum(n, 1) = sum of n natural numbersLet’s take an example to understand the problem,  Input: m = 4, n = 2Output: 231Explanation: sum(2, 4)     = sum ( sum(2, 3), 1 ...

Read More

Find max of two Rational numbers in C++

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

In this problem, we are given two Rational Numbers. Our task is to find max of two Rational numbers. Here, the rational numbers are in the form of p/q.Let’s take an example to understand the problem,  Input: rat1 = 5/4, rat2 = 3/2Output: 3/2Explanation: 5/4 = 1.253/2 = 1.5Solution Approach −A simple solution to the problem is by using a method similar to the one we used to perform in school.For this, we will find the L.C.M of the denominator. And then multiply the numerator based on the denominators value. Then for the common denominator, the rational number with maximum numerator value is the ...

Read More

Find maximum (or minimum) in Binary Tree in C++

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

In this problem, we are given a binary tree. Our task is to Find maximum (or minimum) in Binary Tree. Problem Description: We need to find the nodes of the binary tree that have maximum and minimum value in the binary tree.Let’s take an example to understand the problem,  Input: Output: max = 9 , min = 1Solution ApproachWe need to find the max node of the binary tree. We will do this by traversing the pointer until we reach the leaf node and then find the maximum node of the tree.Program to illustrate the working of our solution, Example#include using namespace std; ...

Read More

Find maximum (or minimum) sum of a subarray of size k in C++

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

In this problem, we are given an  array arr[] and a number k. Our task is to Find the maximum (or minimum) sum of a subarray of size k. Let’s take an example to understand the problem,  Input: arr[] = {55, 43, 12, 76, 89, 25, 99} , k = 2Output: 165Explanation:The subarray of size 2 has sum = 76 + 89 = 165Solution ApproachA simple approach to solve the problem is by finding all k sized subarrays and then return the sum with maximum value.Another Approach is using the sliding window,  we will find the sum of k sized subarrayes. For this, the ...

Read More

Find maximum among all right nodes in Binary Tree in C++

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

In this problem, we are given a Binary Tree. Our task is to find maximum among all right nodes in Binary Tree. Problem Description: Here, we need to find the maximum value amongst all right child nodes of the binary Tree.Let’s take an example to understand the problem,  Input: Output: 9Explanation: All right nodes are: {2, 8, 9}. Maximum of them is 9.Solution ApproachTo solve the problem, we need to traverse the tree and check if its right child exists. If it exists, compare with maxRight element and replace if it is greater.Program to illustrate the working of our solution, Example#include using namespace std; ...

Read More

Find maximum among x^(y^2) or y^(x^2) where x and y are given in C++

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

In this problem, we are given two values x and y. Our task is to find maximum among x^(y^2) or y^(x^2) where x and y are given. Let’s take an example to understand the problem,  Input: x = 4, y = 3Output: 3^(4^2)Explanation: x^(y^2) = 4^(3^2) = 4^9 = 262144y^(x^2) = 3^(4^2) = 3^16 = 43046721Solution approachOne approach can be to calculate both values and then print the maximum of both. But this method does not work when the values are large.A simple and easy approach is using natural log (ln) which will be the solution easier.ln(x^(y^2)) = (y^2) * ln(x)ln(y^(x^2)) = (x^2) * ...

Read More

Find maximum average subarray of k length in C++

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

In this problem, we are given an array arr[] of size n consisting of positive and negative values and an integer k. Our task is to find the maximum average subarray of k length. Let’s take an example to understand the problem,  Input: arr[] = {4, -1, 5, 6, -2, 4} k = 3Output: 10Explanation: The subarray of size 3 with max sum is -1, 5, 6 = 10Solution ApproachA solution to the problem is done by using an auxiliary array to store cumulative sum till the current index in the array.To find the sum of subarrays, we need to compute the difference between the indices ...

Read More

Find maximum in an array without using Relational Operators in C++

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

In this problem, we are given an array arr[] of size n consisting of positive values. Our task is to find maximum in an array without using Relational Operators. Let’s take an example to understand the problem, Input: arr[] = {5, 1, 6, 7 , 8, 2}Output: 8Solution ApproachSince we need to compare values without using logical operators. For this we need to perform repeated subtraction, the number which will last longer will be the larger one.We will decrement all values by one till they become zero. We will start with the first two values of the array and find the greatest ...

Read More

Print cells with same rectangular sums in a matrix in C++

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

In this problem, we are given a matrix mat of size mXn of integer values. Our task is to create a program to Print cells with same rectangular sums in a matrix.Problem description: We will be finding a cell in the matrix in such a way that the sum of sub-matrices that are starting and ending with the cell is equal to the sum of all the remaining elements. For a cell the sum of matrix (a, b) sub-matrix mat[0][0] to mat[a][b] and mat[a][b] to mat[m][n] is equal to the sum of all remaining elements.Let’s take an example to understand the problem, ...

Read More

Print BST keys in given Range - O(1) Space in C++

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

In this problem, we are given two values k1 and k2 (k1 < k2), and the root of the binary search tree. Our task is to create a program to Print BST keys in given Range. Problem Description: We will print all the keys of the tree from n1 to n2 in increasing order.Let’s take an example to understand the problem,  Input: k1 = 4, k2 = 12Output: 6, 7, 9Solution ApproachSimple we can solve the problem using inorder traversal but the space complexity there is 0(n) but the need of the hour is to solve in O(1) complexity. So, for this, we will use a ...

Read More
Showing 311–320 of 975 articles
« Prev 1 30 31 32 33 34 98 Next »
Advertisements