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 49 of 377
Program to find valid matrix given row and column sums in Python
Suppose we have two arrays rowSum and colSum with non-negative values where rowSum[i] has the sum of the elements in the ith row and colSum[j] has the sum of the elements in the jth column of a 2D matrix. We have to find any matrix with non-negative values of size (rowSum size x colSum size) that satisfies the given rowSum and colSum values. So, if the input is like rowSum = [13, 14, 12] and colSum = [9, 13, 17], then the output will be − ...
Read MoreProgram to find maximum non negative product in a matrix in Python
Finding the maximum non-negative product path in a matrix is a dynamic programming problem. We need to traverse from the top-left corner (0, 0) to the bottom-right corner (m-1, n-1), moving only right or down, while tracking both maximum and minimum products at each cell. Problem Understanding Given an m x n matrix, we need to find a path from top-left to bottom-right that maximizes the product of all elements along the path. Since negative numbers can become positive when multiplied by other negatives, we track both maximum and minimum values at each position. For the example ...
Read MoreProgram to find split a string into the max number of unique substrings in Python
Given a string s, we need to find the maximum number of unique substrings that the string can be split into. Each substring must be non-empty, and when concatenated together, they should form the original string. The key constraint is that all substrings must be different from each other. For example, if we have s = "pqpqrrr", we can split it as ['p', 'q', 'pq', 'r', 'rr'] to get 5 unique substrings. However, splitting it as ['p', 'q', 'p', 'q', 'r', 'rr'] is invalid because 'p' and 'q' appear multiple times. Algorithm We use backtracking with a ...
Read MoreProgram to make sum divisible by P in Python
Given an array nums and a value p, we need to remove the smallest subarray such that the sum of remaining elements is divisible by p. We return the length of the smallest subarray to remove, or -1 if no such subarray exists. For example, with nums = [8, 2, 6, 5, 3] and p = 7, removing element 3 gives us a remaining sum of 21, which is divisible by 7. Algorithm The approach uses prefix sums and modular arithmetic ? s := remainder when total sum is divided by p Use a dictionary ...
Read MoreProgram to find maximum sum obtained of any permutation in Python
Given an array nums and an array requests where requests[i] = [start_i, end_i], each request asks for the sum of elements from nums[start_i] to nums[end_i] inclusive. We need to find the maximum total sum of all requests among all permutations of nums. The answer should be returned modulo 10^9+7. Problem Understanding For example, if nums = [10, 20, 30, 40, 50] and requests = [[1, 3], [0, 1]], we want to arrange the array to maximize the sum. The optimal arrangement [30, 50, 40, 20, 10] gives ? Request [1, 3]: nums[1] + nums[2] + nums[3] ...
Read MoreProgram to find minimum cost to connect all points in Python
Suppose we have an array called points with some coordinates in the form (x, y). The cost of connecting two points (xi, yi) and (xj, yj) is the Manhattan distance between them, calculated as |xi - xj| + |yi - yj|. We need to find the minimum cost to connect all points using a Minimum Spanning Tree (MST) approach. So, if the input is like points = [(0, 0), (3, 3), (2, 10), (6, 3), (8, 0)], then the output will be 22. (0, 0) ...
Read MoreProgram to count number of unhappy friends in Python
In this problem, we need to count unhappy friends based on their preferences and pairings. A friend is unhappy if they are paired with someone but prefer another person who also prefers them back over their current partners. Problem Definition Given preferences and pairs, a friend x is unhappy if: x is paired with y There exists a friend u (paired with v) such that: x prefers u over y, and u prefers x over v Algorithm Steps We solve this by building a preference graph: Create a graph ...
Read MoreProgram to find minimum deletion cost to avoid repeating letters in Python
Suppose we have a string s and another array of integers called cost where cost[i] represents the cost of deleting the ith character in s. We have to find the minimum cost of deletions such that there are no two same letters next to each other. We have to keep in mind that we will delete the chosen characters at the same time. So after deleting a character, the costs of deleting other characters will not change. So, if the input is like s = "pptpp", cost = [2, 3, 4, 5, 2], then the output will be 4 ...
Read MoreProgram to find number of ways where square of number is equal to product of two numbers in Python
Suppose we have two arrays nums1 and nums2, we have to find the number of triplets formed (type 1 and type 2) following these two rules − Triplet (i, j, k) if nums1[i]^2 = nums2[j] * nums2[k] where [0
Read MoreProgram to find shortest subarray to be removed to make array sorted in Python
Given an array, we need to find the shortest subarray to remove so that the remaining elements are in non-decreasing (sorted) order. This problem involves finding the longest possible sorted subsequence that can be formed by keeping elements from both ends of the array. So, if the input is like arr = [10, 20, 30, 100, 40, 20, 30, 50], then the output will be 3 because we can remove [100, 40, 20] which is the smallest subarray of length 3, and by removing these all remaining elements are in non-decreasing order [10, 20, 30, 30, 50]. Algorithm ...
Read More