Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Articles by Arnab Chakraborty
Page 54 of 377
Program to find minimum swaps required to make given anagram in python
Suppose we have two strings S and T and they are anagrams of each other. We have to find the minimum number of swaps required in S to make it same as T.So, if the input is like S = "kolkata" T = "katloka", then the output will be 3, as can swap in this sequence [katloka (given), kotlaka, koltaka, kolkata].To solve this, we will follow these steps −Define a function util() . This will take S, T, iif i >= size of S , thenreturn 0if S[i] is same as T[i], thenreturn util(S, T, i + 1)x := T[i]ret ...
Read MoreCousins in Binary Tree in C++
Suppose we have a binary tree, the root node is present at depth 0, and children of each depth k node are at depth k+1.Here two nodes of a binary tree are called cousins if they have the same depth, but have different parents.All values of the tree will be unique, and the values x and y of two different nodes in the tree. We have to check whether the nodes corresponding to the values x and y are cousins or not.So, if the input is likex = 5, y = 4, then the output will be trueTo solve this, ...
Read MoreProgram to count how many numbers should be appended to create all numbers from 1 to k in C++
Suppose we have a list of numbers called nums and another value k. We have to find the minimum number of numbers that we need to insert into nums such that we can make any number from [1, k] using some subset in nums.So, if the input is like nums = [3, 5], k = 6, then the output will be 2, as we have to insert 1, 2, so we can make : 1 = [1], 2 = [2], 3 = [3], 4 = [1, 3], 5 = [5], 6 = [1, 5].To solve this, we will follow these ...
Read MoreRotting Oranges in C++
Suppose we have a grid, here in each cell can have one of three values −value 0 for an empty cell;value 1 for a fresh orange;value 2 for a rotten orange.In every minute, any fresh orange that is adjacent to a rotten orange becomes rotten.We have to find the minimum number of times that must elapse until no cell has a fresh orange. If this is not possible, then return -1.So, if the input is like [[2, 1, 1], [1, 1, 0], [0, 1, 1]], then the output will be 4To solve this, we will follow these steps −minutes := ...
Read MoreProgram to find largest rectangle area under histogram in python
Suppose we have a list of numbers representing heights of bars in a histogram. We have to find area of the largest rectangle that can be formed under the bars.So, if the input is like nums = [3, 2, 5, 7]then the output will be 10To solve this, we will follow these steps −stk := a stack and initially insert -1 into itinsert 0 at the end of heightsans := 0for i in range 0 to size of heights, dowhile heights[i] < heights[top of stk], doh := heights[top of stk] and pop from stkw := i - top of stk ...
Read MoreProgram to find area of largest square of 1s in a given matrix in python
Suppose we have a binary matrix, we have to find largest square of 1s in that given matrix.So, if the input is like100001100000110111100011110001111000111100then the output will be 16.To solve this, we will follow these steps −res := 0for i in range 0 to size of matrix, dores := maximum of res and matrix[i, 0]for i in range 0 to size of matrix[0], dores := maximum of res and matrix[0, i]for i in range 1 to row count of matrix, dofor j in range 1 to column count of matrix, doif matrix[i, j] is same as 1, thenmatrix[i, j] = minimum ...
Read MoreConstruct Binary Tree from String in C++
Suppose we have a string consisting of parenthesis and integers. We have to construct a binary tree from that string. The whole input represents a binary tree. It holds an integer that followed by zero, one or two pairs of parentheses. The integer represents the root's value and a pair of parenthesis contains a child binary tree with the same structure.So, if the input is like "4(2(3)(1))(6(5))", then the output will be [3, 2, 1, 4, 5, 6] (inorder traversal)To solve this, we will follow these steps −Define a function solve(), this will take s, idx, if idx >= size ...
Read MoreProgram to make almost BST to exact BST in python
Suppose we have a binary tree and that is almost a binary search tree. Only two nodes' value are swapped. We have to correct it and return the binary search tree.So, if the input is likethen the output will beTo solve this, we will follow these steps −prev_node := null, min_node := null, max_node := nullfound_one := Falsefor each node in the inorder traversal of root, doif prev_node is not null, thenif value of node < value of prev_node, thenif min_node is null or value of node < value of min_node, thenmin_node := nodeif max_node is null or value of ...
Read MoreOutput Contest Matches in C++
Suppose we have n teams and we always arrange the rather strong team to play with the rather weak team, like make the rank 1 team play with the team with rank n, this strategy is to make the contest more interesting. Now we have to find their final contest matches in the form of a string.These teams are given in the form of positive integers from 1 to n, which represents their initial rank. So, rank 1 is the strongest team, and Rank n is the weakest team. We will use parentheses and commas to represent the contest team ...
Read MoreProgram to find length of longest consecutive path of a binary tree in python
Suppose we have a binary tree; we have to find the longest path in the binary tree.So, if the input is likethen the output will be 5 as longest consecutive sequence is [2, 3, 4, 5, 6].To solve this, we will follow these steps −if root is null, thenreturn 0maxPath := 0Define a function helper() . This will take nodeinc := 1, dec := 1if left of node is not null, then[left_inc, left_dec] := helper(left of node)otherwise, [left_inc, left_dec] := [0, 0]if right of node is not null, then[right_inc, right_dec] := helper(right of node)otherwise, [right_inc, right_dec] := [0, 0]if left ...
Read More