sudhir sharma

sudhir sharma

975 Articles Published

Articles by sudhir sharma

Page 42 of 98

Find larger of x^y and y^x in C++

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

In this problem, we are given two numbers x and y. Our task is to find larger of x^y and y^x. Problem Description: The problem is simple, we need to find weather x to the power y is greater than y to the power x.Let’s take an example to understand the problem,  Input: x = 4, y = 5Output: 1024Explanation: x^y = 4^5 = 1024y^x = 5^4 = 625Solution ApproachThe solution to the problem is simple. We need to find the value of x^y and y^x and return the maximum of both.There can be a more mathematically easy way to solve the problem, which is ...

Read More

Find largest number smaller than N with same set of digits in C++

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

In this problem, we are given a string N that represents a number. Our task is to find the largest number smaller than N with the same set of digits.  Problem Description: we need to find a number using all the digits of the given number which is the largest smaller number than N.Let’s take an example to understand the problem,  Input: N = “54314”Output: 54341Solution ApproachA simple solution to the problem is by finding the digit of the number which can be moved to find the largest smaller number. Now, the number should be greater than its right success in order to solve ...

Read More

Find largest subtree sum in a tree in C++

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

In this problem, we are given a binary tree. Our task is to find the largest subtree sum in a tree. Problem Description: The  binary tree consists of positive as well as negative values. And we need to find the subtree that has the maximum sum of nodes.Let’s take an example to understand the problem,  Output: 13Explanation: The sum of left-subtree is 7The sum of right-subtree is 1The sum of tree is 13Solution ApproachTo solve the problem, we will do the post-order traversal. Calculate sum of nodes left subtree and right subtree. For current node, check if the sum of nodes of current node ...

Read More

Find largest sum of digits in all divisors of n in C++

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

In this problem, we are given an integer n. Our task is to find the largest sum of digits in all divisors of n. Problem  Description: Here, we will find the divisor of the number n whose sum of digits in largest.Let’s take an example to understand the problem,  Input: 18Output: 9Explanation: All divisors of 18 are 1, 2, 3, 6, 9, 18.The maximum digits sum is 9.Solution ApproachFind all divisors of the number N. And then find the sum of digits of each divisors and then return the value with the largest sum.Program to illustrate the working of our solution, Example#include using namespace ...

Read More

Find Last Digit of a^b for Large Numbers in C++

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

In this problem, we are given two numbers a and b. Our task is to find Last Digit of a^b for Large Numbers. Let’s take an example to understand the problem,  Input: a = 4 b = 124Output: 6Explanation: The value of a^b is 4.523128486 * 1074Solution ApproachThe solution to the problem is based on the fact that all the exponents of a number will be repeated after 4 exponent values.So, we will find the value of b%4. Also, for any base value, the last digit of its power is decided by the last digit of the base value.So, the resultant value will be ...

Read More

Find last five digits of a given five digit number raised to power five in C++

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

In this problem, we are given a number N. Our task is to find the last five digits of a given five digit number raised to power five. Let’s take an example to understand the problem,  Input: N = 25211Output:Solution ApproachTo solve the problem, we need to find only the last five digits of the resultant value. So, we will find the last digit of the number after every power increment by finding the 5 digit remainder of the number.  At last return the last 5 digits after power of 5.Program to illustrate the working of our solution, Example#include using namespace ...

Read More

Sum of the products of all possible Subsets in C++

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

In this problem, we are given an array arr[] of N numbers. Our task is to create a program that will find the sum of the products of all possible subsets.Here, we will find all subsets and then find the product of all elements for each subset. Then add all the values to calculate the sum.Let’s take an example to understand the problem, Inputarr[] = {4, 5, 6}Output209Explanation −All subsets of arr[] are: {4}, {5}, {6}, {4, 5}, {5, 6}, {4, 6}, {4, 5, 6} Sum of product = (4) + (5) + (6) + (4*5) + (5*6) + (4*6) ...

Read More

Sum of the series 0.7, 0.77, 0.777 ... upto n terms in C++

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

In this problem, we are given n terms of a number. The series is 0.7, 0.77, 0.777…. Our task is to create a program to find the sim of the series 0.7, 0.77, 0.777 … upto n terms.Let’s take an example to understand the problem, Input  4Output  Explanation −0.7 + 0.77 + 0.777 + 0.7777 = 3.0247To solve this problem, we will derive the formula for sum of series. Let’s find the general formula for it, sum = 0.7 + 0.77 + 0.777 + ... upto n terms sum = 7 (0.1 + 0.11 + 0.111 + … upto n ...

Read More

Sum of the series 1 + (1+3) + (1+3+5) + (1+3+5+7) + + (1+3+5+7+....+(2n-1)) in C++

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

In this problem, we are given an integer n. Our task is to create a program to find the sum of the series 1 + (1+3) + (1+3+5) + (1+3+5+7) + + (1+3+5+7+....+(2n-1)).From this series, we can observe that ith term of the series is the sum of first i odd numbers.Let’s take an example to understand the problem, Inputn = 3Output 14Explanation −(1) + (1+3) + (1+3+5) = 14A simple solution to this problem is using a nested loop and then add all odd numbers to a sum variable. Then return the sum.ExampleProgram to illustrate the working of our solution, ...

Read More

Sum of the series 1, 3, 6, 10... (Triangular Numbers) in C++

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

In this problem, we are given a number n which is given the n of elements of the series 1, 3, 6, 10 … (triangular number). Our task is to create a program to calculate the sum of the series.Let’s brush up about triangular numbers before calculating the sum.Triangular numbers are those numbers that can be represented in the form of a triangle.A triangle is formed in such a way that the first row has one point, second has two, and so on.ExampleLet’s take an example to understand the problem, Inputn = 4OutputExplanation − sum = T1 + T2 + T3 ...

Read More
Showing 411–420 of 975 articles
« Prev 1 40 41 42 43 44 98 Next »
Advertisements