A Binary Search Tree (BST) is a tree data structure where each node has at most two children, and for each node, all values in the left subtree are smaller and all values in the right subtree are larger. We can efficiently search for a value by comparing it with the current node and moving left or right accordingly. .node { fill: #e8f4fd; stroke: #2196f3; stroke-width: 2; } .text { font-family: Arial; font-size: 14px; text-anchor: middle; fill: #333; ... Read More
When working with strings, we sometimes need to check if one string can be transformed into another by shifting characters clockwise in the alphabet. For example, "c" can be converted to "e" using 2 clockwise shifts (c → d → e). In this problem, we have two strings p and q, and a number r representing the maximum allowed shifts. We need to determine if p can be converted to q by shifting characters clockwise at most r times in total. Example If p = "abc", q = "ccc", and r = 3, the answer is True ... Read More
Suppose we have two strings S and T, these two are representing integers. We have to add them and find the result in the same string representation. So, if the input is like "256478921657", "5871257468", then the output will be "262350179125", as 256478921657 + 5871257468 = 262350179125. Approach To solve this, we will follow these steps − Convert S and T from string to integer Add the integers: result = S + T Return the result as string Example Let us see the following implementation to get better understanding − ... Read More
Suppose we have a list of numbers; we have to check whether the list is strictly increasing or strictly decreasing. A list is strictly increasing if each element is larger than the previous one, and strictly decreasing if each element is smaller than the previous one. So, if the input is like nums = [10, 12, 23, 34, 55], then the output will be True, as all elements are distinct and each element is larger than the previous one, so this is strictly increasing. Algorithm To solve this, we will follow these steps ? If size of nums
Suppose we have a list of numbers called nums, we have to squeeze it from both the left and the right until there is one remaining element. We will return the states at each step. So, if the input is like nums = [10, 20, 30, 40, 50, 60], then the output will be ? [ [10, 20, 30, 40, 50, 60], [30, 30, 40, 110], [60, 150], [210] ] Algorithm To solve this, we will follow these steps ? ret := a list with only ... Read More
Suppose we have a list of mailboxes where each mailbox contains a list of strings. Each string represents either "J" for junk, "P" for personal, or "W" for work emails. We need to process each mailbox in round-robin order starting from the first mailbox, filter out junk emails, and return a single sorted list. So, if the input is like mailboxes = [["W", "P"], ["J", "P", "J"], ["W"]], then the output will be ["W", "W", "P", "P"]. In processing order without filtering, we have W → J → W → P → P → J. After filtering out junk ... Read More
Suppose we have a list of numbers called nums, we have to find the number of elements that are present in the correct indices, when the list was to be sorted. So, if the input is like [2, 8, 4, 5, 11], then the output will be 2, as the elements 2 and 11 are in their correct positions. The sorted sequence will be [2, 4, 5, 8, 11] Algorithm To solve this, we will follow these steps − s := sort the list nums count := 0 for i in range 0 to size ... Read More
Suppose we have a string s and a number k. Each character in the string is either a dot ('.') or 'x', where dot indicates an empty space and 'x' indicates a person. We need to check whether it's possible to choose a position to stand on such that the distance between us and the closest person is at least k units away. The distance between neighboring indices is 1. For example, if the input is s = "x...x.." and k = 2, the output will be True because we can stand at position s[2] or s[6]. Algorithm ... Read More
Sometimes we need to find the shortest sublist that, when sorted, makes the entire list sorted. This problem involves comparing the original list with its sorted version to identify the boundaries of the unsorted region. So, if the input is like nums = [1, 2, 5, 4, 9, 10], then the output will be 2, as sorting the sublist [5, 4] would make the entire list sorted: [1, 2, 4, 5, 9, 10]. Algorithm To solve this, we will follow these steps − Initialize first := -1, last := -1 to track the boundaries Create ... Read More
Suppose we have a binary string s, we can delete any two adjacent letters if they are different. Finally, we have to find the length of the smallest string that we can get if we are able to perform this operation as many times as we want. So, if the input is like s = "1100011", then the output will be 1, as after deleting "10" we get "10011", then again delete "10", it will be "011", then delete "01", it will have left "1". Algorithm Approach To solve this, we will follow these steps − ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Economics & Finance