Python Articles

Page 108 of 855

Program to count minimum number of animals which have no predator in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 1K+ Views

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 More

Program to separate persons where no enemies can stay in same group in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 1K+ Views

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 More

Program to find in which interval how many tasks are worked on in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 162 Views

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 More

Program to rotate square matrix by 90 degrees counterclockwise in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 2K+ Views

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 More

Program to check robot can reach target by keep moving on visited spots in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 320 Views

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 More

Program to find minimum number of rocketships needed for rescue in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 219 Views

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 More

Program to reverse linked list by groups of size k in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 464 Views

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

Program to reverse the directed graph in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 1K+ Views

A directed graph reversal means changing the direction of all edges − if an edge goes from node u to v, it becomes an edge from v to u. Given an adjacency list representation, we need to create the reverse graph where all edge directions are flipped. Problem Understanding Consider a directed graph with nodes numbered from 0 to n-1. If the original graph has an edge from node i to node j, the reversed graph will have an edge from node j to node i. Original Graph ...

Read More

Program to reverse a linked list in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 598 Views

A linked list is a linear data structure where elements are stored in nodes, and each node contains data and a pointer to the next node. Reversing a linked list means changing the direction of pointers so that the last node becomes the first node. For example, if we have a linked list: 2 → 4 → 6 → 8, the reversed list will be: 8 → 6 → 4 → 2. Approach We can reverse a linked list using a recursive approach with these steps − Define a recursive function ...

Read More

Program to remove duplicate entries in a linked list in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 275 Views

Suppose we have a linked list of numbers, we have to remove those numbers which appear multiple times in the linked list (hold only one occurrence in the output), we also have to maintain the order of the appearance in the original linked list. So, if the input is like [2 −> 4 −> 6 −> 1 −> 4 −> 6 −> 9], then the output will be [2 −> 4 −> 6 −> 1 −> 9]. Algorithm To solve this, we will follow these steps − if node is not null, then l ...

Read More
Showing 1071–1080 of 8,549 articles
« Prev 1 106 107 108 109 110 855 Next »
Advertisements