In this problem, we are given a string str consisting of english alphabets. Our task is to find the letter's position in the Alphabet using the Bit operation. Problem Description: Here, we will return the position of each character of the string as it is in english alphabets.The characters of the string are case-insensitive i.e. “t” and “T” are treated the same.Let’s take an example to understand the problem, Input: str = “Tutorialspoint”Output: 20 21 20 15 18 9 1 12 19 16 15 9 14 20 Solution ApproachA simple solution to find a character's position is by finding its logical AND operation with 31.Program ... Read More
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
In this problem, we are given k different arrays of different sizes. Our task is to find m-th smallest value in k sorted arrays. Problem Description: Here, we need to find m-th smallest element of the merged array of all k arrays.Let’s take an example to understand the problem, Input: m = 4 arr[][] = { {4 , 7}, {2, 5, 6}, ... Read More
In this problem, we are given a 2-D matrix of size nXm consisting of 0’s and 1’s only. Our task is to find the length of the largest region in the Boolean Matrix. Problem Description: If a cell contains 1, it is a filled Cell. We need to find the length of connected cells which are connected adjacent to each other horizontally or vertically or diagonally. Let’s take an example to understand the problem, Input: matrix[4][5]{ {0, 1, 1, 0, 1}, {0, 0, 1, 1, 1}, {1, 0, 0, 0, 0}, {1, 0, 1, 0, 1} }Output: 6Explanation: The number of connected filled ... Read More
In this problem, we are given a linked list that might contain loops. Our task is to find the length of the loop in the linked list. Problem Description: we need to count the number of nodes in the loop if it contains a loop otherwise return -1.Let’s take an example to understand the problem, Input: linked-list :Output: 8Solution ApproachTo solve the problem, we first need to check whether the linked list contains a loop. An approach to check this is using Floyd’s Cycle Finding Algorithm. In Floyd’s Cycle Finding Algorithm, we will traverse the linked list using two pointers. One slowPointer that increases by 1 ... Read More
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
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
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
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
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