Programming Articles

Page 512 of 2547

Program to find minimum distance that needs to be covered to meet all person in Python

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

This problem asks us to find the optimal meeting point in a 2D grid where people can gather with minimum total walking distance. We have a grid with walls (1), empty spaces (0), and people (2), and need to find the location that minimizes the sum of distances all people must travel. Problem Setup Given a 2D matrix with: 0 represents an empty cell 1 represents a wall 2 represents a person A person can move in four directions (up, down, left, right). We need to find a non-wall cell that minimizes the total ...

Read More

Program to find minimum number of cells it will take to reach bottom right corner in Python

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

Suppose we have a 2D grid representing a maze where 0 represents an empty space and 1 represents a wall. We start at grid[0][0] and need to find the minimum number of cells it takes to reach the bottom-right corner. If we cannot reach the destination, we return -1. Problem Example Consider this grid ? 0 0 0 1 0 0 1 0 0 The output will be 5 because the shortest path requires 5 steps. Algorithm Steps We use Breadth-First Search (BFS) to find the ...

Read More

Program to schedule tasks to take smallest amount of time in Python

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

Suppose we have a list of values called tasks where each different value represents a different task type, and we also have a non-negative integer k. Each task takes one minute to complete, but we must wait k minutes between doing two tasks of the same type. At any time, we can be doing a task or waiting. We have to find the smallest amount of time it takes to complete all the tasks. So, if the input is like tasks = [2, 2, 2, 3, 3, 2], k = 1, then the output will be 7, as the ...

Read More

Program to check whether given tree is symmetric tree or not in Python

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

A symmetric binary tree is one that looks identical to its mirror image. To check if a tree is symmetric, we need to verify that the left subtree is a mirror reflection of the right subtree. Symmetric Tree 1 2 2 3 4 4 ...

Read More

Program to check whether two trees can be formed by swapping nodes or not in Python

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

Binary trees can sometimes be transformed into each other by swapping left and right subtrees at any node. This problem checks whether two given trees can be made identical through such swapping operations. Algorithm Overview The solution uses level-order traversal (BFS) to compare both trees level by level. At each level, we check if the node values can match either in the same order or in reverse order (indicating a possible swap). Step-by-Step Approach Use two queues to store nodes from both trees level by level For ...

Read More

Program to check whether each node value except leaves is sum of its children value or not in Python

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

A binary tree has a special property when each internal node's value equals the sum of its children's values. We need to check if this property holds for all non-leaf nodes in the tree. In this problem, we traverse the tree recursively and verify that each internal node satisfies the sum condition. Algorithm Steps The solution uses a depth-first search (DFS) approach ? If the current node is null, return True (empty subtree is valid) If the current node is a leaf (no children), return True ...

Read More

Program to find three unique elements from list whose sum is closest to k Python

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

Given a list of numbers and a target value k, we need to find three unique elements whose sum is closest to k and return the absolute difference between their sum and k. For example, if nums = [2, 5, 25, 6] and k = 14, the triplet [2, 5, 6] gives us the sum 13, which is closest to 14 with an absolute difference of 1. Approach We'll use a two-pointer technique after sorting the array: Sort the input list to enable two-pointer traversal For each element as the first element of the triplet, ...

Read More

Program to check number of triplets from an array whose sum is less than target or not Python

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

Given a list of numbers and a target value, we need to find the count of triplets (i < j < k) where the sum nums[i] + nums[j] + nums[k] is less than the target. For example, with nums = [−2, 6, 4, 3, 8] and target = 12, we have 5 valid triplets: [−2, 6, 4], [−2, 6, 3], [−2, 4, 3], [−2, 4, 8], [−2, 3, 8]. Algorithm We use a three-pointer approach after sorting the array ? Sort the array to enable efficient two-pointer technique For each element at index i, use ...

Read More

Program to check we can find four elements whose sum is same as k or not in Python

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

Suppose we have a list of numbers called nums and a value k, we have to check whether there are four unique elements in the list that add up to k. So, if the input is like nums = [11, 4, 6, 10, 5, 1] and k = 25, then the output will be True, as we have [4, 6, 10, 5] whose sum is 25. Algorithm To solve this, we will follow these steps − Sort the list nums n := size of nums For i in range 0 to n − 4, do ...

Read More

Program to check whether one tree is subtree of other or not in Python

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

Suppose we have two binary trees. We have to check whether the second tree is a subtree of the first one or not. A subtree means that there exists a node in the main tree such that the subtree rooted at that node has the same structure and node values as the given target tree. So, if the input is like ? Main Tree: 6 4 10 ...

Read More
Showing 5111–5120 of 25,466 articles
« Prev 1 510 511 512 513 514 2547 Next »
Advertisements