C++ Articles

Page 489 of 597

Find the k largest numbers after deleting the given elements in C++

sudhir sharma
sudhir sharma
Updated on 28-Jan-2022 380 Views

In this problem, we are given an array arr[] of size n, array del[] of size m, and an integer k. Our task is to find the k largest numbers after deleting the given elements.We need to print the first k largest elements from the array arr[] found after deleting all elements present in the del[] array. If two instances are present in the array delete the first instance.Let's take an example to understand the problem, Input : arr[] = {3, 5, 1, 7, 9, 2}, del[] = {1, 9, 3}, k = 2 Output : 7, 5Explanation −Array arr[] ...

Read More

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

sudhir sharma
sudhir sharma
Updated on 28-Jan-2022 311 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
sudhir sharma
Updated on 28-Jan-2022 241 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 a sorted array of 0's and 1's in C++

sudhir sharma
sudhir sharma
Updated on 28-Jan-2022 648 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
sudhir sharma
Updated on 27-Jan-2022 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 first repeating element in an array of integers C++

sudhir sharma
sudhir sharma
Updated on 27-Jan-2022 664 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
sudhir sharma
Updated on 27-Jan-2022 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
sudhir sharma
Updated on 27-Jan-2022 958 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 Deepest Node in a Binary Tree in C++

sudhir sharma
sudhir sharma
Updated on 27-Jan-2022 801 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
sudhir sharma
Updated on 27-Jan-2022 1K+ 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
Showing 4881–4890 of 5,962 articles
« Prev 1 487 488 489 490 491 597 Next »
Advertisements