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 More
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 More
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 More
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 More
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 More
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
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 More
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 More
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
When working with bracket sequences, finding the longest balanced subsequence is a common problem. A balanced bracket subsequence contains equal numbers of opening "(" and closing ")" brackets, where each closing bracket has a matching opening bracket before it. So, if the input is like s = "())(()(" , then the output will be 4, as we can take the subsequence like "()()". Algorithm Approach The key insight is to traverse the string from right to left and use a greedy approach ? Initialize result counter and closing bracket counter Traverse string from right to ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Economics & Finance