Python Articles

Page 668 of 855

Number of Dice Rolls With Target Sum in Python

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

The problem asks us to find the number of ways to roll d dice (each with f faces) such that the sum equals a target value. We need to return the result modulo 109 + 7. For example, with 2 dice having 6 faces each and target sum 7, there are 6 ways: (1, 6), (2, 5), (3, 4), (4, 3), (5, 2), (6, 1). Algorithm Approach We'll use dynamic programming where dp[i][j] represents the number of ways to achieve sum j using i+1 dice ? Base case: For the first die, there's exactly 1 ...

Read More

Minimum Swaps to Group All 1's Together in Python

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

In a binary array, we often need to group all 1's together with minimum swaps. This problem uses a sliding window approach with prefix sums to find the optimal position for grouping all 1's. Algorithm Approach The key insight is to use a sliding window of size equal to the total count of 1's. We find the window position that already contains the maximum number of 1's, minimizing the swaps needed. Steps Count total number of 1's in the array Create a prefix sum array for efficient range sum queries Use sliding window of size ...

Read More

Snapshot Array in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 666 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 598 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 455 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 490 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 753 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 235 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 231 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 285 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
Showing 6671–6680 of 8,549 articles
« Prev 1 666 667 668 669 670 855 Next »
Advertisements