Programming Articles - Page 1470 of 3366

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

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

327 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

121 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

380 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

Find if given matrix is Toeplitz or not in C++

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

272 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 array has an element whose value is half of array sum in C++

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

197 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

Find if a string starts and ends with another given string in C++

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

560 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

287 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

Find Height of Binary Tree represented by Parent array in C++

sudhir sharma
Updated on 22-Jan-2021 13:48:56

216 Views

In this problem, we are given an array arr[] of size n that denotes a tree. Our task is to find height of Binary Tree represented by Parent array. A Binary Search Tree (BST) is a tree in which all the nodes follow the below-mentioned properties −The value of the key of the left sub-tree is less than the value of its parent (root) node's key.The value of the key of the right subtree is greater than or equal to the value of its parent (root) node's key.Height of a tree is the number of nodes traversed when going from root node ... Read More

Fill array with 1's using minimum iterations of filling neighbors in C++

sudhir sharma
Updated on 22-Jan-2021 13:45:26

207 Views

 In this problem, we are given an array arr consisting of n elements that are either 0 or 1. Our task is to fill array with 1’s using minimum iterations of filling neighbors. Let’s take an example to understand the problem, Input: arr[] = {0, 1, 1, 0, 0, 1}Output: 1Solution Approach −To solve the problem, we need to know the fact that if 1 is present in a position it can convert two neighbouring 0’s to 1.If,                arr[i] is 1.Then,           arr[i-1] and arr[i+1] will be converted to 1.Using this, the ... 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