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 106 of 377
Program to find smallest intersecting element of each row in a matrix in Python
Suppose we have a 2D matrix where each row is sorted in ascending order. We have to find the smallest number that exists in every row. If there's no such result, then return −1. So, if the input is like ? 2 3 5 5 10 10 1 3 5 then the output will be 5 because it's the smallest element present in all three rows. Algorithm To solve this, we will follow these steps ? If matrix is empty, then return −1 ...
Read MoreProgram to split a set into equal sum sets where elements in first set are smaller than second in Python
We need to split a list of numbers into two groups A and B such that they have equal sums, and every number in A is strictly smaller than every number in B. For example, with nums = [3, 4, 5, 12], we can split into A = [3, 4, 5] and B = [12], both having sum 12. Algorithm The key insight is that after sorting, we need to find a split point where: All elements before the split form group A All elements from the split onwards form group B Both groups have ...
Read MoreProgram to count total number of set bits of all numbers in range 0 to n in Python
Suppose we have a number n. For each number i in the range 0 ≤ i ≤ n, we need to count the number of set bits (1's) in their binary representation and return the total count. For example, if n = 5, the numbers are [0, 1, 2, 3, 4, 5] with binary representations [0, 1, 10, 11, 100, 101]. The count of 1's are [0, 1, 1, 2, 1, 2], so the total is 7. Approach We use dynamic programming with bit manipulation. The key insight is that for any number i, if i & (i-1) ...
Read MoreProgram to count minimum number of animals which have no predator in Python
Suppose we have a list of numbers called nums where nums[i] shows the predator of the ith animal and if there is no predator, it will hold −1. We have to find the smallest number of groups of animals such that no animal is in the same group with its direct or indirect predator. So, if the input is like nums = [1, 2, −1, 4, 5, −1], then the output will be 3, as we can have the groups like: [0, 3], [1, 4], [2, 5]. Approach To solve this, we will follow these steps − ...
Read MoreProgram to separate persons where no enemies can stay in same group in Python
Suppose we have a number n and a 2D matrix called enemies. Here n indicates there are n people labeled from [0, n - 1]. Now each row in enemies contains [a, b] which means that a and b are enemies. We have to check whether it is possible to partition the n people into two groups such that no two people that are enemies are in the same group. So, if the input is like n = 4, enemies = [[0, 3], [3, 2]], then the output will be True, as we can have these two groups [0, ...
Read MoreProgram to find in which interval how many tasks are worked on in Python
Suppose we have a list of intervals where each interval is like [start, end) and we also have a list of strings called types. Now for a given i, the intervals[i] shows the times someone worked on the job types[i] from [start, end). Two intervals of the same type never overlap or touch. So we have to find a sorted merged list where each item has [start, end, num_types], indicates from start to end, num_types number of tasks were being worked on. So, if the input is like intervals = [[0, 3], [5, 7], [0, 7]] types = ["problem ...
Read MoreProgram to rotate square matrix by 90 degrees counterclockwise in Python
Rotating a square matrix 90 degrees counterclockwise is a common matrix transformation problem. We can achieve this using a two-step approach: first reverse each row, then transpose the matrix. Understanding the Rotation Let's visualize how a 90-degree counterclockwise rotation works: Original Matrix 1 4 7 2 5 8 3 ...
Read MoreProgram to check robot can reach target by keep moving on visited spots in Python
Suppose we have a robot that is currently sitting at position (0, 0) on a Cartesian plane. If we have a list of moves containing N(North), S(South), W(West), and E(East), the robot has a special behavior: when it reaches a spot it has visited before, it continues moving in the same direction until it reaches a new unvisited spot. We need to check whether after all its moves, it will end at a target coordinate (x, y). ...
Read MoreProgram to find minimum number of rocketships needed for rescue in Python
Suppose we have a list of numbers called weights representing people's weights and a value limit that determines the weight limit of one rocket ship. Each rocket ship can take at most two people. We need to find the minimum number of rocket ships required to rescue everyone. So, if the input is like weights = [300, 400, 300], limit = 600, then the output will be 2. One rocket ship takes the two people with weights 300 each (total 600), and another takes the person with weight 400. Algorithm Approach To solve this problem efficiently, we ...
Read MoreProgram to reverse linked list by groups of size k in Python
Suppose we have a singly linked list and another value k, we have to reverse every k contiguous group of nodes in the list. So, if the input is like List = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 3, then the output will be [3, 2, 1, 6, 5, 4, 9, 8, 7, 10]. Algorithm To solve this, we will follow these steps − Create a dummy node to simplify edge cases Use two pointers: one to track the previous group ...
Read More