Programming Articles

Page 233 of 2544

Nested List Weight Sum II in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 347 Views

Given a nested list of integers, return the sum of all integers in the list weighted by their depth. Each element is either an integer, or a list -- whose elements may also be integers or other lists. Different from the previous question where weight is increasing from root to leaf, now the weight is defined from bottom up. i.e., the leaf level integers have weight 1, and the root level integers have the largest weight.So, if the input is like [[1, 1], 2, [1, 1]], then the output will be 8, as four 1's at depth 1, one 2 ...

Read More

Program to traverse binary tree using list of directions in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 316 Views

Suppose we have a binary tree and a list of strings moves consisting of "R"(Right), "L"(Left) and "U"(Up). Starting from root, we have to traverse the tree by performing each move in moves where: "R" indicates traverse to the right child. "L" indicates traverse to the left child. "U" indicates traverse to its parent.So, if the input is like["R", "R", "U", "L"], then the output will be 3To solve this, we will follow these steps −past := a new listfor each move in moves, doinsert root at the end of pastif move is same as "L", thenroot := left of ...

Read More

Program to find maximum number enemies will be killed to place a bomb in C++?

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 514 Views

Suppose we have a 2D matrix of three different values, 2s, 1s, and 0s, where a 2 represents an enemy, 1 represents a wall and 0 represents an empty cell. We have to find the maximum enemies we can kill using one bomb. The bomb kills all the enemies in the same row and column from the planted point until it hits the wall. And we can put bombs only on blank spaces.So, if the input is likethen the output will be 3, as we can place the bomb at the green box to kill maximum 3 enemies.ret := 0n ...

Read More

Find Leaves of Binary Tree in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 712 Views

Suppose we have a binary tree. We will collect and remove all leaves and repeat until the tree is empty.So, if the input is likethen the output will be [[4, 5, 3], [2], [1]]To solve this, we will follow these steps −Define one map szDefine one 2D array retDefine a function dfs(), this will take node, if node is null, then −sz[val of node] := 1 + maximum of dfs(left of node) and dfs(right of node)if size of ret < sz[val of node], then −Define an array tempinsert temp at the end of retinsert val of node at the end ...

Read More

Program to find maximum profit we can get by buying and selling stocks with a fee in Python?

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 233 Views

Suppose we have a list of stock prices of a company in chronological sequence, and also have the transaction fee for one sell transaction. We have to find the maximum profit we could have made from buying and selling that stock any number of times. We must buy before we can sell it.So, if the input is like prices = [2, 10, 4, 8] fee = 3, then the output will be 6, as we can buy at 2 and sell at 10 and incur a fee of 3, so profit is 5. Then we buy at 4 and sell ...

Read More

Plus One Linked List in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 288 Views

Suppose we have a non-negative integer represented as non-empty a singly linked list of digits, now we have to plus one to the integer. We may assume the integer do not contain any leading zero, except the number 0 itself. In the linked list the most significant digit is at the head of the list.So, if the input is like [1, 2, 3], then the output will be [1, 2, 4]To solve this, we will follow these steps −if head is null, then −return headcurr = headreq = NULLwhile curr is non-zero, do −if val of curr is not equal ...

Read More

Program to count number of valid triangle triplets in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 920 Views

Suppose we have an array of numbers, we have to find the number of triplets chosen from the array that can make triangles if we take them as side lengths of a triangle. So if the input is like [2, 2, 3, 4], then the result will be 3 as there are three triplets [2, 3, 4] using first 2, [2, 3, 4] using second 2, and [2, 2, 3].To solve this, we will follow these steps −ret := 0, n := size of nums, sort numsfor i in range n − 1 down to 0right := i − 1, ...

Read More

Program to count number of swaps required to change one list to another in Python?

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 466 Views

Suppose we have two lists of numbers L1 and L2, the length of each list is n and each value is unique to its list, and values are in range 1 to n, we have to find the minimum number of adjacent swaps required to transform L1 to L2.So, if the input is like L1 = [0, 1, 2, 3] L2 = [2, 0, 1, 3], then the output will be 2, as we can swap 1 and 2, L1 will be [0, 2, 1, 3], and then 0 and 2, L1 will be [2, 0, 1, 3], this is ...

Read More

Range Addition in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 264 Views

Suppose we have an array of size n and that is initialized with 0's and we also have a value k, we will perform k update operations. Each operation will be represented as triplet: [startIndex, endIndex, inc] which increments each element of subarray A[startIndex ... endIndex] (startIndex and endIndex inclusive) with inc. We have to find the modified array after all k operations were executed.So, if the input is like length = 5, updates = [[1, 3, 2], [2, 4, 3], [0, 2, -2]], then the output will be [- 2, 0, 3, 5, 3]To solve this, we will follow ...

Read More

Program to find possible number of palindromes we can make by trimming string in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 241 Views

Suppose we have a string s, we have to find the number of ways we can obtain a palindrome by trimming the left and right sides of s.So, if the input is like s = "momo", then the output will be 6, as You can get ["mom", "omo", "o", "o", "m", "m", "o")To solve this, we will follow these steps −Define a function expand() . This will take i, j, sc := 0while i >= 0 and j < size of s and s[i] is same as s[j], doi := i − 1, j := j + 1c := c ...

Read More
Showing 2321–2330 of 25,433 articles
« Prev 1 231 232 233 234 235 2544 Next »
Advertisements