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
-
Economics & Finance
Python Articles
Page 633 of 855
Program to make all elements equal by performing given operation in Python
Suppose we have a list of numbers and we want to make all values equal. We can perform an operation where we pick one element from the list and increment every other element by 1. We need to find the minimum number of operations required to make all element values equal. So, if the input is like [2, 4, 5], then the output will be 5. Algorithm To solve this problem, we will follow these steps − Find the minimum value in the list Initialize sum as 0 ...
Read MoreProgram to perform given operation with each element of a list and given value in Python
Suppose we have a list of numbers called nums, an operator string op representing operations like "+", "-", "/", or "*", and a value val. We need to perform the operation on every number in nums with val and return the result. For example, if the input is [5, 3, 8] with operator "*" and value 3, the output will be [15, 9, 24]. Algorithm Steps To solve this problem, we will follow these steps − Create an empty result list For each number in the input list: ...
Read MoreProgram to remove all nodes of a linked list whose value is same as in Python
A singly linked list is a linear data structure where each node contains data and a reference to the next node. Sometimes we need to remove all nodes with a specific target value while maintaining the list structure. So, if the input is like [5, 8, 2, 6, 5, 2, 9, 6, 2, 4] and target is 2, then the output will be [5, 8, 6, 5, 9, 6, 4]. Algorithm To solve this problem, we follow these steps − Start with the head node While current node and next node exist: If next ...
Read MoreProgram to check whether a binary tree is BST or not in Python
A Binary Search Tree (BST) is a binary tree with specific ordering properties. We can check if a binary tree is a valid BST by performing an inorder traversal and verifying the result is sorted. BST Properties A valid BST must satisfy these conditions − All nodes in the left subtree are smaller than the current node value All nodes in the right subtree are larger than the current node value These properties hold recursively for all nodes ...
Read MoreProgram to remove all nodes from BST which are not in range in Python
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 MoreProgram to check whether different brackets are balanced and well-formed or not in Python
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 MoreProgram to check whether parentheses are balanced or not in Python
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 MoreProgram to count number of unique binary search tree can be formed with 0 to n values in Python
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 MoreProgram to find contiguous intervals of a unique array in Python
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 MoreProgram to check string contains consecutively descending string or not in Python
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