Found 7347 Articles for C++

Find the hypotenuse of a right angled triangle with given two sides in C++

sudhir sharma
Updated on 27-Jan-2022 11:07:53

1K+ Views

In this problem, we are given two integer values H and B defining height and base of a right angled triangle. Our task is to find the hypotenuse of a right angled triangle with given two sides.Right Angled Triangle is a special triangle whose two angles are at right angles.Let's take an example to understand the problem, Input : B = 5, H = 12 Output : 13.00Solution ApproachA simple solution to the problem is using the concept of pythagoras theorem to find the hypotenuse of a triangle using the base and height.Pythagoras Theorem States that the square of the ... Read More

Find the good permutation of first N natural numbers C++

sudhir sharma
Updated on 27-Jan-2022 11:01:35

172 Views

In this problem, we are an integer value N. Our task is to find the good permutation of first N natural numbers.permutation is an arrangement of all or part of a set of objects, with regard to the order of the arrangement.Good Permutation is a permutation in which $1\leqslant{i}\leqslant{N}$ and follows, $P_{pi}\:=\:i$$P_{p!}\:=\:i$Let's take an example to understand the problem, Input : N = 1 Output : -1Solution ApproachA simple solution to the problem is by finding permutations p such that pi = i.Then we will reconsider the equation to satisfy pi != i. So, for a value x such that ... Read More

Find the first repeating element in an array of integers C++

sudhir sharma
Updated on 27-Jan-2022 10:59:12

383 Views

In this problem, we are an array arr of n integer values. Our task is to find the first repeating element in an array of integers.We need to find the first integer value from the array which occurred more than once in the array.Let's take an example to understand the problem, Input : arr[] = {4, 1, 8, 9, 7, 2, 1, 6, 4} Output : 4Explanation −Integers with more than one occurrence are 4 and 1. 4's first occurrence is smaller than 1. Hence the answer is 4Solution ApproachA simple solution to the problem is using nested loops. We ... Read More

Find the first repeated word in a string in C++

sudhir sharma
Updated on 27-Jan-2022 10:50:35

1K+ Views

In this problem, we are a string str consisting of comma separated words. Our task is to find the first repeated word in a string.We need to find the first word 'string between two spaces' which gets repeated in the string.Let's take an example to understand the problem, Input : str = "C program are easy to program" Output : programSolution ApproachA simple solution to the problem is using hashmap data structure. To find the first repeated word, we will store each word and its count (number of times it appeared in the string ) in the hashmap. For this ... Read More

Find the first maximum length even word from a string in C++

sudhir sharma
Updated on 27-Jan-2022 10:43:34

749 Views

In this problem, we are a string str consisting of comma separated words. Our task is to find the first maximum length, even word, from a string.We need to find the largest word 'string between two spaces' whose length is maximum and even.Let's take an example to understand the problem, Input : str = "learn programming at TutorialsPoint" Output : TutorialsPointExplanation −The string with even length is TutorialsPoint. Solution ApproachA simple solution to the problem is by simply finding the string which has even length greater than the current string. Initialise the maxString length to 0.AlgorithamStep 1 − Iterate over ... Read More

Find the dimensions of Right angled triangle in C++

sudhir sharma
Updated on 27-Jan-2022 10:37:20

485 Views

In this problem, we are given two values H and A, denoting the hypotenuse and area of a right angled triangle. Our task is to find the dimensions of the Right angled triangle.Right Angled Triangle is a special type of triangle in which two sides intersect at right angles.Fig : Right Angle TriangleLet's take an example to understand the problem, Input : H = 7 , A = 8 Output : height = 2.43, base = 6.56Solution ApproachSolution to this problem can be found by using the mathematical formula for the values. And lets derive them here, $A\:=\:1/2^*h^*b$$H^2\:=\:h^2\:+\:b^2$Using the formula, ... Read More

Find the Deepest Node in a Binary Tree in C++

sudhir sharma
Updated on 27-Jan-2022 09:15:56

558 Views

In this problem, we are given a binary tree. Our task is to find the Deepest Node in a Binary Tree.Binary Tree is a special data structure used for data storage purposes. A binary tree has a special condition that each node can have a maximum of two children.Deepest node in a binary tree is the node which is at the maximum possible height in the tree.Let's take an example to understand the problem, Input :Output : 8Solution ApproachThere can be multiple ways to solve this problem, as you need to find the height and traverse the tree to the ... Read More

Find the average of first N natural numbers in C++

sudhir sharma
Updated on 27-Jan-2022 09:08:26

589 Views

In this problem, we are given a number n. Our task is to find the average of first N natural numbers.Average of numbers is defined as the sum of all numbers divided by the total number of numbers.Average of N natural numbers is defined as the sum of first N natural numbers divided by N.Let's take an example to understand the problem, Input : N = 23 Output : 12Explanation −1 + 2 + 3 + ... + 22 + 23 = 276 276 / 23 = 12Solution ApproachTo find the average of the number we will use the formula ... Read More

Find the arrangement of queue at given time in C++

sudhir sharma
Updated on 27-Jan-2022 09:02:17

154 Views

In this problem, we are given a string consisting of characters 'M' and 'F' only and a time t. Our task is to find the arrangement of the queue at a given time.The string defines people standing in a common queue to enter the bus. All males in the queue are so chivalrous that if they see a female behind them at any point of time they exchange places with them. There is t unit time left to enter the bus and each exchange takes one unit time. We need to find the positions when the bus comes by rearranging ... Read More

Find sum of the series 1+22+333+4444+... upto n terms in C++

sudhir sharma
Updated on 27-Jan-2022 08:49:02

417 Views

In this problem, we are given an integer value N. Our task is to find Sum of Series 1 + 22 + 333 + 4444 + 55555... upto n terms.Let's take an example to understand the problem, Input : N = 4 Output : 4800Explanation −1 + 22 + 333 + 4444 = 4800 Solution ApproachA simple approach to solve the problem is finding the general term of the series and then finding the sum till n terms. And calculating the sum using formula will reduce the time to O(1).The series is, 1 + 22 + 333 + 4444 + ... Read More

Advertisements