Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Programming Articles
Page 220 of 2544
Count the nodes in the given tree whose sum of digits of weight is odd in C++
Given a binary tree with weights of its nodes. The goal is to find the number of nodes that have weights such that the sum of digits in that weights add up to an odd number. If weight is 12 then the digit sum is 3 which is odd so this node will be counted.For ExampleInputThe tree which will be created after inputting the values is given below −OutputCount of nodes in the given tree whose sum of digits of weight is odd are: 2Explanationwe are given with the tree node and the weights associated with each node. Now we ...
Read MoreCheck if it is possible to sort an array with conditional swapping of adjacent allowed in Python
Suppose we have an unordered array of numbers called nums and all elements are in range 0 to n-1. We can swap adjacent elements in nums as many times as required but only when the absolute difference between these element is 1. We have to check whether we can sort the nums or not.So, if the input is like nums = [1, 0, 3, 2, 5, 4], then the output will be True as we can swap these pairs [(1, 0), (3, 2), (5, 4)] to sort [0, 1, 2, 3, 4, 5].To solve this, we will follow these steps ...
Read MoreCount the nodes in the given tree whose weight is a power of two in C++
Given a binary tree with weights of its nodes. The goal is to find the number of nodes that have weights such that the number is power of two. If weight is 32 then it is 25 so this node will be counted.For ExampleInputThe tree which will be created after inputting the values is given below −OutputCount the nodes in the given tree whose weight is a power of two are: 3Explanationwe are given with the tree node and the weights associated with each node. Now we calculate the power of each and every weight and check whether it can ...
Read MoreCheck if it is possible to sort the array after rotating it in Python
Suppose we have a list of numbers called nums, we have to check whether we can sort nums or not by using rotation. By rotation we can shift some contiguous elements from the end of nums and place it in the front of the array.So, if the input is like nums = [4, 5, 6, 1, 2, 3], then the output will be True as we can sort by rotating last three elements and send them back to first.To solve this, we will follow these steps −n := size of numsif nums is sorted, thenreturn Trueotherwise, status := Truefor i ...
Read MoreCheck if it is possible to survive on Island in Python
Suppose there is an island. There is only one store in that location, this store remains open always except Sunday. We have following values as input −N (Maximum number of food someone can buy each day).S (Number of days someone is required to survive).M (Number of food required each day to survive).If it is Monday, and we need to survive for the next S number of days. We have to check whether we can survive or not, if we can find the minimum number of days on which we need to buy food, so that we can survive the next ...
Read MoreCount the nodes of the tree whose weighted string contains a vowel in C++
Given a binary tree with weights of its nodes as strings. The goal is to find the number of nodes that have weights such that the string contains a vowel. If weight is ‘aer’ then it has vowels ‘a’ and ‘e’ so the node will be counted.For ExampleInputThe tree which will be created after inputting the values is given below −OutputCount the nodes of the tree whose weighted string contains a vowel are: 5Explanationwe are given with the tree nodes and the string weights associated with each node. Now we check whether the string of nodes contains vowels or not.NodeWeightvowelsyes/no2aeeyes1bcdNo ...
Read MoreCheck if it is possible to transform one string to another in Python
Suppose we have two strings s and t, t is in uppercase. We have to check whether we can convert s to t by performing following operations.Convert some lowercase letters uppercase.Remove all of the lowercase letters.So, if the input is like s = "fanToM", t = "TOM", then the output will be True as we can change ‘o’ to ‘O’ then remove all other lowercase letters from s to make it t.To solve this, we will follow these steps −n := size of s, m := size of tdp:= a matrix of size (m + 1)x(n + 1) and fill ...
Read MoreCheck if it possible to partition in k subarrays with equal sum in Python
Suppose we have an array of numbers called nums, and also have another value K. We have to check whether we can partition the array nums into K contiguous subarrays such that the elements sum of each subarrays is equal.So, if the input is like nums = [2, 5, 3, 4, 7] k = 3, then the output will be True as we can make three partitions like [(2, 5), (3, 4), (7)] all have equal sum 7.To solve this, we will follow these steps −n := size of numscumul_sum := cumulative sum of all elements in numstotal_sum := cumul_sum[n ...
Read MoreCheck if item can be measured using a scale and some weights in Python
Suppose we have some weights like a^0, a^1, a^2, …, a^100, here 'a' is an integer, and we also have a weighing scale where weights can be put on both the sides of that scale. We have to check whether a particular item of weight W can be measured using these weights or not.So, if the input is like a = 4, W = 17, then the output will be True the weights are a^0 = 1, a^1 = 4, a^2 = 16, we can get 16 + 1 = 17.To solve this, we will follow these steps −found := ...
Read MoreCheck if LCM of array elements is divisible by a prime number or not in Python
Suppose we have an array called nums and another value k, we have to check whether LCM of nums is divisible by k or not.So, if the input is like nums = [12, 15, 10, 75] k = 10, then the output will be True as the LCM of the array elements is 300 so this is divisible by 10.To solve this, we will follow these steps −for i in range 0 to size of nums - 1, doif nums[i] is divisible by k, thenreturn Truereturn FalseExampleLet us see the following implementation to get better understanding −def solve(nums, k) : ...
Read More