C++ Articles

Page 62 of 597

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

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 261 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

Program to Find Out Median of an Integer Array in C++

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

Suppose we have to implement a class named MedianClass which contains the following methods −add(value) which adds a value to the data structure.median() finds the median of all the numbers currently present in the data structure.So, if we add 5, 3, 8 and find median, the output will be 5.0, then if we add 9 and find the median, the output will be 6.5.To solve this, we will follow these steps −Define priority queue left and rightDefine addNum method, this will take the number as input −if left is empty or num < top element of left, then, insert num ...

Read More

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

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 392 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 three integers less than or equal to N such that their LCM is maximum - C++

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 643 Views

In this tutorial, we are going to write a program that is based on the concepts of LCM. As the title says, we have to find three numbers that are less than or equal to the given number whose LCM is maximum.Let's see an example.Before diving into the problem let's see what LCM is and write program for it.LCM is the least common multiple of a number. It is also known as Least common divisor. For two positive number a and b, the LCM is the smallest integer that is evenly divisible by both a and b.If the given integers ...

Read More

Find largest subtree sum in a tree in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 467 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 time taken for signal to reach all positions in a string - C++

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 203 Views

In this tutorial, we are going to write a program that computes the time taken for a signal to reach all positions in a string. Let me explain it with an example.We will have a string that contains only s and p characters. s is a signal and p is a position in the string. A signal starts at s and travels in both left and right directions. We are assuming it takes one unit of time to travel to the next position in the string. Our task is to compute the time needed to convert all positions into signals.Let's ...

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 winner of an election where votes are represented as candidate names in C++

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 870 Views

In this tutorial, we are going to write a program that finds the election winner. We will have an array of votes that each candidate got in the election. Let's see an example.Input {"A", "B", "C", "B", "A", "C", "D", "D", "A", "B", "D", "B", "A", "C", "D"}Output AHere, A and B got the same number of votes. In that case, we have to select the winner based on the alphabetical order of their names.Let's see the steps to solve the problem.Initialize an array of string with dummy data.Initialize a map with string as key and int as value.Iterate over the votes ...

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 337 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

Find zeroes to be flipped so that number of consecutive 1's is maximized in C++

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 168 Views

In this tutorial, we are going to find the zeroes count that need to be flipped to get maximum number of consecutive 1's in the array.We are going to use the sliding window approach to solve the problem. Let's see the steps to solve the problem.Initialize the array and max zeroes to be flipped.Initialize window starting, ending indexes along with the length.Store the max sub array of consecutive 1's length and starting index.Iterate over the array until ending indexes crosses the array length.If the zeroes count is less than the max zeroes count then increment the ending index and zeroes ...

Read More
Showing 611–620 of 5,962 articles
« Prev 1 60 61 62 63 64 597 Next »
Advertisements