Suppose we have one year Y and a month M, we have to return the number of days of that month for the given year. So if the Y = 1992 and M = 7, then the result will be 31, if the year is 2020, and M = 2, then the result is 29.To solve this, we will follow these steps −if m = 2, thenif y is a leap year, return 29, otherwise 28make an array with elements [1, 3, 5, 7, 8, 10, 12]if m is in the list, then return 31, otherwise, return 30.Example(Python)Let us see ... Read More
Suppose we have an array A of integers and another integer K is given. We have to find the maximum S such that there exists i < j with A[i] + A[j] = S and S < K. If there is no i, j exists satisfying this equation, then return -1. So for example if A = [34, 23, 1, 24, 75, 33, 54, 8] and K = 60, then the output will be 58, as we can use 34 and 24 to sum 58, which is less than 60.To solve this, we will follow these steps −res = - ... Read More
Suppose we have a text string and words (a list of strings), we have to find all index pairs [i, j] such that the substring text[i]...text[j] is in the list of words. So if the string is like “ababa” and words array is like [“aba”, “ab”], then the output will be [[0, 1], [0, 2], [2, 3], [2, 4]]. One thing we can notice, that the matches can overlap, the “aba” is matched in [0, 2] and [2, 4].To solve this, we will follow these steps −res := an empty listfor i in range 0 to a length of stringfor ... Read More
Suppose we have an array A of unique integers sorted in ascending order, we have to return the smallest index i that satisfies A[i] == i. Return -1 if no such i exists. So if the array is like [-10, -5, 0, 3, 7], then the output will be 3, as A[3] = 3 the output will be 3.To solve this, we will follow these steps −For i in range 0 to length of Aif i = A[i], then return ireturn -1Example(Python)Let us see the following implementation to get a better understanding − Live Democlass Solution(object): def fixedPoint(self, A): ... Read More
Suppose we have an array of integers. We have to return the indices of two integers, such that if we add them up, we will reach to a specific target that is also given. Here we will take one assumption, that is always there will be one unique solution in the array, so no two set of indices for same target will be there.For an example, suppose the array is like A = [2, 8, 12, 15], and the target sum is 20. Then it will return indices 1 and 2, as A[1] + A[2] = 20.To solve this, we ... Read More
Suppose we have a triangle. We have to find the minimum path sum from top to the bottom. In each step, we can move to adjacent numbers on the row below.For example, if the following triangle is like[ [2], [3, 4], [6, 5, 7], [4, 1, 8, 3] ]The minimum path sum from top to bottom is 11 (2 + 3 + 5 + 1 = 11).Let us see the steps −Create one table to use in Dynamic programming approach.n := size of trianglefor i := n – 2 down to 0for ... Read More
Suppose we have an expression. The expression has some parentheses; we have to check the parentheses are balanced or not. The order of the parentheses are (), {} and []. Suppose there are two strings. “()[(){()}]” this is valid, but “{[}]” is invalid.To solve this, we will follow these steps −Traverse through the expression until it has exhaustedif the current character is opening bracket like (, { or [, then push into stackif the current character is closing bracket like ), } or ], then pop from stack, andcheck whether the popped bracket is corresponding starting bracket of thecurrent character, ... Read More
Suppose we have a non-negative integer number num. For each number i in the range 0 ≤ i ≤ num we have to calculate the number of 1's in their binary counterpart and return them as a list. So if the number is 5, then the numbers are [0, 1, 2, 3, 4, 5], and number of 1s in these numbers are [0, 1, 1, 2, 1, 2]To solve this, we will follow these steps −res := an array which holds num + 1 number of 0soffset := 0for i in range 1 to num + 1if i and i ... Read More
Suppose there is a list of non-negative integers, we have to arrange them such that they form the largest number. So if the array is [10, 2], then the largest number will be 210.To solve this, we will follow these steps −Arrange the numbers where the most significant digits are greater than place them at first, like this arrangement the numbers. After that just join the numbers from the array.ExampleLet us see the following implementation to get a better understanding − Live Demofrom functools import cmp_to_key class Solution(object): def largestNumber(self, nums): for i in range(len(nums)): ... Read More
Suppose we have Reverse polish notation and we have to evaluate the value. The reverse polish notation is also known as postfix expression. Here we have to use the stack data structure to solve the postfix expressions.From the postfix expression, when some operands are found, pushed them in the stack. When some operator is found, two items are popped from stack and then the operation is performed in the correct sequence. After that, the result is also pushed in the stack for future use. After completing the whole expression, the final result is also stored in the stack top. So ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP