Found 7197 Articles for C++

Find the Initial Array from given array after range sum queries in C++

sudhir sharma
Updated on 28-Jan-2022 07:37:49

249 Views

In this problem, we are given an array res[] of size N. Our task is to find the Initial Array from given array after range sum queries.We need to find the starting array which will return the array rel[] on performing [s, e, val] query on it.Each [s, e, val] query is solved ass -> starting indexe -> ending indexval -> update value to be added to each element from s to e in array.Let's take an example to understand the problem, Input : rel[] = {7, 4, 8} Query[][] = {{1, 2, 1}, {0, 1, 3}} Output : {4, ... Read More

Find the index of the left pointer after possible moves in the array in C++

sudhir sharma
Updated on 28-Jan-2022 07:32:10

173 Views

In this problem, we are given an array arr[] of size N. Our task is to find the index of the left pointer after possible moves in the array.We have two pointers for the array, one left pointer and another right pointer.Left pointer starts at index 0 and the value is incremented.Right pointer starts at index (n-1) and the value is decremented.The value of a pointer increases if the sum traversed is lesser than other, i.e. if left pointer's sum is less than right pointer's sum, left pointer is increased otherwise right pointer is decreased. And sum's are updated.Let's take ... Read More

Find the index of first 1 in an infinite sorted array of 0s and 1s in C++

sudhir sharma
Updated on 28-Jan-2022 07:32:17

607 Views

In this problem, we are given an infinite array bin[] consisting of boolean values (only 0's and 1's) in sorted order. Our task is to find the index of first 1 in an infinite sorted array of 0's and 1's.Here, we have an infinite array which guarantees that there exists 1 in the array.Let's take an example to understand the problem, Input : bin[] = {0, 0, 0, 1, 1, ....} Output : 3Explanation −First 1 of the binary array is encountered at index 3.Solution ApproachTo solve the problem, we basically need to find the index of 1st 1 in ... Read More

Find the index of first 1 in a sorted array of 0's and 1's in C++

sudhir sharma
Updated on 28-Jan-2022 07:20:11

559 Views

In this problem, we are given an array bin[] consisting of boolean values (only 0's and 1's) in sorted order. Our task is to find the index of first 1 in a sorted array of 0's and 1's.Let's take an example to understand the problem, Input : bin[] = {0, 0, 0, 1, 1} Output : 3Explanation −First 1 of the binary array is encountered at index 3.Solution ApproachTo solve the problem, we basically need to find the index of 1st 1 in the array. For that we can use a searching technique.One approach can be using linear search, we ... Read More

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

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

2K+ 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

274 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

557 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

885 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

651 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

Advertisements