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
Python Articles
Page 109 of 855
Program 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 MoreProgram to find longest subarray of 1s after deleting one element using Python
Suppose we have a binary array called nums, we can delete one element from it. We have to find the size of the longest non-empty subarray which is containing only 1's in the resulting array. If there is no such subarray, then return 0. So, if the input is like nums = [1, 0, 1, 1, 1, 0, 1, 1, 0], then the output will be 5 because by removing 0 from position 5, we can get a subarray [1, 1, 1, 1, 1] there are five 1s. Algorithm To solve this, we will follow these steps ...
Read MoreProgram to find the kth factor of n using Python
Finding the kth factor of a number efficiently requires a smart approach. Instead of finding all factors, we can find factors up to the square root and use mathematical properties to determine the kth factor without generating the complete list. Algorithm Approach The key insight is that factors come in pairs. For a number n, if i is a factor, then n/i is also a factor. We only need to find factors up to √n and use this pairing property. Step-by-Step Solution Here's how the algorithm works ? from math import floor def ...
Read MoreProgram to make file names unique using Python
When creating multiple files or directories with the same name, file systems automatically add suffixes to make each name unique. This problem simulates that behavior by adding (k) suffixes where k is the smallest positive integer that keeps the name unique. So, if the input is like names = ["my_dir", "my_dir(1)", "my_new_dir", "my_new_dir", "abc"], then the output will be ['my_dir', 'my_dir(1)', 'my_new_dir', 'my_new_dir(1)', 'abc'] because "my_new_dir" appears twice, so the second occurrence gets suffix "(1)". Algorithm To solve this, we will follow these steps − Create a dictionary to track name ...
Read MoreProgram to find minimum number of days to make m bouquets using Python
Suppose we have an array with integers called bloomDay, along with two values m and k. We need to make m bouquets, where each bouquet requires k adjacent flowers from the garden. The garden has n different flowers, and the ith flower will bloom on day bloomDay[i]. Each flower can be used in only one bouquet. We need to find the minimum number of days to wait to make m bouquets from the garden. If we cannot make m bouquets, return -1. Problem Example If the input is bloomDay = [5, 5, 5, 5, 10, 5, 5], m ...
Read MoreProgram to find least number of unique integers after K removals using Python
Given an array of integers and a number k, we need to find the minimum number of unique elements remaining after removing exactly k elements. The strategy is to remove elements with the lowest frequency first to minimize unique elements. For example, if we have nums = [5, 4, 2, 2, 4, 4, 3] and k = 3, we can remove 5 (frequency 1), 3 (frequency 1), and one occurrence of 2 (frequency 2). This leaves us with 2 unique elements: 2 and 4. Algorithm The approach involves these steps ? Count ...
Read MoreProgram to find two non-overlapping sub-arrays each with target sum using Python
Given an array and a target sum, we need to find two non-overlapping sub-arrays where each has a sum equal to the target. If multiple solutions exist, we return the one with the minimum combined length of both sub-arrays. So, if the input is like arr = [5, 2, 6, 3, 2, 5] and target = 5, then the output will be 2. There are three subarrays with sum 5: [5], [3, 2], and [5]. We can choose two sub-arrays of length 1 each, giving us a minimum combined length of 2. Algorithm Approach We use a ...
Read MoreProgram to find maximum population year using Python
Given a table with birth and death years of people, we need to find the earliest year with the maximum population. A person is counted as alive during year y if y is in the range [birth, death - 1] (they are not counted in their death year). For example, with the following data: Birth Death 1970 2010 1960 2020 1940 1970 We need to find the year when the most people were alive simultaneously. Algorithm We will use a dictionary to count the population ...
Read More