Python Articles

Page 109 of 855

Program to find range sum of sorted subarray sums using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 453 Views

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 More

Program to count submatrices with all ones using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 706 Views

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 More

Program to count number of square submatrices with all ones using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 261 Views

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

Program to find longest subarray of 1s after deleting one element using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 475 Views

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 More

Program to find the kth factor of n using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 698 Views

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 More

Program to make file names unique using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 1K+ Views

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 More

Program to find minimum number of days to make m bouquets using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 749 Views

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 More

Program to find least number of unique integers after K removals using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 860 Views

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 More

Program to find two non-overlapping sub-arrays each with target sum using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 256 Views

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 More

Program to find maximum population year using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 662 Views

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
Showing 1081–1090 of 8,549 articles
« Prev 1 107 108 109 110 111 855 Next »
Advertisements