Programming Articles

Page 17 of 2547

Snapshot Array in Python

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

A Snapshot Array is a data structure that allows you to take snapshots of an array at different points in time and retrieve values from those snapshots. This is useful when you need to track the history of changes to array elements. Interface Requirements The SnapshotArray must support these operations: SnapshotArray(int length) − Initialize array with given length, all elements start as 0 set(index, val) − Set element at given index to val snap() − Take a snapshot and return snapshot ID (starts from ...

Read More

Binary Tree Coloring Game in Python

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

The Binary Tree Coloring Game is a two-player strategy game played on a binary tree. Each player colors nodes to control territory, and the winner is determined by who colors more nodes. As the second player, we need to determine if we can guarantee a win by choosing the optimal starting node. Game Rules The game works as follows: Player 1 chooses node x and colors it red Player 2 chooses node y and colors it blue Players alternate turns coloring adjacent uncolored nodes The game ends when no moves are possible Winner is the player who ...

Read More

Decrease Elements To Make Array Zigzag in Python

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

Suppose we have an array of integers, where a move operation involves choosing any element and decreasing it by 1. An array is a zigzag array if it satisfies either of these patterns: Every even-indexed element is greater than adjacent elements: A[0] > A[1] < A[2] > A[3] < A[4] > ... and so on. Every odd-indexed element is greater than adjacent elements: A[0] < A[1] > A[2] < A[3] > A[4] < ... and so on. We need to find the minimum number of moves to transform the given array into a zigzag array. ...

Read More

Connecting Cities With Minimum Cost in Pytho

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

Connecting cities with minimum cost is a classic Minimum Spanning Tree (MST) problem. Given N cities and connections with costs, we need to find the minimum cost to connect all cities. This can be solved using Kruskal's algorithm with a Union-Find data structure. Problem Understanding We have N cities numbered from 1 to N, and connections where each connection [city1, city2, cost] represents the cost to connect two cities. We need to find the minimum cost to ensure every pair of cities has a path between them ? ...

Read More

Shortest Path with Alternating Colors in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 749 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 229 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 223 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 273 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 179 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 414 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
Showing 161–170 of 25,469 articles
« Prev 1 15 16 17 18 19 2547 Next »
Advertisements