- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- 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 10784 Articles for Python
177 Views
Suppose we have a list of numbers nums, now consider a circular list of nums where the start and end of nums are neighbors. We have to find the maximum sum of a non-empty sublist in the circular list.So, if the input is like nums = [2, 3, -7, 4, 5], then the output will be 14, as we can take the sub list [4, 5, 2, 3] which sums to 14.To solve this, we will follow these steps −max_sum := negative infinity, cur_max := 0min_sum := positive infinity, cur_min := 0for each num in nums, docur_max := maximum of ... Read More
Program to find k-sized list where difference between largest and smallest item is minimum in Python
147 Views
Suppose we have a list of numbers called nums and an integer k, we have to select elements from nums to create a list of size k such that the difference between the largest integer in the list and the smallest integer is as low as possible. And we will return this difference.So, if the input is like nums = [3, 11, 6, 2, 9], k = 3, then the output will be 4, as the best list we can make is [2, 3, 6].To solve this, we will follow these steps −sort the list numsls := a new listfor ... Read More
171 Views
Suppose we have a list of strings words, we have to group all anagrams together and return the size of the largest grouping.So, if the input is like words = ["xy", "yx", "xyz", "zyx", "yzx", "wwwww"], then the output will be 3, as ["xyz", "zyx", "yzx"] is the largest grouping.To solve this, we will follow these steps −lookup := a new map, initially emptyres := 0for each i in words, dop := sort i in lexicographical wayif p is in lookup, increase count, otherwise 1res := maximum of res and lookup[p]return resLet us see the following implementation to get better ... Read More
401 Views
Suppose we have a string s of lowercase alphabet characters, and another number k, we have to find the minimum number of required changes in the string so that the resulting string has at most k distinct characters. In this case The change is actually changing a single character to any other character.So, if the input is like s = "wxxyyzzxx", k = 3, then the output will be 1, as we can remove the letter "w" to get 3 distinct characters (x, y, and z).To solve this, we will follow these steps −count := a map of each character ... Read More
449 Views
Suppose we have a list of sorted unique numbers called nums and an integer k, we have to find the kth missing number from the first element of the given list.So, if the input is like nums = [5, 6, 8, 10, 11], k = 1, then the output will be 9, as 9 is the second (index 1) missing number.To solve this, we will follow these steps −for i in range 1 to size of nums, dodiff := nums[i] - nums[i - 1] - 1if k >= diff, thenk := k - diffotherwise, return nums[i - 1] + k ... Read More
1K+ Views
Suppose we have a singly linked list, we have to check find the value of the kth last node (0-indexed). We have to solve this in single pass.So, if the input is like node = [5, 4, 6, 3, 4, 7], k = 2, then the output will be 3, as The second last (index 3) node has the value of 3.To solve this, we will follow these steps −klast := nodelast := nodefor i in range 0 to k, dolast := next of lastwhile next of last is not null, dolast := next of lastklast := next of klastreturn ... Read More
219 Views
Suppose we have a list of stacks and an integer k. We have to find the maximum possible sum that can be achieved from popping off exactly k elements from any combination of the stacks.So, if the input is like stacks = [[50, -4, -15], [2], [6, 7, 8]], k = 4, then the output will be 39, as we can pop off all 3 elements from the first stack and pop the last element of the last stack to get -15 + -4 + 50 + 8 = 39.To solve this, we will follow these steps −Define a function ... Read More
205 Views
Suppose we have a list of numbers called nums and another number k, we have to find a list of count of distinct numbers in each window of size k.So, if the input is like nums = [2, 2, 3, 3, 4], k = 2, then the output will be [1, 2, 1, 2], as the windows are [2, 2], [2, 3], [3, 3], and [3, 4].To solve this, we will follow these steps −c := make a dictionary of elements in nums and their frequenciesans := a new listfor i in range k to size of nums, doinsert size ... Read More
358 Views
Suppose we have a binary matrix where 0 shows empty cell and 1 shows a block that forms a shape, now we have to find the perimeter of the shape. The shape will not hold any hole inside it.So, if the input is like0000000111001100111000000then the output will be 14.To solve this, we will follow these steps −d := 0perimeter := 0height := row count of matrixlength := column count of matrixfor each row in matrix, doc := 0for each val in row, doif val is same as 1, thensurround := 4if c is not same as length - 1, thenif ... Read More
356 Views
Suppose we have two linked lists l1 and l2, we have to return one linked list by interleaving elements of these two lists starting with l1. If there are any leftover nodes in a linked list, they should be appended to the list.So, if the input is like l1 = [5, 4, 6, 3, 4, 7] l2 = [8, 6, 9], then the output will be [5, 8, 4, 6, 6, 9, 3, 4, 7]To solve this, we will follow these steps −ans := l1while l2 is not null, doif ans is not null, thenif next of ans is not ... Read More