Find if K Bookings are Possible with Given Arrival and Departure Times in C++

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

316 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 Pair in Root to Leaf Path with Sum Equals to Node Data in C++

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

118 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 Subarray with 0 Sum in C++

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

385 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

Check if Given Matrix is Toeplitz in C++

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

261 Views

In this problem, we are given a 2D square matrix mat[][] of size n*n. Our task is to find if the given matrix is Toeplitz or not.Toeplitz matrix also known as diagonal matrix is a matrix in which the elements at the diagonal start from top-left corner to bottom-right corner.Let’s take an example to understand the problem, Input:          Mat[][] = {{3, 5, 1},                            {4, 3 ,2},                            {1, 2, 3}}Output: YesExplanation:  Diagonal : ... 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

363 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

Determine Page Rotation Possibility by Angle 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 Array Has Element Equal to Half of Array Sum in C++

sudhir sharma
Updated on 22-Jan-2021 13:53:43

187 Views

In this problem, we are given an array arr of sorted unique values. Our task is to find if array has an element whose value is half of array sum. Problem Description: For the array arr[], we need to find element x in the array such that the sum of all elements of array is equal to 2*X.Let’s take an example to understand the problem, Input: arr[] = {2, 4, 5, 6, 7}Output: NoExplanation: Sum = 2 + 4 + 5 + 6 + 7 = 24No element found.Solution Approach: To solve the problem, we simply need to find the elements which is half of the ... Read More

Check If a String Starts and Ends With Another String in C++

sudhir sharma
Updated on 22-Jan-2021 13:53:31

546 Views

In this problem, we are given two strings str and corStr. Our task is to find if a string starts and ends with another given string. Let’s take an example to understand the problem, Input: str = “abcprogrammingabc” conStr = “abc”Output: TrueSolution Approach: To solve the problem, we need to check if the string starts and ends with the conStr. For this, we will find the length of string and corStr. Then we will check if len(String) > len(conStr), if not return false.Check if prefix and suffix of size corStr are equal and check they contain corStr or not.Program to illustrate the working of ... Read More

Find If a Number is Divisible by Every Number in a List in C++

sudhir sharma
Updated on 22-Jan-2021 13:53:18

281 Views

In this problem, we are given a list of n numbers and a number. Our task is to find if a number is divisible by every number in a list. We need to check if the given number divides all elements of the list or not.Let’s take an example to understand the problem, Input: list[] = [4, 10 ,6, 5, 9] num = 5Output: NoExplanation:Elements 4, 6, 9 are not divisible by 5.Solution Approach: To solve the problem, we need to check if any element of the list is divisible by num. If every number of lists is divisible by num, return true else ... Read More

File Opening Modes: r versus r+ in C++

sudhir sharma
Updated on 22-Jan-2021 13:49:44

3K+ Views

File handling in programming languages is very important for the interaction of programming with the memory for accessing files and fetching data in it.Using a program, you can read data from a file as well as write data to the file and do a lot more functions.Here, we will see reading of data from a file.In programming, before performing any operation you need to open the file. And there are multiple modes to open a file in the programming language. The access to the file is based on the mode it is opened with.Here we will learn about the difference between ... Read More

Advertisements