Arnab Chakraborty

Arnab Chakraborty

3,768 Articles Published

Articles by Arnab Chakraborty

Page 78 of 377

Program to find length of longest balanced subsequence in Python

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

Suppose we have a string s containing brackets parenthesis "(" and ")", we have to find the length of the longest subsequence of balanced brackets.So, if the input is like s = "())(()(", then the output will be 4, as we can take the subsequence like "()()"To solve this, we will follow these steps −res := 0n := size of sclose := 0for i in range n - 1 to 0, decrease by 1, doif s[i] is same as ")", thenclose := close + 1otherwise, if close > 0, thenclose := close - 1res := res + 2return resLet us ...

Read More

Program to find the sum of all digits of given number in Python

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

Suppose we have a number num, we have to find the sum of its digits. We have to solve it without using strings.So, if the input is like num = 512, then the output will be 8, as 8 = 5 + 1 + 2.tput will be 8, as 8 = 5 + 1 + 2. To solve this, we will follow these steps −sum:= 0while num is not same as 0, dosum := sum + (num mod 10)num:= quotient of num/10return sumLet us see the following implementation to get better understanding −Exampleclass Solution:    def solve(self, num):       sum=0       while(num!=0):          sum = sum+int(num%10)          num=int(num/10)       return sum ob = Solution() print(ob.solve(512))Input512Output8

Read More

Program to traverse binary tree level wise in alternating way in Python

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

Suppose we have binary tree, we have to show the values of each level by alternating from going left-to-right and right-to-left.So, if the input is likethen the output will be [5, -10, 4, -2, -7, 15]To solve this, we will follow these steps −if root is null, thenreturn a new lists1 := a list initially insert roots2 := a new listres := a new listwhile s1 is not empty or s2 is not empty, dowhile s1 is not empty, donode := delete last element from s1if left of node is not null, theninsert left of node at the end of ...

Read More

Program to find any two numbers in a list that sums up to k in Python

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

Suppose we have a list of numbers called nums and we have another number k, we have to check whether any two numbers present in the list add up to k or now. Same elements must not be used twice. And numbers can be negative or 0.So, if the input is like nums = [45, 18, 9, 13, 12], k = 31, then the output will be True, as 18 + 13 = 31To solve this, we will follow these steps −temp_set:= a new setfor each num in nums, doif num is in temp_set, thenreturn Trueadd (k-num) into temp_setreturn FalseLet ...

Read More

Program to perform level order traversal of binary tree in C++

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

Suppose we have a binary tree. We have to traverse this tree using level order traversal fashion. So if the tree is likeThe traversal sequence will be like: [1, 2, 3, 5, 4]To solve this, we will follow these steps −define queue que to store nodesinsert root into the que.while que is not empty, doitem := item present at front position of queueprint the value of itemif left of the item is not null, then insert left of item into queif right of the item is not null, then insert right of item into quedelete front element from queLet us ...

Read More

Program to find minimum number of operations required to make one number to another in Python

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

Suppose we have a number start and another number end (start < end), we have to find the minimum number of operations required to convert start to end using these operations −Increment by 1Multiply by 2So, if the input is like start = 5, end = 11, then the output will be 2, as we can multiply 2 to get 10, then add 1 to get 11.To solve this, we will follow these steps −ct:= 0while end/2 >= start, doif end mod 2 is same as 1, thenend := end - 1end := end/2ct := ct + 2otherwise, end:= end/2ct ...

Read More

Program to find tree level that has minimum sum in C++

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

Suppose we have a binary tree, the level of its root is 1, the level of its children is 2, and so on.We have to find the smallest level X such that the sum of all the values of nodes at level X is minimum. So if the tree is like −Output will be 2 as the sum is 4 – 10 = -6, which is minimum.To solve this, we will follow these steps −level := 1, sum := value of r, ansLevel := level, ansSum := sumdefine a queue q, insert given node r into qwhile q is not ...

Read More

Program to find number of tasks can be finished with given conditions in Python

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

Suppose we have a list of tasks and another list of people. The tasks[i] determines the amount of strength required to perform the ith task. And the people[i] determines the amount of strength the ith person has. Finally, we have to find the number of tasks that can be finished if one person can perform at most one task.So, if the input is like tasks = [4, 3, 9, 15], people = [10, 5, 3, 2], then the output will be 3, as the first person can perform task 9, second person can perform task 4, third person can perform ...

Read More

Program to find final text in an editor by performing typing and backspacing in Python

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

Suppose we have a string s that represents characters that typed into an editor, the symbol "

Read More

Program to check whether list can be split into sublists of k increasing elements in C++

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

Suppose we have a list of numbers called nums, and another number k, we have to check whether the list can be split into lists where each list contains k values and the values are consecutively increasing.So, if the input is like nums = [4, 3, 2, 4, 5, 6], k = 3, then the output will be True, as we can split the list into [2, 3, 4] and [4, 5, 6]To solve this, we will follow these steps −Define one mapfor each key it in mincrease m[it] by 1ok := truewhile (size of m is not 0 and ...

Read More
Showing 771–780 of 3,768 articles
« Prev 1 76 77 78 79 80 377 Next »
Advertisements