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 52 of 377
Program to find minimum swaps to arrange a binary grid using Python
When working with binary matrices, we sometimes need to arrange rows so that all elements above the major diagonal are 0. This can be achieved by swapping adjacent rows. Let's explore how to find the minimum number of swaps required. Problem Understanding Given an n x n binary matrix, we need to swap adjacent rows to ensure all elements above the major diagonal are 0. If no solution exists, we return -1. For example, with this matrix: 010 011 100 We need to rearrange it so that elements above the diagonal are all ...
Read MoreProgram to find number of good leaf nodes pairs using Python
A binary tree contains good leaf node pairs when the shortest path between two different leaf nodes is less than or equal to a given distance d. Consider this binary tree with distance d = 4 ? 1 2 3 4 5 6 8 ...
Read MoreProgram to make a bulb switcher using binary string using Python
Suppose we have n bulbs in a room, numbered from 0 to n-1 and arranged in a row from left to right. Initially, all bulbs are turned off (0-state). We need to achieve a target configuration represented by a binary string where '1' means the bulb is on and '0' means it's off. The bulb switcher has a special flipping operation: Select any bulb index i Flip each bulb from index i to index n-1 (flip all bulbs from position i to the end) We need to find the minimum number of flips required to ...
Read MoreProgram to find number of good ways to split a string using Python
Suppose we have a string s. A split is said to be a good split when we can split s into 2 non-empty strings p and q where their concatenation equals s and the number of distinct letters in p and q are equal. We need to find the number of good splits we can make in s. For example, if the input is s = "xxzxyx", then the output will be 2. We can split it as ("xxz", "xyx") or ("xxzx", "yx") — both are good splits since each part has the same number of distinct characters. ...
Read MoreProgram to find number of nodes in the sub-tree with the same label using Python
In this problem, we have a rooted tree with n nodes numbered from 0 to n-1, where each node has a label (lowercase English letter). We need to find the count of nodes in each node's subtree that have the same label as that node. Problem Understanding Given a tree structure and node labels, we need to return an array where each element represents the count of nodes with the same label in that node's subtree (including the node itself). 0(c) ...
Read MoreProgram to find path with maximum probability using Python
Suppose we have an undirected weighted graph with n nodes (nodes are numbered from 0 onwards). This graph is given as input using an edge list, and for each edge e, it has a probability of success of traversing that edge probability[e]. We also have start and end nodes, and we need to find the path with the maximum probability of success to go from start to end and return its success probability. If we cannot find any path, then return 0. Problem Example Consider the following graph: ...
Read MoreProgram to find minimum difference between largest and smallest value in three moves using Python
Given an array nums, we can change any element to any value in one move. The goal is to find the minimum difference between the largest and smallest values after performing at most 3 moves. The strategy is to either remove the largest elements or smallest elements (or a combination) to minimize the range. Since we have 3 moves, we can consider removing 0-3 smallest and 3-0 largest elements respectively. Algorithm The approach works as follows ? If array size ≤ 4, we can make all elements equal in 3 moves, so return 0 Sort ...
Read MoreProgram to find range sum of sorted subarray sums using Python
Suppose we have an array nums with n positive elements. We need to compute the sum of all non-empty contiguous subarrays of nums, sort them in non-decreasing order, and then find the sum of elements from index left to index right (1-indexed) in the sorted array. The answer should be returned modulo 10^9 + 7. Problem Understanding For example, if nums = [1, 5, 2, 6], left = 1, and right = 5: All subarray sums are: 1, 5, 2, 6, 6, 7, 8, 8, 13, 14 After ...
Read MoreProgram to count submatrices with all ones using Python
Given an m x n binary matrix, we need to count how many submatrices contain all ones. A submatrix is a contiguous rectangular area within the matrix. So, if the input matrix is ? 1 0 1 0 1 1 0 1 1 Then the output will be 13 as there are 6 (1x1) matrices, 3 (2x1) matrices, 2 (1x2) matrices, 1 (3x1) matrix and 1 (2x2) matrix. Algorithm Approach We use dynamic programming to solve this efficiently ? Create a DP matrix where dp[i][j] ...
Read MoreProgram to count number of square submatrices with all ones using Python
In this problem, we need to count all possible square submatrices that contain only ones in a binary matrix. This is solved using dynamic programming where we track the largest square ending at each position. Problem Understanding Given a binary matrix, we count squares of all sizes. For the example matrix ? Matrix: ...
Read More