Arnab Chakraborty

Arnab Chakraborty

3,768 Articles Published

Articles by Arnab Chakraborty

Page 53 of 377

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 476 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 699 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 751 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 864 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 664 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

Program to find minimum distance to the target element using Python

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

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

Program to replace all digits with characters using Python

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

Suppose we have an alphanumeric string that contains lowercase English letters in its even positions and digits in its odd positions. We need to perform a shift operation where each digit at odd position i gets replaced with the character obtained by shifting the letter at position i-1 by the digit value. The shift operation works as follows: shift('p', 5) = 'u' and shift('a', 0) = 'a'. For every odd index i, we replace the digit s[i] with shift(s[i-1], s[i]). Problem Example If the input is s = "a2b1d4f3h2", then the output will be "acbcdhfihj" because ? ...

Read More

Program to find sum of digits in base K using Python

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

Converting a number from base 10 to base K and finding the sum of its digits is a common programming problem. We need to repeatedly divide the number by K and sum up all the remainders. So, if the input is like n = 985 k = 8, then the output will be 12 because the number 985 in octal is 1731, so the digit sum is 1+7+3+1 = 12. Algorithm To solve this, we will follow these steps ? Initialize ans := 0 while n >= k, do ans := ans + n ...

Read More
Showing 521–530 of 3,768 articles
« Prev 1 51 52 53 54 55 377 Next »
Advertisements