Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by Arnab Chakraborty
Page 50 of 377
Program to find maximum length of subarray with positive product in Python
When working with arrays containing positive and negative numbers, we often need to find the maximum length of a subarray where the product of all elements is positive. A product is positive when there's an even number of negative values in the subarray. So, if the input is like nums = [2, -2, -4, 5, -3], then the output will be 4 because the first four elements [2, -2, -4, 5] form a subarray with product 2×(-2)×(-4)×5 = 80, which is positive. Algorithm Approach To solve this problem, we need to ? Split the array ...
Read MoreProgram to find out the greatest subarray of a given length in python
Suppose we have an array containing various integer values and a given length k. We have to find out the greatest subarray from the array of the given length. A subarray is said to be greater than another subarray if at the first differing position, the first subarray has a larger element than the second subarray. So, if the input is like nums = [5, 3, 7, 9], k = 2, then the output will be [7, 9]. Algorithm To solve this, we will follow these steps ? start := size of nums − k ...
Read MorePython Program to find out the sum of values in hyperrectangle cells
A hyperrectangle is a multi-dimensional rectangle with k dimensions. Each dimension has a length denoted as n1, n2, n3, ..., nm. The hyperrectangle's cells are addressed as (p, q, r, ...) and contain a value equivalent to the GCD (Greatest Common Divisor) of their coordinates. Our task is to find the sum of all cell values gcd(p, q, r, ...) where 1 ≤ p ≤ n1, 1 ≤ q ≤ n2, and so on. Problem Understanding Given input_arr = [[2, 2], [5, 5]], we need to calculate the sum for two test cases ? First instance [2, ...
Read MoreProgram to check if number of compass usage to get out of a maze is enough in Python
In this problem, we need to determine if we can escape from a maze using a compass at most k times. The maze is represented as a matrix where 'S' is the starting position, 'D' is the destination (exit), '-' represents walkable paths, and 'O' represents blocked cells. The compass is used when we have multiple path choices at any position. Understanding the Problem The key insight is that we only need to use the compass when we have multiple valid moves from our current position. If there's only one valid path, we can move without using the ...
Read MoreProgram to find out the minimum number of moves for a chess piece to reach every position in Python
In chess, different pieces have unique movement patterns. This program solves the problem of finding the minimum number of moves for a special chess piece to reach every position on an n×n chessboard from the starting position (0, 0). The special piece moves in an L-shape pattern defined by parameters a and b. From position (x1, y1), it can move to (x2, y2) where: x2 = x1 ± a, y2 = y1 ± b x2 = x1 ± b, y2 = y1 ± a Algorithm Approach We use Breadth-First Search (BFS) to find the ...
Read MoreProgram to change the root of a binary tree using Python
Suppose we are given a binary tree and need to change its root to a specific leaf node. This transformation involves restructuring the tree by following specific rules to maintain the binary tree structure while making the leaf node the new root. Transformation Rules To change the root of a binary tree, we follow these rules − If a node has a left child, it becomes the right child. A node's parent becomes its left child. In this process, the parent node's link to that node becomes null, so it will have only one child. ...
Read MoreProgram to fix a erroneous binary tree using Python
When a binary tree has an erroneous connection where a node's right child pointer incorrectly points to another node at the same level, we need to identify and fix this error. The solution involves detecting the problematic node and removing it along with its descendants, except for the node it erroneously points to. Consider this example where node 4's right child incorrectly points to node 6: 5 3 7 ...
Read MoreProgram to find out the lowest common ancestor of a binary tree using parent pointers using Python
The Lowest Common Ancestor (LCA) of two nodes in a binary tree is the lowest node that has both nodes as descendants. When parent pointers are available, we can efficiently find the LCA by traversing upward from one node and checking if we encounter the other node's ancestors. Node Structure The tree node contains parent pointers for upward traversal ? TreeNode: data: left: right: parent: Algorithm The approach follows these steps ? ...
Read MoreProgram to find out the lowest common ancestor of a binary tree using Python
The Lowest Common Ancestor (LCA) of two nodes in a binary tree is the deepest node that has both nodes as descendants. A node can be considered a descendant of itself. This problem is commonly solved using recursive traversal. So, if the input is like: 5 3 7 2 ...
Read MoreProgram to add two polynomials given as linked lists using Python
Suppose we are given two polynomials represented as linked lists and we need to find their addition. Each polynomial is stored as a linked list where each node contains a coefficient, power, and pointer to the next node. We need to return a new linked list representing the sum of the two polynomials. For example, if we have polynomials 1x^1 + 1x^2 and 2x^1 + 3x^0, then the output will be 3x^1 + 1x^2 + 3x^0. Algorithm To solve this, we will follow these steps − Create dummy and node pointers ...
Read More