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
Server Side Programming Articles
Page 514 of 2109
Program to find length of longest sublist with given condition in Python
Suppose we have a list of numbers called nums. We need to find the length of the longest sublist where 2 * minimum of sublist > maximum of sublist. So, if the input is like nums = [10, 2, 6, 6, 4, 4], then the output will be 4, as the sublist [6, 6, 4, 4] is the longest sublist that satisfies the condition since 2 * 4 > 6. Algorithm We use a sliding window approach with two deques to efficiently track minimum and maximum values in the current window ? ...
Read MoreProgram to find length of longest alternating inequality elements sublist in Python
Given a list of numbers, we need to find the length of the longest sublist where the inequality relation between consecutive numbers alternates between less-than and greater-than operations. The first two numbers can start with either inequality relation. So, if the input is like nums = [1, 2, 6, 4, 5], then the output will be 4, as the longest alternating inequality sublist is [2, 6, 4, 5] where 2 < 6 > 4 < 5. Algorithm To solve this problem, we follow these steps ? Define a helper function get_direction() that returns the relationship ...
Read MoreProgram to find length of longest increasing subsequence in Python
The Longest Increasing Subsequence (LIS) problem asks us to find the length of the longest subsequence where elements are in increasing order. For example, in the array [6, 1, 7, 2, 8, 3, 4, 5], the LIS is [1, 2, 3, 4, 5] with length 5. We'll solve this using an efficient binary search approach with O(n log n) time complexity. Algorithm Steps The algorithm maintains a tails array where tails[i] stores the smallest ending element of all increasing subsequences of length i+1 ? Create a tails array of same size as input array, initialize ...
Read MoreProgram 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 More