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
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
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
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
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
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
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
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
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
We need to find the minimum distance from a start position to any occurrence of a target element in an array. The distance is calculated as the absolute difference between indices. Given an array nums, a target value that exists in the array, and a start index, we find the index i where nums[i] = target and |i - start| is minimized. Example If nums = [3, 4, 5, 6, 7], target = 7, and start = 2, the target 7 is found at index 4. The distance is |4 - 2| = 2. Algorithm ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Economics & Finance