Python Articles - Page 685 of 929

Program to find k sublists with largest sums and return sums in ascending order in Python

Arnab Chakraborty
Updated on 19-Nov-2020 06:36:22

199 Views

Suppose we have a list of numbers called nums, and another value k, we have to find k sublists with the largest sums and return the sums in non-decreasing order.So, if the input is like nums = [2, 4, 5, -100, 12, -30, 6, -2, 6] k = 3, then the output will be [10, 11, 12], as we have these 3 sublists with the largest sums − [6, -2, 6], [2, 4, 5], [12].To solve this, we will follow these steps −ps := a list of 1 + size of nums and fill with 0for each index i and ... Read More

Program to find a sub-list of size at least 2 whose sum is multiple of k in Python

Arnab Chakraborty
Updated on 19-Nov-2020 06:34:05

167 Views

Suppose we have a list of non-negative numbers called nums and another positive value k. We have to find check whether there is any sublist of length at least 2 whose sum is multiple of k or not.So, if the input is like nums = [12, 6, 3, 4] k = 5, then the output will be True, as the sublist is [12, 3] sums to 15 which is divisible by 5.To solve this, we will follow these steps −sum := 0m := a new mapm[0] := -1for i in range 0 to size of nums, dosum := sum + ... Read More

Program to find number of elements in A are strictly less than at least k elements in B in Python

Arnab Chakraborty
Updated on 19-Nov-2020 06:32:36

2K+ Views

Suppose we have two lists of numbers A and B, and another value k, we have to find the number of elements in A that are strictly less than at least k elements in B.So, if the input is like A = [6, -2, 100, 11] B = [33, 6, 30, 8, 14] k = 3, then the output will be 3, as -2, 6, and 11 are strictly less than 3 elements in B.To solve this, we will follow these steps −if k is same as 0, thenreturn size of Asort B in reverse orderct := 0for each i ... Read More

Program to check number of global and local inversions are same or not in Python

Arnab Chakraborty
Updated on 19-Nov-2020 06:30:40

191 Views

Suppose we have a list of distinct numbers called nums. Here a global inversion is when there's indices i < j such that nums[i] > nums[j]. And local inversion is when there is an index i and i + 1 such that nums[i] > nums[i + 1]. We have to check whether the number of global inversions is equal to the number of local inversions or not.So, if the input is like nums = [3, 2, 4], then the output will be True, as the indices 0 and 1 are both a global and local inversion.To solve this, we will ... Read More

Program to merge intervals and sort them in ascending order in Python

Arnab Chakraborty
Updated on 19-Nov-2020 06:28:45

945 Views

Suppose we have a list intervals, we have to find the union of them in sorted sequence.So, if the input is like inv = [[2, 5],[4, 10],[20, 25]], then the output will be [[2, 10], [20, 25]]To solve this, we will follow these steps −sort the list intervalsans := a new listfor each start and end (s, e) in intervals, doif ans and s

Program to find overlapping intervals and return them in ascending order in Python

Arnab Chakraborty
Updated on 19-Nov-2020 06:26:33

412 Views

Suppose we have a list of closed intervals and another list of intervals. Individually, each list is non-overlapping and they are sorted in non-decreasing order. We have to find the overlap of the two intervals sorted in non-decreasing order.So, if the input is like inv1 = [[50, 100],[190, 270],[310, 330]] inv2 = [[40, 120],[180, 190]], then the output will be [[50, 100], [190, 190]]To solve this, we will follow these steps −ans := a new listi := 0, j := 0while i < size of A and j < size of B, doif start

Program to find total unique duration from a list of intervals in Python

Arnab Chakraborty
Updated on 19-Nov-2020 06:25:00

1K+ Views

Suppose we have a list of intervals where each list represents an interval [start, end] (inclusive). We have to find the total unique duration it covers.So, if the input is like intervals = [[2, 11], [13, 31], [41, 61]], then the output will be 50, as the total unique covered distance is (11 - 2 + 1) = 10 then (31 - 13 + 1) = 19 and (61 - 41 + 1) = 21, so total is 50.To solve this, we will follow these steps −if intervals list is empty, thenreturn 0sort the list intervals[start, end] := intervals[0]ans := ... Read More

Program to find intervals that do not intersect the cut interval in Python

Arnab Chakraborty
Updated on 19-Nov-2020 06:23:27

270 Views

Suppose we have a sorted and disjoint intervals list and another list cut, that represents an interval. We have to delete all parts of intervals that are intersecting with cut interval, and return the new list.So, if the input is like intervals = [[2, 11], [13, 31], [41, 61]] cut = [8, 46], then the output will be [[2, 8], [46, 61]]To solve this, we will follow these steps −cut_start, cut_end := cutans := a new listfor each start, end in intervals, doif maximum of cut_start and start < minimum of end and cut_end, thenif start < cut_start, theninsert interval ... Read More

Program to find number of minimum steps to reach last index in Python

Arnab Chakraborty
Updated on 19-Nov-2020 06:19:24

581 Views

Suppose we have a list of numbers called nums and we are placed currently at nums[0]. On each step, we can either jump from the current index i to i + 1 or i - 1 or j where nums[i] == nums[j]. We have to find the minimum number of steps required to reach the final index.So, if the input is like nums = [4, 8, 8, 5, 4, 6, 5], then the output will be 3, as we can jump from index 0 to index 4 as their values are both 4. And then we jump back to index ... Read More

Program to find number of friend groups in a set of friends connections in Python

Arnab Chakraborty
Updated on 19-Nov-2020 06:15:35

850 Views

Suppose we have a a friends list, where friends[i] is a list of people i is friends with. The connection of friendships are two-way. And each person is friend with themselves and two people are in a friend group as long as there is some path of mutual friends connecting them. We have to find the total number of friend groups.So, if the input is like friends = [[0, 1, 5], [1, 0], [2], [3, 4], [4, 3], [5, 0]], then the output will be 3, as The three friend groups are as below −To solve this, we will follow ... Read More

Advertisements