Arnab Chakraborty

Arnab Chakraborty

3,768 Articles Published

Articles by Arnab Chakraborty

Page 149 of 377

Recover Binary Search Tree in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 403 Views

Suppose we have one binary search tree, now consider two elements of this BST is swapped, so we have to recover this binary search tree.So if the given tree is like below (first one), the recovered tree will be (second one) −To solve this, we will follow these steps −Define some prev, first, second reference for nodesDefine one method called findProblem(), this will take nodeif node is null, then returncall findProblem(left of node)if prev is not null and value of prev > value of node, thenif first is null, then first = prevsecond := nodeprev := nodecall findProblem(right of node)From ...

Read More

Jump Game V in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 696 Views

Suppose we have an array of integers called arr and an integer d. In one step we can jump from index i to −i + x where: i + x < n and x in range 1 to d.i - x where: i - x >= 0 and x in range 1 to d.Here n is the size of array. In addition, we can only jump from index i to index j when arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j. We can choose any index of the array and start the jumping. ...

Read More

Index Pairs of a String in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 985 Views

Suppose we have a text string and words (a list of strings), we have to find all index pairs [i, j] such that the substring text[i]...text[j] is in the list of words. So if the string is like “ababa” and words array is like [“aba”, “ab”], then the output will be [[0, 1], [0, 2], [2, 3], [2, 4]]. One thing we can notice, that the matches can overlap, the “aba” is matched in [0, 2] and [2, 4].To solve this, we will follow these steps −res := an empty listfor i in range 0 to a length of stringfor ...

Read More

Two Sum Less Than K in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 916 Views

Suppose we have an array A of integers and another integer K is given. We have to find the maximum S such that there exists i < j with A[i] + A[j] = S and S < K. If there is no i, j exists satisfying this equation, then return -1. So for example if A = [34, 23, 1, 24, 75, 33, 54, 8] and K = 60, then the output will be 58, as we can use 34 and 24 to sum 58, which is less than 60.To solve this, we will follow these steps −res = - ...

Read More

Remove Vowels from a String in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 2K+ Views

Suppose we have a string, we have to remove all vowels from that string. So if the string is like “iloveprogramming”, then after removing vowels, the result will be − "lvprgrmmng"To solve this, we will follow these steps −create one array vowel, that is holding [a, e, i, o, u]for v in a vowelreplace v using blank stringExampleLet us see the following implementation to get a better understanding −class Solution(object):    def removeVowels(self, s):       s = s.replace("a", "")       s = s.replace("e", "")       s = s.replace("i", "")       s = ...

Read More

Largest Unique Number in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 619 Views

Suppose we have a list of numbers, we have to return the number whose occurrence is 1, if no such element is present, then return -1. So if the list is like [5, 2, 3, 6, 5, 2, 9, 6, 3], then the output will be 9.To solve this, we will follow these steps −We will check each element, and put the elements inside the map, so if the element is not in map, then put a new entry, otherwise increase the valuethen go through the map, when the value is 1, return the key.Example(Python)Let us see the following implementation ...

Read More

Check If a Number Is Majority Element in a Sorted Array in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 373 Views

Suppose we have an array called, nums and that is sorted in non-decreasing order, and a number target. We have to find if the target is a majority element. In an array a majority element is an element that appears more than N/2 times in an array of length N. So if the array is like − [2, 4, 5, 5, 5, 5, 5, 6, 6] and target is 5, then output is true.To solve this, we will follow these steps −There will be two helping modules, lower() and upper(). These are as follows.The lower() takes two arguments array arr ...

Read More

Single-Row Keyboard in python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 462 Views

Suppose, there is a special keyboard with all keys in a single row. So if we have a string of length 26 indicating the layout of the keyboard (indexed from 0 to 25), initially our finger is at index 0. To type a character, we have to move your finger to the index of the next character. The time taken to move your finger from index i to index j is denoted as |i - j|. So if we want to type a string. we have to define a function to calculate how much time it takes to type it ...

Read More

Diet Plan Performance in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 2K+ Views

Suppose a dieter consumes calories[i], this indicates the calories on the i-th day. If we have an integer k, for every consecutive sequence of k days that is (calories[i], calories[i+1], ..., calories[i+k-1] for all 0 upper, so increase one point, lower = length of C, then come out from the looptemp := temp + C[right]return pointsExampleLet us see the following implementation to get better understanding −class Solution(object):    def dietPlanPerformance(self, c, k, l, u):       temp = 0       for i in range(k):          temp += c[i]       right = ...

Read More

Intersection of Three Sorted Arrays in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 568 Views

Suppose there are three integer arrays arr1, arr2 and arr3 and they are sorted in strictly increasing order, we have to return a sorted array of only the integers that appeared in all of these three arrays. So if arrays are [1, 2, 3, 4, 5], [1, 2, 5, 7, 9], and [1, 3, 4, 5, 8], so the output will be [1, 5]To solve this, we will follow these steps −define an array called rescreate three maps f1, f2 and f3for i in range 0 to length of arr1f1[arr1[i]] increase by 1for i in range 0 to length of ...

Read More
Showing 1481–1490 of 3,768 articles
« Prev 1 147 148 149 150 151 377 Next »
Advertisements