Found 7347 Articles for C++

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

sudhir sharma
Updated on 28-Jan-2022 08:51:23

122 Views

In this problem, we are given an array aar[] of size n and another array del[] of size m. Our task is to find the largest after deleting the given elements. If the deletion of an element with multiple instances is required, delete the first instance of the element.Let's take an example to understand the problem, Input : arr[] = {3, 5, 1, 7, 9, 2}, del[] = {1, 9, 3} Output : 7Explanation −Array arr[] after deleting the elements : {5, 7, 2} Largest element of the array is 7Solution ApproachA simple solution the problem is by deleting all ... Read More

Find the Kth node in the DFS traversal of a given subtree in a Tree in C++

sudhir sharma
Updated on 28-Jan-2022 08:45:24

108 Views

In this problem, we are given a tree of size N, a node of the tree V and k. Our task is find the Kth node in the DFS traversal of a given subtree in a Tree.We need to find the kth node in the DFS traversal of the tree starting from vertex V.Let's take an example to understand the problem, Input :V = 2, k = 3Output : 4Explanation −The series is {1, 2, 3, 5, 6, 7} The 4th element is 5.Solution ApproachA simple solution to the problem is find the DFS traversal of the node V and ... Read More

Find the kth element in the series generated by the given N ranges in C++

sudhir sharma
Updated on 28-Jan-2022 08:27:50

187 Views

In this problem, we are given a N range of integer values between intervals L - R as an matrix range[N][2] and an integer value k. Our task is to find the kth element in the series generated by the given N ranges.Let's take an example to understand the problem, Input : ranges[][] = {{1, 3}, {5, 7}}, k = 4 Output : 5Explanation −The series is {1, 2, 3, 5, 6, 7} The 4th element is 5.Solution ApproachA simple solution to the problem is by creating a series of integers for the given ranges and then find the elements ... Read More

Find the k-th smallest divisor of a natural number N in C++

sudhir sharma
Updated on 28-Jan-2022 08:20:50

338 Views

In this problem, we are given two integer values N and k. Our task is to find the k-th smallest divisor of a natural number N.Let's take an example to understand the problem, Input : N = 15, k = 3 Output : 5Explanation −Factors of 15 are 1, 3, 5, 15 3rd smallest is 5Solution ApproachA simple solution to the problem is by finding the factors of the number and storing them in sorted manner and printing kth values.For sorting, we will loop till root(N) and check if N is divisible by i. And store the value of i ... Read More

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

sudhir sharma
Updated on 28-Jan-2022 08:07:45

117 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 smallest numbers after deleting the given elements.We need to print the first k smallest 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 : 2, 5Explanation −Array arr[] ... Read More

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

sudhir sharma
Updated on 28-Jan-2022 07:46:25

183 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
Updated on 28-Jan-2022 07:37:49

197 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

96 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

412 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

397 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

Advertisements