Found 7197 Articles for C++

Find K items with the lowest values in C++

sudhir sharma
Updated on 25-Jan-2021 04:51:25

130 Views

In this problem, we are given a list that consists of items and their values and an integer k. Our task is to find K items with the lowest values. Problem description: We need to find k items from the list that have the lowest value.Let’s take an example to understand the problem, Input: item-value = { {item1, 200}, {item2, 100}, {item3, 500}, {item4, 400}} k = 2Output: item1 , item2Explanation: Two elements with least value are item1 with 200 and item2 with 100.Solution ApproachA solution to the problem is by finding k items with the least value greedily. We will first sort the item ... Read More

Find integers that divides maximum number of elements of the array in C++

sudhir sharma
Updated on 25-Jan-2021 04:51:49

414 Views

In this problem, we are given an array arr[] of n integers.Our task is to find integers that divides maximum number of elements of the array. Problem Description: We need to find a number p which can divide the maximum number of elements of the array. In case, there are more than one such element we will return the smaller one.Let’s take an example to understand the problem,  Input: arr[] = {4, 5, 6, 7, 8}Output: 2Explanation: The element 2 divides {4, 6, 8}.Solution ApproachA simple solution to the problem is by looping through the array and then for each element of the array, divide each ... Read More

Find index of an extra element present in one sorted array in C++

sudhir sharma
Updated on 22-Jan-2021 14:08:10

712 Views

In this problem, we are given two sorted arrays arr1 and arr2 of size n  and n+1 with all elements the same except the extra element. Our task is to find index of an extra element present in one sorted array. Problem Description: We need to find the index of an element from the n+1 size array which is not present in an array of size n.Let’s take an example to understand the problem, Input:         arr1[n]  = {3, 5, 7, 8, 9, 12}                   arr2[n+1] = {3, 4, 5, 7, 8, 9, ... Read More

Find if two people ever meet after same number of jumps in C++

sudhir sharma
Updated on 22-Jan-2021 14:06:52

303 Views

In this problem, we are given four integers denoting the starting points and jumps taken by each in the race. Our task is to find if two people ever meet after same number of jumps.  Problem Description: Here, we need to check if two persons starting at points p1 and p2 taking jumps j1 and j2 will be at some point in the path or not.Let’s take an example to understand the problem, Input: p1 = 5, p2 = 9, j1 = 4, j2 = 2Output: YesExplanation:After first jump, p1 = 9, p2 = 11After second jump, p1 = 13, p2 = 13Solution Approach: For ... Read More

Find if there is any subset of size K with 0 sum in an array of -1 and +1 in C++

sudhir sharma
Updated on 22-Jan-2021 14:06:39

170 Views

In this problem, we are given an array arr[] consisting of only 1 and -1 and an integer value k. Our task is to find if there is any subset of size K with 0 sum in an array of -1 and +1. Let’s take an example to understand the problem, Input: arr[] = {-1, 1, -1, -1, 1 , 1, -1}, k = 4Output: YESExplanation:Subset of size 4, {-1, 1, -1, 1}. Sum = -1 + 1 - 1 + 1 = 0Solution Approach: We need to check if there exists any subset of size k whose sum is equal to 0. As ... Read More

Find if there is a subarray with 0 sum in C++

sudhir sharma
Updated on 22-Jan-2021 14:05:59

388 Views

In this problem, we are given an array arr[] of size n consisting of integer values. Our task is to find if there is a subarray with 0 sum. We need to check whether the given array contains a sub-array in which the sum of all elements is equal to 0.Let’s take an example to understand the problem, Input: arr[] = {3, 1, -2, 1, 4, 5}Output: YesExplanation: Subarray {1, -2, 1} has the sum of all values equal to 0.Solution Approach: A simple solution to the problem by considering all subarrays and checking the sum of all elements is equal to 0.Another solution to ... Read More

Find if there is a pair in root to a leaf path with sum equals to root's data in C++

sudhir sharma
Updated on 22-Jan-2021 14:06:11

121 Views

In this problem, we are given a Binary Tree. And we need to find if there is a pair in root to a leaf path with sum equals to root’s data. We need to check if there exists a pair of nodes that lies between root node to leaf nodes such that the sum of values of pairs is equal to the root node’s value.Let’s take an example to understand the problem,   Input:Output: YesExplanation: Root node value 7Pairs with sum value equal to root node, (2, 5), (1, 6).Solution Approach: We need to traverse the tree and find the pairs using hashing.For this we ... Read More

Find if k bookings possible with given arrival and departure times in C++

sudhir sharma
Updated on 22-Jan-2021 14:06:24

318 Views

In this problem, we are given two arrays  consisting of N values denoting arrival and departure at hotel and an integer k. Our task is to find if k bookings are possible with given arrival and departure times. Problem Description: Here, we need to check if the hotel with k rooms is able to accommodate all arrivals and departures.Let’s take an example to understand the problem, Input:         Arrivals :     {1 4 5 7}Departures : {3 5 6 9}             K = 1Output: YesSolution approach:To solve the problem, we will store arrival and departure for ... Read More

Find if it's possible to rotate the page by an angle or not in C++

sudhir sharma
Updated on 22-Jan-2021 14:02:15

107 Views

In this problem, we are given coordinates of three points that lie on a page. Our task is to find if it’s possible to rotate the page by an angle or not.  The rotation of the page is made in such a way that the new position of ‘x’ is the old position of ‘y’, the new position of ‘y’ is the old position of ‘z’. And print “Yes” or “No” based on the rotation.Let’s take an example to understand the problem, Input: x = (0, 1), y = (1, 0), z = (0, -1)Output: YesExplanation:We can rotate the page by 90o.Solution Approach: We ... Read More

Find if given number is sum of first n natural numbers in C++

sudhir sharma
Updated on 22-Jan-2021 14:02:30

366 Views

In this problem, we are given a number num. Our task is to find if the given number is the sum of first n natural numbers.  Problem Description: Here, we need to check whether the given number is the sum of first n natural numbers.Let’s take an example to understand the problem, Input: num = 55Output: yes, 10Explanation:55 is the sum of the first 10 natural numbers, 1+2+3+4+5+6+7+8+9+10.Solution Approach: A simple approach to solving the problem is finding the sum of n natural numbers until it becomes equal to or greater than num.If the sum is equal to num, return n.If at any value of ... Read More

Advertisements