Arnab Chakraborty

Arnab Chakraborty

3,768 Articles Published

Articles by Arnab Chakraborty

Page 152 of 377

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 19-Nov-2020 201 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
Arnab Chakraborty
Updated on 19-Nov-2020 168 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
Arnab Chakraborty
Updated on 19-Nov-2020 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
Arnab Chakraborty
Updated on 19-Nov-2020 192 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 find overlapping intervals and return them in ascending order in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 19-Nov-2020 414 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

Read More

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 19-Nov-2020 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
Arnab Chakraborty
Updated on 19-Nov-2020 272 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 Inorder Successor of a binary search tree in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 19-Nov-2020 775 Views

Suppose we have a binary search tree BST and another value of a node, we have to find the in-order successor of that node in the BST. As we all know that the successor of a node p is the node with the smallest key greater than the value of p.So, if the input is likeAnd p = 1, then the output will be 2, To solve this, we will follow these steps −Define recursive method inorderSuccessor(), this will take root and pif root null, then:return nullif val of root val val){             return inorderSuccessor(root->right, ...

Read More

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 19-Nov-2020 582 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
Arnab Chakraborty
Updated on 19-Nov-2020 851 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
Showing 1511–1520 of 3,768 articles
« Prev 1 150 151 152 153 154 377 Next »
Advertisements