Found 7197 Articles for C++

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

sudhir sharma
Updated on 25-Jan-2021 05:00:05

280 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, ExampleLive Demo#include using ... Read More

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

sudhir sharma
Updated on 25-Jan-2021 04:58:42

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 largest sum of digits in all divisors of n in C++

sudhir sharma
Updated on 25-Jan-2021 04:58:23

211 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, ExampleLive Demo#include using ... Read More

Find largest subtree sum in a tree in C++

sudhir sharma
Updated on 25-Jan-2021 04:56:41

398 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 number smaller than N with same set of digits in C++

sudhir sharma
Updated on 25-Jan-2021 04:56:14

312 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 larger of x^y and y^x in C++

sudhir sharma
Updated on 25-Jan-2021 04:55:33

190 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 kth smallest number in range [1, n] when all the odd numbers are deleted in C++

sudhir sharma
Updated on 25-Jan-2021 04:54:25

189 Views

In this problem, we are given two integer values n and k. Our task is to find kth smallest number in range [1, n] when all the odd numbers are deleted. We need to find the kth smallest number in the range [1, n] which contains only even values.So, from range [1, 5] -> number will be 2, 4.Let’s take an example to understand the problem,  Input: n = 12, k = 4Output: 8Explanation: Even elements in the range [1, n] : 2, 4, 6, 8, 10, 12The 4th smallest element is 8.Solution approach: The solution is simple as we need to find the kth ... Read More

Find kth node from Middle towards Head of a Linked List in C++

sudhir sharma
Updated on 25-Jan-2021 04:54:49

463 Views

In this problem, we are given a linked-list and a number k. Our task is to find kth node from Middle towards the Head of a Linked List. Let’s take an example to understand the problem,  Input: linked-list : 4 -> 2 -> 7 -> 1 -> 9 -> 12 -> 8 -> 10 -> 5, k = 2Output: 7Explanation: Middle node value is 9.The 2nd node from middle towards head is 7.Solution ApproachWe need to find kth element from the middle of the linked-list towards the beginning. For this we need to find the size of linked-list by traversing the linked-list from beginning ... Read More

Find k-th smallest element in given n ranges in C++

sudhir sharma
Updated on 25-Jan-2021 04:55:13

353 Views

In this problem, we are given n ranges and an integer k. Our task is to find k-th smallest element in given n ranges. We need to find the kth smallest elements from the array which is created after combining the ranges.Let’s take an example to understand the problem,  Input: ranges = {{2, 5}, {7, 9}, {12, 15}}, k = 9Output: 13Explanation: The array created is {2, 3, 4, 5, 7, 8, 9, 12, 13, 14, 15}The smallest elements is 13Solution Approach:A simple solution to the problem is by creating the array from all ranges and as it is created from range it is ... Read More

Find k maximum elements of array in original order in C++

sudhir sharma
Updated on 25-Jan-2021 04:52:14

171 Views

In this problem, we are given an array arr[] of n elements. Our task is to find k maximum elements of the array in original order. We need to find k maximum elements of the array and then print then as they were indexed originally.Let’s take an example to understand the problem, Input: arr[] = {5, 1, 3, 6, 2}, k  = 2Output: 5, 6Explanation: The largest two elements of the array are 6 and 5. But 5 comes before 6 in the original array hence we have printed in that way.Solution ApproachTo solve the problem, and print k elements in the original order.For ... Read More

Advertisements