Found 7347 Articles for C++

Find length of Diagonal of Hexagon in C++

sudhir sharma
Updated on 25-Jan-2021 05:03:38

95 Views

In this problem, we are given an integer n denoting the length of the side of a regular hexagon. Our task is to Find length of Diagonal of Hexagon.Problem Description: Here, we have the side of a regular hexagon. And we need to find the length of the diagonal of the hexagon.Let’s take an example to understand the problem,  Input: a = 7Output: 12.11Solution ApproachTo solve the problem and find the length of diagonal which is given by the mathematical formula, Diagonal = 1.73 * aLet’s derive the formula, Here, we have a regular polygon of length a.The angle between the diagonal and ... Read More

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

197 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

835 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

126 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

281 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

242 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

132 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

140 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

346 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

271 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

Advertisements