Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Programming Articles - Page 1931 of 3366
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
944 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
205 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
350 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
498 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
440 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
695 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
513 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
2K+ Views
Suppose we have an array of words and a width maxWidth, we have to format the text such that each line has exactly maxWidth number of characters and is fully justified. We should pack our words in a greedy approach; so that is, pack as many words as we can in each line. We will pad extra spaces ' ' when necessary so that each line has exactly maxWidth characters.Here extra spaces between words should be distributed as evenly as possible. If the number of spaces on a line do not divide evenly between words, empty slots on the left ... Read More
2K+ Views
Suppose we have a set of non-overlapping intervals; we have to insert a new interval into the intervals. We can merge if necessary. So if the input is like − [[1, 4], [6, 9]], and new interval is [2, 5], then the output will be [[1, 5], [6, 9]].To solve this, we will follow these steps −Insert new interval at the end of the previous interval listsort the interval list based on the initial time of the intervals, n := number of intervalscreate one array called ans, insert first interval into ansindex := 1while index < n, last := size ... Read More