Programming Articles - Page 1930 of 3363

Candy in C++

Arnab Chakraborty
Updated on 26-May-2020 13:35:46

600 Views

Suppose there are N children, they are standing in a line. Here each child is assigned a rating value. We are supplying candies to these children subjected to the following requirements −Each child must have at least one candy.Children whose rating is high will get more candies than their neighbors.We have to find the minimum number of candies we must give?So if the input is like [1, 1, 3], then the output will be 4. So they will get 1, 1 and 2 candies respectively.To solve this, we will follow these steps −n := size of the array ratings, create ... Read More

Palindrome Partitioning II in C++

Arnab Chakraborty
Updated on 26-May-2020 13:14:32

200 Views

Suppose we have a string s, we have to find the number of cuts needed to divide this string into different substring and each part is a palindrome. So if the string is like “ababba”, then this will take 2 cuts. [aba|bb|a]To solve this, we will follow these steps −n := number of characters in the string screate one array called res of size n + 1res[n] := -1for i in range n – 1 down to 0res[i] := n – i – 1for j in range i to nif substring of a, from index i, to j – i ... Read More

Longest Consecutive Sequence in Python

Arnab Chakraborty
Updated on 26-May-2020 13:10:25

5K+ Views

Suppose we have an array of integers. We have to find the length of the longest consecutive elements sequence. So if the input is like [100, 4, 250, 1, 3, 2], answer will be 4, as the longest consecutive sequence is [1, 2, 3, 4].To solve this, we will follow these steps −make the array set, longest := 0for i in range array −if i – 1 is not in a −current := i, streak := 0while i in a −increase i by 1, increase streak by 1longest := max of longest and streakreturn longestExampleLet us see the following implementation ... Read More

Binary Tree Maximum Path Sum in Python

Arnab Chakraborty
Updated on 26-May-2020 13:07:57

964 Views

Suppose we have one non-empty binary tree. We have to find the path sum. So here, a path is any sequence of nodes from some starting node to any node in the where the parent-child connections are present. The path must contain at least one node and does not need to go through the root node. So if the input tree is −Here the output will be 32.To solve this, we will follow these steps −Define one method called solve(), this will take nodeif node is null or the value of node is 0, then return 0left := max of ... Read More

Distinct Subsequences in C++

Arnab Chakraborty
Updated on 26-May-2020 12:59:27

222 Views

Suppose we have strings S and T. We have to count number of distinct sequences of S which is equal to T.We know that a subsequence of a string is a new string which is formed from the original string by removing some (can be none) of the characters without disturbing the relative positions of the remaining characters. (Like, "ACE" is a subsequence of "ABCDE" while "AEC" is not).If the input strings are “baalllloonnn” and “balloon”, then there will be 36 different ways to select.To solve this, we will follow these steps −n := size of s, m := size ... Read More

Recover Binary Search Tree in C++

Arnab Chakraborty
Updated on 26-May-2020 12:56:38

373 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

Interleaving String in C++

Arnab Chakraborty
Updated on 26-May-2020 12:50:30

531 Views

Suppose we have three strings s1, s2 and s3. Then check whether s3 is formed by interleaving s1 and s2 or not. So if the strings are “aabcc”, s2 = “dbbca”, and s3 is “aadbbcbcac”, then the result will be true.To solve this, we will follow these steps −Define one method called solve(), this will take s1, s2, s3 and one 3d array dp, then i, j, kif i = 0 and j = 0 and k = 0, then return trueif dp[i, j, k] is not -1, then return dp[i, j, k]ans := falseif j > 0 and k ... Read More

Maximal Rectangle in C++

Arnab Chakraborty
Updated on 26-May-2020 12:46:33

468 Views

Suppose we have a 2D binary matrix where 0s and 1 values are present. We have to find the largest rectangle containing only 1s and return its area.To solve this, we will follow these steps−Define a function called getAns, this will take array acreate stack st, i := 0, ans := 0while i < size of a, thenif stack is empty or a[i] >= top of stack, then insert i into st, increase i by 1otherwise −height := a[top of stack], delete from stackwidth := i when stack is empty, otherwise i – top of st – 1area := height ... Read More

Minimum Window Substring in C++

Arnab Chakraborty
Updated on 26-May-2020 12:27:46

722 Views

Suppose we have a string S and T. We have to find the minimum window in S which will contain all the characters in T. So if the input is like “ABHDAXCVBAGTXATYCB” and T = “ABC”, then the result will be: “CVBA”.To solve this, we will follow these steps −Create one map mstore the frequency of x into mlength := size of s, left := 0, right := 0, ansLeft := 0 and ansRight := 0counter := size of x, flag := false, ans := empty stringwhile height < size of s −c := s[right]if c is present in m, ... Read More

Edit Distance in C++

Arnab Chakraborty
Updated on 26-May-2020 12:24:03

533 Views

Suppose we have two words word1 and word2, we have to find the minimum number of operations required to concert from word1 to word2. The operations can be of three types, these are insert a character, delete a character and replace a character. So if the input strings are “evaluate” and “fluctuate”, then the result will be 5.To solve this, we will follow these steps −n := size of w1, m := size of w2, create an array dp of size n + 1for i in range 0 to ndp[i] := new array of size m + 1for j in ... Read More

Advertisements