Arnab Chakraborty

Arnab Chakraborty

3,768 Articles Published

Articles by Arnab Chakraborty

Page 204 of 377

Program to find the left side view of a binary tree in C++

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

Suppose we have a binary tree, if we see the tree from left side, then we can see some elements of it. we have to display those elements. So if the tree is like −The output will be [1, 2, 5]To solve this, we will follow these steps −Define an array retDefine a function dfs(), this will take node, c initialize it with 1, if node is null, then −returnif c > lvl, then −lvl := cinsert value of node into retdfs(left of node, c + 1)dfs(right of node, c + 1)From the main method, do the following −lvl := ...

Read More

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 314 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 tree level that has minimum sum in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 342 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 check whether list can be split into sublists of k increasing elements in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 193 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

Program to find length of longest bitonic subsequence in C++

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

Suppose we have a list of numbers. We have to find length of longest bitonic subsequence. As we knot a sequence is said to be bitonic if it's strictly increasing and then strictly decreasing. A strictly increasing sequence is bitonic. Or a strictly decreasing sequence is bitonic also.So, if the input is like nums = [0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15], size of sequence 16., then the output will be 7.To solve this, we will follow these steps −increasingSubSeq := new array of given array size, and fill with 1for ...

Read More

Program to find length of longest common subsequence in C++

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

Suppose we have two strings text1 and text2, we have to find the length of their longest common subsequence. As we know a subsequence of a string is a new string generated from the original string with some characters deleted without changing the relative order of the remaining characters. (So for example "abe" is a subsequence of "abcde" but "adc" is not). A common subsequence of two strings is a subsequence that is common to both strings. So If there is no common subsequence, return 0. If the input is like “abcde”, and “ace”, then the result will be 3.To ...

Read More

Program to find length of longest common substring in C++

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

Suppose we have two lowercase strings X and Y, we have to find the length of their longest common substring.So, if the input is like X = "helloworld", Y = "worldbook", then the output will be 5, as "world" is the longest common substring and its length is 5.To solve this, we will follow these steps −Define an array longest of size: m+1 x n+1.len := 0for initialize i := 0, when i

Read More

Program to find longest path between two nodes of a tree in Python

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

Suppose we have a binary tree; we have to find the longest path between any two nodes in the tree.So, if the input is like then the output will be 5 To solve this, we will follow these steps:ans := 0Define a function getMaxPath() . This will take nodeif node is null, thenreturn 0leftCnt := getMaxPath(left of node)rightCnt := getMaxPath(right of node)temp := 1 + maximum of leftCnt and rightCntans := maximum of ans and l+r+1From the main method do the following −getMaxPath(root)return ansLet us see the following implementation to get better understanding −Exampleclass TreeNode:    def __init__(self, val, left=None, right=None):   ...

Read More

Swap Even Index Elements And Odd Index Elements in Python

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

Suppose we have a list of numbers called nums, we will exchange each consecutive even indexes with each other, and also exchange each consecutive odd index with each other.So, if the input is like [1,2,3,4,5,6,7,8,9], then the output will be [3, 4, 1, 2, 7, 8, 5, 6, 9]To solve this, we will follow these steps −length := size of numsfor i in range 0 to length, increase by 4, doif i+2

Read More

Program to find the maximum number in rotated list in C++

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

Suppose there is an array, and that is sorted, consider that array is rotated at some pivot, that is unknown to us. So we have to find the maximum from that rotated array. So if the array is like[3, 4, 5, 1, 2], then the output will be 5.To solve this, we will follow these steps −low := 0 and high := last index of array, n := size of array, ans := 0while low arr[mid], then ans := maximum of ans and arr[mid], high := mid – 1else if low = mid, then ans := maximum of ans ...

Read More
Showing 2031–2040 of 3,768 articles
« Prev 1 202 203 204 205 206 377 Next »
Advertisements