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
Programming Articles
Page 518 of 2547
Program to find longest even value path of a binary tree in Python
A Binary tree is one of the data structures in Python in which each element is known as a Node. Each node should have a minimum of two child nodes. These children nodes are called as left child and the right child. The top node of the binary tree is known as the Root Node, and the nodes with no children are called Leaf Nodes. Following is the representation of the Binary Tree in Python − 10 / \ 5 ...
Read MoreProgram to find longest equivalent sublist after K increments in Python
The longest equivalent sublist after K increments is the longest contiguous portion of a list where we can make all elements equal by performing at most K increment operations. This problem uses a sliding window technique on a sorted array to find the optimal solution. Given a list nums and integer k, we need to find the maximum length of a sublist where all elements can be made equal using at most k increment operations on individual elements. Problem Example Consider this input scenario where we can increment 9 once and 6 four times to get the ...
Read MoreProgram to find length of longest distinct sublist in Python
A distinct sublist is a continuous portion of a list in which all the elements are different from each other. The goal is to find the maximum length of such a sublist within a given list of elements. In Python, a list is an ordered and mutable data structure in which sequence of elements are enclosed in square brackets []. The elements in a list can be of any datatype such as integer, float, string etc., and each element in a list has a unique index position which starts from 0. Using Sliding Window with set() ...
Read MoreProgram to find length of longest anagram subsequence in Python
In Python, a string is an immutable data structure in which sequence of characters are enclosed in double("") or single quotes(''). In some cases, we need to find the length of the longest subsequence of characters that can be rearranged to form a palindrome. This type of subsequence is referred to as a palindromic anagram subsequence. An anagram is a word or phrase formed by rearranging the letters of another word or phrase by using all the original letters exactly once. In other words, two strings are said to be anagrams of each other if they contain the same ...
Read MoreProgram to return number of smaller elements at right of the given list in Python
In Python, a list is an ordered and mutable data structure where elements are enclosed in square brackets []. In some scenarios, we may need to count how many elements to the right of each element in the list are smaller than it. For example, given the list [5, 2, 6, 1], for element 5 at index 0, there are 2 smaller elements (2 and 1) to its right. For element 2 at index 1, there is 1 smaller element (1) to its right. Using Binary Search with bisect_left() Function The bisect_left() function from the bisect module ...
Read MoreProgram to find which element occurs exactly once in Python
In Python, data structures such as strings, lists, etc., may contain duplicate values. In some scenarios, we may need to find which element appears only once while all other elements appear multiple times. Let's explore different methods to find which element occurs exactly once in Python. Using Bitwise XOR The XOR (Exclusive OR) is a bitwise operator that returns 1 if the corresponding bits of two operands are different, otherwise it returns 0 if they are the same. In Python, we use the ^ operator to perform XOR operations. The XOR approach works because of these properties: ...
Read MoreProgram to partition color list in Python
Suppose we have a list of color strings containing "red", "green" and "blue". We need to partition the list so that red comes before green, and green comes before blue. So, if the input is like colors = ["blue", "green", "blue", "red", "red"], then the output will be ['red', 'red', 'green', 'blue', 'blue']. Algorithm To solve this, we will follow these steps − Initialize three pointers: red = 0, green = 0, blue = 0 For each string in the list: ...
Read MoreProgram to create linked list to binary search tree in Python
A Binary Search Tree (BST) is a tree data structure where each node has at most two children, and for every node, the left subtree contains values smaller than the node, and the right subtree contains values greater than the node. Converting a sorted linked list to a BST involves finding the middle element as the root and recursively building left and right subtrees. Algorithm The key insight is to use the two-pointer technique to find the middle node of the linked list ? Find the middle node using slow and fast pointers Make the middle ...
Read MoreProgram to convert level order binary tree traversal to linked list in Python
Converting a binary tree to a singly linked list using level-order traversal means visiting nodes level by level from left to right. We use a queue to perform breadth-first traversal and create linked list nodes in the same order. Problem Understanding Given a binary tree like this: 5 4 10 2 7 ...
Read MoreProgram to traverse binary tree level wise in alternating way in Python
Suppose we have a binary tree, we need to traverse it level by level in an alternating manner − first level left−to−right, second level right−to−left, third level left−to−right, and so on. So, if the input is like: 5 4 -10 ...
Read More