
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 33676 Articles for Programming
Program to count maximum number of distinct pairs whose differences are larger than target in Python

308 Views
Suppose we have a list of numbers called nums and another value target. We have to find the maximum number of pairs where for each pair i < j, i and j are not in any other pair, and |nums[i] - nums[j]| >= target.So, if the input is like nums = [2, 4, 6, 10, 11], target = 5, then the output will be 2, as we can get pairs: (2, 6), (5, 10)To solve this, we will follow these steps −N := size of Asort the list Aans := 0j := N / 2for i in range 0 to ... Read More

278 Views
Suppose we have a 2D grid, containing colors as strings "r", "g", and "b". We have to perform floodfill operation at row r, column c with the color target. As we know the Floodfill operation should replace all elements that are both connected to grid[r, c] (up/right/down/left) and have the same color as grid[r, c] with the same color as target.So, if the input is likeRRRRGBGBBthen the output will beGGGGGBGBBas the red cells connected to grid[0, 0] are replaced with green ("g").To solve this, we will follow these steps −define a new set seenoldcolor := matrix[r, c]Define a function dfs() ... Read More

544 Views
Suppose we have a list of numbers nums, we will pack consecutive elements of the same value into sublists. We have to keep in mind that there is only one occurrence in the list it should still be in its own sublist.So, if the input is like nums = [5, 5, 2, 7, 7, 7, 2, 2, 2, 2], then the output will be [[5, 5], [2], [7, 7, 7], [2, 2, 2, 2]]To solve this, we will follow these steps −if nums is empty, thenreturn a new listresult := a list with another list that contains nums[0]j := 0for ... Read More

163 Views
Suppose we have a 2D list of numbers called intervals where each row represents [start, end] (inclusive) interval. For an interval [a, b] (a < b), its size is (b - a). We must add one interval to the given list such that, after merging all the intervals, we get exactly one range left. We have to find the minimum possible size of the added interval.So, if the input is like intervals = [[15, 20], [30, 50]], then the output will be 10, as we can add the interval [20, 30] which is the smallest possible interval.To solve this, we ... Read More

806 Views
Suppose we have a list of numbers called nums. We can reduce the length of nums by taking any two numbers, removing them, and appending their sum at the end. The cost of doing this operation is the sum of the two integers we removed. We have to find the minimum total cost of reducing nums to one integer.So, if the input is like nums = [2, 3, 4, 5, 6], then the output will be 45, as we take 2 and 3 then remove to get [4, 5, 6, 5], then we take 4 and 5 then remove to ... Read More

428 Views
Suppose we have two strings S and T we have to check whether they are one or zero edit distance away or not. An edit operation can be defined as deleting a character, adding a character, or replacing a character with another character.So, if the input is like S = "hello", T = "hallo", then the output will be True, as these two strings have edit distance of 1.To solve this, we will follow these steps −m := size of S, n := size of Ti := 0, j := 0count := 0if |m - n| > 1, thenreturn Falsewhile ... Read More

691 Views
Suppose we have a list of numbers called nums, we have to find a new list such that each element at index i of the newly generated list is the product of all the numbers in the original list except the one at index i. Here we have to solve it without using division.So, if the input is like nums = [2, 3, 4, 5, 6], then the output will be [360, 240, 180, 144, 120]To solve this, we will follow these steps −if size of nums < 1, thenreturn numsl := size of numsleft := a list of size ... Read More

301 Views
Suppose we have a string s, we have to check whether all its palindromic substrings have odd lengths or not.So, if the input is like s = "level", then the output will be TrueTo solve this, we will follow these steps −for i in range 1 to size of s, doif s[i] is same as s[i - 1], thenreturn Falsereturn TrueLet us see the following implementation to get better understanding −Example Live Democlass Solution: def solve(self, s): for i in range(1, len(s)): if s[i] == s[i - 1]: ... Read More

3K+ Views
In Python, finding palindromic substrings (sequences that read the same forwards and backwards) can be done using various methods, such as expanding from the centre and the brute force method etc. Some of the common approaches to finding palindromic substrings in Python are as follows: Expand around the centre: It involves taking each character in the word as the centre of the potential palindrome. Brute force: This method ... Read More

630 Views
Suppose we have one array nums, where all elements are positive. We are at index 0. Here, each element in the array represents our maximum jump length at that position. Our goal is to reach to the final index (n-1, where n is size of nums) with less number of jumps. So if the array is like [2, 3, 1, 1, 4], and then the output will be 2, as we can jump to index 1 from 0, then jump to index 4, that is the last index.To solve this, we will follow these steps −end := 0, jumps := ... Read More