Server Side Programming Articles - Page 1472 of 2650

Program to remove all nodes with only one child from a binary tree in Python?

Arnab Chakraborty
Updated on 10-Nov-2020 08:51:15

710 Views

Suppose we have a binary tree root; we have to remove all nodes with only one child.So, if the input is likethen the output will beTo solve this, we will follow these steps:Define a method called solve(), this will take tree rootif root is null, thenreturn rootif left of root is null and right of root is null, thenreturn rootif left of root is null, thenreturn solve(right of root)if right of root is null, thenreturn solve(left of root)left of root := solve(left of root)right of root := solve(right of root)return rootExampleclass TreeNode:    def __init__(self, data, left = None, right ... Read More

Program to find length of longest possible stick in Python?

Arnab Chakraborty
Updated on 10-Nov-2020 08:47:08

289 Views

Suppose we have a list of integers sticks. Here each element in the list represents a stick with two ends, these values are between 1 and 6. These are representing each end. We can connect two sticks together if any of their ends are same. The resulting stick's ends will be the leftover ends and its length is increased. We have to find the length of the longest stick possible.So, if the input is like sticks = [ [2, 3], [2, 4], [3, 5], [6, 6] ], then the output will be 3, as we can connect [2, 3] and ... Read More

Program to find a matrix for each condominium's height is increased to the maximum possible height in Python?

Arnab Chakraborty
Updated on 10-Nov-2020 08:44:10

282 Views

Suppose we have a 2D matrix, where matrix [r, c] represents the height of a condominium in a city. The west-east skyline is visible by taking the maximum of each row in the matrix. And the north-south skyline can be visible by taking the maximum of each column. We have to find a new matrix where each condominium's height is increased to the maximum possible height while keeping the same west-east and north-south skyline.So, if the input is like23456789104447778910as the west-east skyline is [4, 7, 10] and north-south skyline is [8, 9, 10]. We can increase everything in the first ... Read More

Program to find sum of concatenated pairs of all each element in a list in Python?

Arnab Chakraborty
Updated on 10-Nov-2020 08:41:57

589 Views

Suppose we have a list of numbers called nums. We have to find the sum of every concatenation of every pair of numbers in nums. Here the pair (i, j) and pair (j, i) are considered different.So, if the input is like nums = [5, 3], then the output will be 176, as We have the following concatenations: (nums[0] + nums[0]) = (5 concat 5) = 55, (nums[0] + nums[1]) = (5 concat 3) = 53, (nums[1] + nums[0]) = (3 concat 5) = 35, (nums[0] + nums[0]) = (3 concat 3) = 33, then the sum is 55 + ... Read More

Program to find length of concatenated string of unique characters in Python?

Arnab Chakraborty
Updated on 10-Nov-2020 08:36:22

271 Views

Suppose we have a list of strings words. We have to make a string that is constructed by concatenating a subsequence of words such that each letter is unique. We have to finally find the length of the longest such concatenation.So, if the input is like words = ["xyz", "xyw", "wab", "cde"], then the output will be 9, as we cannot pick any word since they contain duplicate characters.To solve this, we will follow these stepsans := 0Define a function recur() . This will take i:= 0, cur:= blank stringif i is same as size of words , then   ... Read More

Program to find minimum number of groups in communication towers in C++?

Arnab Chakraborty
Updated on 10-Nov-2020 08:33:18

226 Views

Suppose we have a 2D binary matrix where 1 represents a communication tower, and 0 represents an empty cell. The towers can communicate in the following ways: 1. If tower A, and tower B are either on the same row or column, they can communicate with each other. 2. If tower A can talk with tower B and B can talk with C, then A can talk to C (transitive property). We have to find the total number of tower groups there are (here a group is a list of towers that can talk with each other).So, if the input ... Read More

Program to find angle between hour and minute hands of a clock in C++?

Arnab Chakraborty
Updated on 10-Nov-2020 08:26:28

2K+ Views

Suppose we have two values hours and minutes. We have to find a smaller angle formed between the hour and the minute hand.So, if the input is like hour = 12 minutes = 45, then the output will be 112.5To solve this, we will follow these steps:if h = 12, then set h := 0if m = 60, then set m := 0hAngle := 0.5 * (60h) + mmAngle := 6mret := |hAngle - mAngle|return minimum of ret and (360 – ret)Let us see the following implementation to get better understanding:Example Live Demo#include using namespace std; class Solution { public: ... Read More

Program to find maximum number of courses we can take based on interval time in Python?

Arnab Chakraborty
Updated on 10-Nov-2020 08:24:08

272 Views

Suppose we have a list of intervals in the form [start, end], this is representing the start and end time of a course. We have to find the maximum number of courses we can take, assuming we can only take one course at a time and the start of a course needs to be later than the end of the last course.So, if the input is like times = [[3, 6], [6, 9], [7, 8], [9, 11]], then the output will be 3, as we can take courses [[3, 6], [7, 8], [9, 11]]To solve this, we will follow these ... Read More

Program to find number of boxes that form longest chain in Python?

Arnab Chakraborty
Updated on 10-Nov-2020 08:21:39

236 Views

Suppose we have a list of boxes, here each entry has two values [start, end] (start < end). We can join two boxes if the end of one is equal to the start of another. We have to find the length of the longest chain of boxes.So, if the input is like blocks = [ [4, 5], [5, 6], [4, 8], [1, 2], [2, 4] ], then the output will be 4, as we can form the chain: [1, 2], [2, 4], [4, 5], [5, 6]To solve this, we will follow these steps:if boxes are empty, thenreturn 0sort the list ... Read More

Program to check whether first player win in candy remove game or not in Python?

Arnab Chakraborty
Updated on 10-Nov-2020 08:17:11

216 Views

Suppose we have a list of numbers called candies and someone is playing a game against his/her friend. In each round, a player can remove any two consecutive candies with the same value. And whoever can not pick up a candy loses and that player1 start first, we have to check whether player1 will win or not.So, if the input is like nums = [2, 2, 5], then the output will be True, as if player1 picks the 2s then the other player cannot pick any candies.To solve this, we will follow these steps:stack := a new stackturns := 0for ... Read More

Advertisements