Found 7197 Articles for C++

Find maximum (or minimum) in Binary Tree in C++

sudhir sharma
Updated on 25-Jan-2021 05:20:42

1K+ Views

In this problem, we are given a binary tree. Our task is to Find maximum (or minimum) in Binary Tree. Problem Description: We need to find the nodes of the binary tree that have maximum and minimum value in the binary tree.Let’s take an example to understand the problem,  Input: Output: max = 9 , min = 1Solution ApproachWe need to find the max node of the binary tree. We will do this by traversing the pointer until we reach the leaf node and then find the maximum node of the tree.Program to illustrate the working of our solution, ExampleLive Demo#include using namespace ... Read More

Find max of two Rational numbers in C++

sudhir sharma
Updated on 25-Jan-2021 05:12:19

197 Views

In this problem, we are given two Rational Numbers. Our task is to find max of two Rational numbers. Here, the rational numbers are in the form of p/q.Let’s take an example to understand the problem,  Input: rat1 = 5/4, rat2 = 3/2Output: 3/2Explanation: 5/4 = 1.253/2 = 1.5Solution Approach −A simple solution to the problem is by using a method similar to the one we used to perform in school.For this, we will find the L.C.M of the denominator. And then multiply the numerator based on the denominators value. Then for the common denominator, the rational number with maximum numerator value is the ... Read More

Find m-th summation of first n natural numbers in C++

sudhir sharma
Updated on 25-Jan-2021 05:11:42

463 Views

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

Find m-th smallest value in k sorted arrays in C++

sudhir sharma
Updated on 25-Jan-2021 05:11:04

250 Views

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

Find M-th number whose repeated sum of digits of a number is N in C++

sudhir sharma
Updated on 25-Jan-2021 05:13:26

156 Views

In this problem, we are given two positive numbers N and M. Our task is to find the M-th number whose repeated sum of digits of a number is N. Problem description: Here, we need to find the Mth number whose sum of digits till the sum becomes single digit is equal to N.Let’s take an example to understand the problem,  Input: N = 4 M = 6Output: 49Solution ApproachA simple solution of the problem, is by finding all numbers and count the number whose sum of digits is N, and return m-th number.Another solution to the problem is using formula to find M-th ... Read More

Find longest length number in a string in C++

sudhir sharma
Updated on 25-Jan-2021 05:13:50

440 Views

In this problem, we are given a string str consisting of character and alphabets only. Our task is to find the longest length number in a string. Problem Description: we need to find the length of the number i.e. consecutive numerical characters in the string.Let’s take an example to understand the problem,  Input: str = “code001tutorials34124point”Output: 34124Explanation:  Numbers in the string are001 - size 334124 - size 5Solution ApproachA simple solution to the problem is by traversing the sting and finding the number’s length and its starting index. We will store the starting position and count of characters in the string for each number ... Read More

Find letter's position in Alphabet using Bit operation in C++

sudhir sharma
Updated on 25-Jan-2021 05:12:08

861 Views

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

Find length of the largest region in Boolean Matrix in C++

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

630 Views

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

Find length of loop in linked list in C++

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

513 Views

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

Find length of Diagonal of Hexagon in C++

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

210 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

Advertisements