Server Side Programming Articles

Page 523 of 2109

Program to remove all nodes from BST which are not in range in Python

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

In this tutorial, we'll learn how to remove all nodes from a Binary Search Tree (BST) that are not within a given range [low, high]. This is a common tree pruning problem that uses the BST property to efficiently eliminate nodes. Problem Statement Given a BST and two values low and high, we need to delete all nodes that are not between [low, high] (inclusive). The resulting tree should maintain the BST property. Original BST 5 1 9 ...

Read More

Program to check whether different brackets are balanced and well-formed or not in Python

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

Checking whether brackets are balanced is a classic programming problem. A string of brackets is considered balanced when every opening bracket has a corresponding closing bracket in the correct order. For example, the string "([()()]{[]})()" is balanced because each opening bracket matches with its corresponding closing bracket properly. Algorithm Approach We use a stack data structure to solve this problem efficiently ? Create an empty stack to store opening brackets Create a dictionary mapping each closing bracket to its opening counterpart For each character in the string: If it's a closing bracket, check if ...

Read More

Program to check whether parentheses are balanced or not in Python

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

Checking whether parentheses are balanced is a fundamental programming problem. A string has balanced parentheses if every opening parenthesis ( has a corresponding closing parenthesis ) and they appear in the correct order. So, if the input is like s = "(()())(())", then the output will be True because all parentheses are properly matched. Algorithm Approach To solve this problem, we will follow these steps − Initialize a counter open_count to track open parentheses For each character in the string: ...

Read More

Program to count number of unique binary search tree can be formed with 0 to n values in Python

Farhan Muhamed
Farhan Muhamed
Updated on 25-Mar-2026 341 Views

In this article, we will discuss a problem that involves counting the number of unique binary search tree (BSTs) that can be formed with a given number of nodes. We will explain the problem, test cases, its solution, and provide a Python implementation. Problem Statement Given an integer n, we need to find how many structurally unique BSTs can be formed using values from 1 to n. This is known as the Catalan number problem in computer science. Example 1 Input: n = 3 Output: 5 Explanation: With 3 nodes (values 1, 2, ...

Read More

Program to find contiguous intervals of a unique array in Python

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

Suppose we have a list of unique numbers called nums. We have to find a sorted 2D matrix of numbers where each list represents an inclusive interval summarizing numbers that are contiguous in nums. So, if the input is like nums = [10, 11, 12, 15, 16, 17, 28, 30], then the output will be [[10, 12], [15, 17], [28, 28], [30, 30]], as in the list [10 to 12], [15 to 17] are contiguous, and 28 and 30 are there, they are represented as [28 to 28] and [30 to 30]. Algorithm To solve this, we ...

Read More

Program to check string contains consecutively descending string or not in Python

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

Suppose we have a string s with some digits, we have to check whether it contains consecutively descending integers or not. So, if the input is like s = "99989796", then the output will be True, as this string is holding [99, 98, 97, 96] Algorithm To solve this, we will follow these steps: Define a function helper(). This will take pos, prev_num if pos is same as n, then return True num_digits := digit count of prev_num for i in range num_digits - 1 to num_digits, do if s[from index ...

Read More

Program to check we can visit any city from any city or not in Python

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

Suppose we have n cities represented as numbers in range [0, n) and we also have a list of one-way roads that connects one city to another. We have to check whether we can reach any city from any city or not. So, if the input is like n = 3, roads = [[0, 1], [0, 2], [1, 0], [1, 2], [2, 0], [2, 1]], then the output will be True, as you can go from any city to any other city through the available roads. Approach To solve this problem, we need to check if the ...

Read More

Program to check whether a binary tree is complete or not in Python

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

Suppose we have a binary tree; we have to check whether this is a complete binary tree or not. A complete binary tree is one where all levels are completely filled except possibly the last level, and all nodes in the last level are as far left as possible. So, if the input is like ? 9 7 10 ...

Read More

Program to count minimum number of operations to flip columns to make target in Python

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

Suppose we have a matrix M and a target matrix T with the same number of rows and columns. We can perform an operation where we flip a particular column in the matrix so that all 1s become 0s and all 0s become 1s. If we can reorder the matrix rows for free, we need to find the minimum number of operations required to turn M into T. If there is no solution, return -1. Problem Example Given matrices M and T ? Matrix M: 00 10 11 Target T: 01 10 11 ...

Read More

Program to count number of operations required to all cells into same color in Python

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

When we have a two-dimensional matrix where each cell contains a value representing its color, we need to find the minimum number of operations to make all cells the same color. Adjacent cells (top, bottom, left, right) with the same color form connected groups, and in each operation, we can change all cells in one group to any color. The key insight is that we should keep the color group with the most connected components unchanged, and convert all other groups to that color. Problem Understanding Given the matrix: 2 2 2 2 ...

Read More
Showing 5221–5230 of 21,090 articles
« Prev 1 521 522 523 524 525 2109 Next »
Advertisements