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 on Trending Technologies
Technical articles with clear explanations and examples
Program to find best team with no conflicts in Python
Suppose we have two lists called scores and ages, where scores[i] and ages[i] represent the score and age of the ith player in a basketball game. We want to select the team with the highest overall score, where the team score is the total sum of scores of all selected players. However, we must avoid conflicts − a conflict exists if a younger player has a strictly higher score than an older player. So, if the input is like scores = [5, 7, 9, 14, 19], ages = [5, 6, 7, 8, 9], then the output will be 54 ...
Read MoreProgram to find lexicographically smallest string after applying operations in Python
Suppose we have a string s with only numeric digits and also have two values a and b. We can apply any one of the following two operations any number of times and in any order on s ? Add 'a' to all odd positioned items of s (0-indexed). If digit is 9, then by adding something with it will be cycled back to 0. Rotate 's' to the right by b positions. We have to find the lexicographically smallest string we can get by applying the above operations any number of times on s. ...
Read MoreProgram to find number of sets of k-non-overlapping line segments in Python
Suppose we have n points on a line, where the ith point (from 0 to n-1) is at position x = i, we have to find the number of ways we can draw exactly k different non-overlapping line segments such that each segment covers two or more points. The endpoints of each line segment must have integral coordinates. The k line segments do not have to cover all given n points, and they can share endpoints. If the answer is too large, then return result mod 10^9+7. So, if the input is like n = 4, k = 2, ...
Read MoreProgram to find maximal network rank in Python
The maximal network rank problem asks us to find the maximum combined connectivity between any two cities in a road network. The network rank of two cities is the total number of roads directly connected to either city, counting shared roads only once. Given a network where roads[i] = [u, v] represents a bidirectional road between cities u and v, we need to find the pair of cities with the highest combined connectivity. Understanding the Problem For any two cities, their network rank equals: Number of roads connected to city 1 Plus number of roads connected ...
Read MoreProgram 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 More