Programming Articles

Page 566 of 2547

Shortest Path with Alternating Colors in Python

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

Suppose we have a directed graph with nodes labelled 0, 1, ..., n-1. In this graph, each edge is colored with either red or blue colors, and there could be self-edges or parallel edges. Each [i, j] in red_edges indicates a red directed edge from node i to node j. Similarly, each [i, j] in blue_edges indicates a blue directed edge from node i to node j. We need to find an array answer of length n, where each answer[X] is the length of the shortest path from node 0 to node X such that the edge colors alternate along ...

Read More

Lowest Common Ancestor of Deepest Leaves in Pytho

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

In a rooted binary tree, we need to find the lowest common ancestor (LCA) of the deepest leaves. The deepest leaves are the leaf nodes that are furthest from the root. Key concepts: A leaf node has no children The depth of the root is 0, and each child's depth is parent's depth + 1 The LCA is the deepest node that contains all target nodes in its subtree Algorithm We use a recursive approach that returns both the depth and the LCA node: If a node is null, return depth 0 ...

Read More

Maximum Average Subtree in Pytho

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

Given the root of a binary tree, we need to find the maximum average value of any subtree in that tree. A subtree's average is calculated by dividing the sum of all nodes in the subtree by the count of nodes. 5 6 1 Maximum average: 6.0 (node 6) For the example tree above ? Node 5 subtree: (5 + ...

Read More

Path In Zigzag Labelled Binary Tree in Python

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

In an infinite binary tree where every node has two children, nodes are labeled in a zigzag pattern. Odd-numbered rows (1st, 3rd, 5th...) are labeled left to right, while even-numbered rows (2nd, 4th, 6th...) are labeled right to left. 1 3 2 4 5 6 7 ...

Read More

The Earliest Moment When Everyone Become Friends in C++

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

The problem asks us to find the earliest moment when everyone in a social group becomes acquainted with everyone else. This is a classic Union-Find (Disjoint Set Union) problem where we need to track connected components and determine when all people form a single connected group. Given N people with IDs from 0 to N-1 and a list of friendship logs, each log contains [timestamp, person_A, person_B]. We need to find the earliest timestamp when all people are connected through friendships. Algorithm Overview We use the Union-Find data structure with the following approach − Sort ...

Read More

Broken Calculator in Python

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

A broken calculator problem involves finding the minimum operations needed to transform one number into another using only two allowed operations: double (multiply by 2) and decrement (subtract 1). Given an initial number X displayed on the calculator, we need to reach the target number Y using the minimum number of operations. Problem Understanding The calculator supports only two operations: Double − Multiply the current number by 2 Decrement − Subtract 1 from the current number For example, if X = 5 and Y = 8, we can reach 8 from 5 in ...

Read More

Insert Delete GetRandom O(1) in Python

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

The Insert Delete GetRandom O(1) problem requires designing a data structure that supports three operations in average O(1) time: inserting elements, removing elements, and returning a random element with equal probability. Problem Requirements insert(val) − Insert an item val to the set if not already present remove(val) − Remove an item val from the set if it exists getRandom() − Return a random element with equal probability for all elements Solution Approach We use two data structures to achieve O(1) operations: Dictionary (present) − Maps values to their indices in the array ...

Read More

Flatten Nested List Iterator in Python

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

A nested list iterator flattens a multi-level list structure into a single sequence. Each element can be either an integer or another list containing integers or nested lists. Problem Statement Given a nested list like [[1, 1], 2, [1, 1]], we need to create an iterator that returns elements in flattened order: 1, 1, 2, 1, 1. Algorithm The solution uses recursion to flatten the nested structure during initialization ? Initialize an empty result list and index counter Recursively traverse the nested list using getVal() If element is integer, add to result list If ...

Read More

Course Schedule in Python

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

The Course Schedule problem asks whether we can complete all courses given their prerequisites. This is essentially a cycle detection problem in a directed graph where courses are nodes and prerequisites are edges. Problem Understanding Given a number of courses and prerequisite pairs, we need to determine if it's possible to finish all courses. For example, if we have 2 courses and prerequisites = [[1, 0]], it means to take course 1, we must first complete course 0. This is possible, so we return True. Algorithm Approach We solve this using Depth-First Search (DFS) with cycle ...

Read More

Minimum Cost Tree From Leaf Values in Python

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

The Minimum Cost Tree From Leaf Values problem asks us to build a binary tree from an array where each leaf corresponds to array elements in order, and each internal node's value equals the product of the maximum leaf values in its left and right subtrees. We need to find the minimum sum of all internal node values. Given an array of positive integers, we must construct binary trees such that: Each node has either 0 or 2 children Array values correspond to leaf values in an inorder traversal Each non-leaf node equals the product of the ...

Read More
Showing 5651–5660 of 25,466 articles
« Prev 1 564 565 566 567 568 2547 Next »
Advertisements