Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by Arnab Chakraborty
Page 9 of 377
Program to count how many blocks are covered k times by walking in Python
Suppose we have two lists called walks and target. At the beginning we are at position 0 in a one-dimensional line. Now |walks[i]| represents the number of steps we have walked. When walks[i] is positive it indicates we walked right, and negative for left. When we walk, we move one block at a time to the next or previous integer position. We have to find the number of blocks that have been walked on at least target number of times. So, if the input is like walks = [3, -7, 2] and target = 2, then the output will ...
Read MoreProgram to count number of overlapping islands in two maps in Python
Suppose we have two binary matrices mat1 and mat2. Here 1 represents land and 0 represents water. If there is a group of 1s (land) surrounded by water, it's called an island. We need to find the number of islands that exist in both mat1 and mat2 at the exact same coordinates. Problem Understanding Given two matrices, we need to count overlapping islands where both matrices have land (1) at the same positions. For example, if mat1 = 101 100 100 And mat2 = 101 100 101 ...
Read MoreProgram to find index, where we can insert element to keep list sorted in Python
Finding the correct insertion index to maintain sorted order is a common problem that can be efficiently solved using binary search. We need to find the rightmost position where we can insert the target element while keeping the list sorted in ascending order. Given a sorted list nums and a target value, we want to find the index where the target should be inserted. If the target already exists, we return the largest possible index (after all existing occurrences). Problem Example For nums = [1, 5, 6, 6, 8, 9] and target = 6, the output should ...
Read MoreProgram to find how many updates required to make string half monotonous in Python
Suppose we have a lowercase string s whose length is even. We have to find the minimum number of characters that need to be updated such that one of the following three conditions is satisfied for all i, where 0 ≤ i < n/2 and j, n/2 ≤ j < n − s[i] > s[j] (left half characters greater than right half) s[i] < s[j] (left half characters less than right half) s[i] == s[j] (left half characters equal to right half) So, if the input is ...
Read MoreProgram to find minimum costs needed to fill fruits in optimized way in Python
Suppose we have a list called fruits and two values k and cap. Each fruits[i] contains three values: [c, s, t], where fruit i costs c each, has size s, and there are t total fruits available. The k represents number of fruit baskets with capacity cap. We want to fill the fruit baskets with the following constraints in this order ? Each basket can only hold same type fruits Each basket should be as full as possible Each basket should be as cheap as possible ...
Read MoreProgram to find minimum cost to send same number of people to two different cities in Python
Suppose we have a list called costs, where costs[i] has [c1, c2] indicating that for person i it costs c1 amount to reach city 0 and c2 amount to reach city 1. We want the same number of people to go to city 0 as city 1, and we need to find the minimum cost required. So, if the input is like costs = [[2, 6], [10, 3], [4, 9], [5, 8]], then the output will be 17, because person 0 and 2 will go to city 0 and person 1 and 3 to city 1. For city 0, ...
Read MoreProgram to check first player can win by reaching total sum to target in Python
Suppose we have two numbers k and target. Now consider Amal and Bimal are playing a game. In each round, Amal picks a number from 1 to k to add to the total score that initially starts from 0. Whoever crosses the total to target wins. Amal always plays first, we have to check whether he can force a win if both of them play optimally. So, if the input is like k = 5, target = 10, then the output will be True, as if Amal picks 4 first, then whether Bimal picks 1, 2, ..., or 5, ...
Read MoreProgram to find indices or local peaks in Python
A local peak is an element that is greater than or equal to its neighbors. In Python, we can find indices of local peaks by comparing each element with its adjacent elements. A peak can be a single element or a plateau (consecutive equal elements that are peaks). Peak Definition An index i is a peak when these conditions are met: The next different number is either absent or smaller than nums[i] The previous different number is either absent or smaller than nums[i] There is at least one different number on either side Algorithm ...
Read MoreProgram to find sum of the minimums of each sublist from a list in Python
Suppose we have a list of numbers called nums. We have to find the sum of minimum values for every possible sublist in nums. If the answer is too large, then mod the result by 10^9 + 7. So, if the input is like nums = [5, 10, 20, 10, 0], then the output will be 90 because the sublists are [[5], [10], [20], [10], [0], [5, 10], [10, 20], [20, 10], [10, 0], [5, 10, 20], [10, 20, 10], [20, 10, 0], [5, 10, 20, 10], [10, 20, 10, 0], [5, 10, 20, 10, 0]], and their minimum ...
Read MoreProgram to check every sublist in a list containing at least one unique element in Python
Suppose we have a list of elements called nums, we have to check whether every sublist has at least 1 element in it that occurs exactly once in the sublist or not. We have to solve this problem in linear time. So, if the input is like nums = [5, 10, 20, 10, 0], then the output will be True, because every sublist in nums has at least one element which has occurred only once. [[5], [10], [20], [10], [0], [5, 10], [10, 20], [20, 10], [10, 0], [5, 10, 20], [10, 20, 10], [20, 10, 0], [5, 10, ...
Read More