Programming Articles - Page 811 of 3363

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

286 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

600 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

276 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

335 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

277 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

206 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

649 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

596 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

312 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

Advertisements