Server Side Programming Articles

Page 519 of 2109

Program to find a tree by updating values with left and right subtree sum with itself in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 330 Views

A binary tree can be transformed by updating each node's value to include the sum of all values in its left and right subtrees. This creates a new tree where each node contains its original value plus the total sum of all descendant nodes. .node-circle { fill: #e8f4f8; stroke: #2c5aa0; stroke-width: 2; } .node-text { font-family: Arial; font-size: 12px; text-anchor: middle; dominant-baseline: central; } .tree-line { stroke: #333; stroke-width: 2; } ...

Read More

Program to count number of unique palindromes we can make using string characters in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 351 Views

When working with palindromes, we need to understand that a palindrome reads the same forwards and backwards. To count unique palindromes from string characters, we use combinatorial mathematics to calculate arrangements of the first half of the palindrome. Algorithm Overview The key insight is that for a palindrome to be possible, at most one character can have an odd frequency (this becomes the middle character). We then calculate arrangements of the first half using factorial division ? Step-by-Step Approach Count frequency of each character in the string Check if palindrome formation is possible (at most ...

Read More

Program to calculate vertex-to-vertex reachablity matrix in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 411 Views

A vertex-to-vertex reachability matrix is a 2D matrix that shows whether there is a path between any two vertices in a graph. For a graph with n vertices, we create an n×n matrix M where: M[i, j] = 1 when there is a path between vertices i and j M[i, j] = 0 otherwise Problem Example Given a graph represented as an adjacency list, we need to find the reachability matrix. Consider this graph: 0 1 ...

Read More

Program to count number of ways we can throw n dices in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 314 Views

Sometimes we need to find the number of ways to throw n dice with a specific number of faces to achieve a target total. This is a classic dynamic programming problem that can be solved efficiently using bottom-up approach. Problem Statement Given n dice, each with a specific number of faces, find how many ways we can throw them to get a target total. Since the answer can be very large, return the result modulo 10^9 + 7. For example, if n = 2, faces = 6, and total = 8, there are 5 ways: (2, 6), ...

Read More

Program to find sum each of the diagonal path elements in a binary tree in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 240 Views

In this problem, we need to find the sum of elements along each diagonal path in a binary tree. A diagonal path moves from top-left to bottom-right, where moving to the right child continues the same diagonal, while moving to the left child starts a new diagonal level. Looking at the example tree structure: 12 8 15 3 10 ...

Read More

Program to sort each diagonal elements in ascending order of a matrix in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 602 Views

Matrix diagonal sorting is a problem where we need to sort each diagonal of a matrix from top-left to bottom-right in ascending order. Each diagonal runs parallel to the main diagonal and gets sorted independently. Understanding the Problem Given a matrix, we identify all diagonals that run from top-left to bottom-right. For example, in a 3x4 matrix, we have multiple diagonals starting from different positions. ...

Read More

Program to find number of moves to win deleting repeated integer game in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 234 Views

Suppose two friends Amal and Bimal are playing a game with a sorted list of numbers called nums. In this game in a single turn, Amal chooses any three numbers. Bimal removes one of them, and then Amal removes one of them. The list starts out with an odd number of elements. Here Amal wishes to minimize the number of turns required to make the list contain no repeated elements, Bimal wishes to maximize the number of turns. If Amal and Bimal act optimally, we have to find how many turns are needed for this game. So, if the ...

Read More

Program to make target by removing from first or last and inserting again in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 181 Views

In this problem, we have two strings S and T that are permutations of each other. We can perform an operation where we remove either the first or last character from S and insert it anywhere in the string. The goal is to find the minimum number of operations needed to convert S into T. Problem Understanding Given strings s = "zyvxw" and t = "vwxyz", we need to transform s into t using the allowed operations. The key insight is to find the longest common subsequence that appears in the same order in both strings, then calculate ...

Read More

Program to delete all leaves with even values from a binary tree in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 445 Views

Suppose we have a binary tree, we will repeatedly delete all leaves that have even values. After deleting all even-valued leaves, if only the root remains and it has an even value, that will be deleted as well. So, if the input is like ? 13 12 14 16 22 ...

Read More

Program to find number of ways we can decode a message in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 414 Views

Suppose we have a mapping like a = 1, b = 2, ... z = 26, and we have an encoded message string. We need to count the number of ways it can be decoded using dynamic programming. For example, if the input is message = "222", then the output will be 3, as this can be decoded in 3 ways: "bbb" (2-2-2), "bv" (2-22), and "vb" (22-2). Algorithm To solve this problem, we will follow these steps − Create a memo array of size message length + 1 initialized with zeros Set memo[0] = ...

Read More
Showing 5181–5190 of 21,090 articles
« Prev 1 517 518 519 520 521 2109 Next »
Advertisements