
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
Found 10476 Articles for Python

183 Views
Suppose we have a list of numbers called nums. Let us consider an operation where we can select a number, then remove it and increase our score by the sum of the number and its two adjacent numbers. If we can perform this operation as many times as we want as long as we do not select the first or the last number in the list. We have to find the maximal score possible.So, if the input is like nums = [2, 3, 4, 5, 6], then the output will be 39, as we can select 5, then sum will ... Read More

439 Views
Suppose we have a 2D matrix, where each row and column is sorted in non-decreasing order, we have to check whether given target is present inside it or not.So, if the input is like243034316632And target = 31, then the output will be TrueTo solve this, we will follow these steps −col := column size of matrix - 1for i in range 0 to row size of matrix, dowhile matrix[i, col] > target and col >= 0, docol := col - 1if matrix[i, col] is same as target, thenreturn Truereturn FalseLet us see the following implementation to get better understanding −Example Live ... Read More

175 Views
Suppose we have a 2D matrix, where each row and column is sorted in non-decreasing order, we have to find the nth smallest number.So, if the input is like243034316632And n = 4, then the output will be 6.To solve this, we will follow these steps −lst := a new listfor each row i in matrix, dofor each cell j in i, doinsert j at the end of lstsort the list lstreturn lst[n]Let us see the following implementation to get better understanding −Example Live Democlass Solution: def solve(self, matrix, n): lst = [] for i ... Read More

233 Views
Suppose we have a binary matrix. We have to find the same matrix, but each cell's value will be the Manhattan distance to the nearest 0. We can assume at least one 0 exists in the matrix.So, if the input is like101101110then the output will be101101210as only the bottom left cell has distance of 2 to the nearest 0.To solve this, we will follow these steps −m := row size of matrix, n := column size of matrixfor y in range 0 to m, dofor x in range 0 to n, doif matrix[y, x] is non-zero, thenmatrix[y, x] := infinityfor ... Read More

1K+ Views
Suppose we have a list of coins and another value amount, we have to find the number of combinations there are that sum to amount. If the answer is very large, then mod the result by 10^9 + 7.So, if the input is like coins = [2, 5] amount = 10, then the output will be 2, as we can make these combinations − [2, 2, 2, 2, 2], [5, 5]To solve this, we will follow these steps −m := 10^9 + 7dp := a list of size same as amount + 1, and fill it with 0dp[0] := 1for ... Read More

256 Views
Suppose we have a list of numbers called nums and another value target. We have to find the lowest sum of pair of numbers that is larger than target.So, if the input is like nums = [2, 4, 6, 10, 14] target = 10, then the output will be 12, as we pick 2 and 10To solve this, we will follow these steps −sort the list numsn := size of numsanswer := 10^10i := 0, j := n - 1while i < j, doif nums[i] + nums[j] > target, thenanswer := minimum of answer and (nums[i] + nums[j])j := j ... Read More

294 Views
Given a nested list of integers, return the sum of all integers in the list weighted by their depth. Each element is either an integer, or a list -- whose elements may also be integers or other lists. Different from the previous question where weight is increasing from root to leaf, now the weight is defined from bottom up. i.e., the leaf level integers have weight 1, and the root level integers have the largest weight.So, if the input is like [[1, 1], 2, [1, 1]], then the output will be 8, as four 1's at depth 1, one 2 ... Read More

634 Views
Suppose we have a number n we have to generate nth term in “Look and Say” sequence. This is a sequence whose few terms are like below −111211211111221The string will be read like1 (One)11 (One 1) So read the previous 1, and say “One 1”21 (Two 1) So read the previous 11, and say “Two 1”1211 (One 2 one 1) So read the previous 21, and say “One 2 one 1”111221 (One 1 one 2 two 1) So read the previous 1211, and say “One 1 one 2 two 1”Suppose we have a number n, 1

252 Views
Suppose we have a string s where each characters are sorted and we also have a number k, we have to find the length of the longest substring such that every character occurs at least k times.So, if the input is like s = "aabccddeeffghij" k = 2, then the output will be 8, as the longest substring here is "ccddeeff" here every character occurs at least 2 times.To solve this, we will follow these steps −Define a function rc() . This will take lstc := a map with all characters and their occurrencesacc := a new listans := 0valid ... Read More

224 Views
Suppose we have a list of numbers called nums and another value k, we have to find the length of longest sublist where the absolute difference between the largest and smallest element is ≤ k.So, if the input is like nums = [2, 4, 6, 10] k = 4, then the output will be 3, as we can select pick [2, 4, 6] here the absolute difference is 4.To solve this, we will follow these steps −Create two double ended queue maxd, mindi := 0, res := 1for each index j and value a in A, dowhile maxd is not ... Read More