Found 33676 Articles for Programming

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

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

386 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

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 if k bookings 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 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

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

Find if given matrix is Toeplitz or not 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 array has an element whose value is 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

Find if a string starts and ends with another given 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

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

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

207 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

Advertisements