Program to find length of longest contiguous sublist with same first letter words in Python

Arnab Chakraborty
Updated on 26-Mar-2026 15:17:54

297 Views

Suppose we have a list of lowercase alphabet strings called words. We have to find the length of the longest contiguous sublist where each word has the same first letter. So, if the input is like words = ["she", "sells", "seashells", "on", "the", "sea", "shore"], then the output will be 3, because the longest contiguous sublist is ["she", "sells", "seashells"] where each word starts with 's'. Algorithm To solve this, we will follow these steps − Initialize cnt = 1 to track current sequence length Initialize maxcnt = 0 to track maximum length found Initialize ... Read More

Matplotlib – How to plot the FFT of signal with correct frequencies on the X-axis?

Rishikesh Kumar Rishi
Updated on 26-Mar-2026 15:17:32

3K+ Views

To plot the FFT (Fast Fourier Transform) of a signal with correct frequencies on the X-axis in matplotlib, we need to properly compute the frequency bins and visualize the power spectrum. Understanding FFT Frequency Calculation The key to plotting FFT with correct frequencies is using np.fft.fftfreq() which returns the discrete Fourier Transform sample frequencies. For real signals, we typically plot only the positive frequencies using np.fft.rfftfreq(). Basic FFT Plot with Normalized Frequencies Here's how to create a basic FFT plot with normalized frequencies ? import numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] ... Read More

Program to find number of possible position in n-person line with few person at front and back in Python

Arnab Chakraborty
Updated on 26-Mar-2026 15:17:06

247 Views

Suppose we have three numbers n, a and b. Consider we are in a line of n people, and we are unaware about our position in the line. But we know there are at least a number of people in front of us and at most b number of people behind us. We have to find the number of possible positions for us. So, if the input is like n = 10, a = 3, b = 4, then the output will be 5. There are 10 people in the line with at least 3 people in front and ... Read More

Program to find lexicographically largest mountain list in Python

Arnab Chakraborty
Updated on 26-Mar-2026 15:16:50

372 Views

A mountain list is a sequence that strictly increases to a peak and then strictly decreases. Given three positive numbers n, lower, and upper, we need to find the lexicographically largest mountain list of length n with all values in range [lower, upper]. The challenge is ensuring both increasing and decreasing parts are non-empty while maximizing the lexicographic order. Problem Analysis For a valid mountain list of length n: Must have at least one increasing element and one decreasing element Total possible unique values = upper − lower + 1 Maximum mountain length = 2 ... Read More

Program to find maximum sum by performing at most k negate operations in Python

Arnab Chakraborty
Updated on 26-Mar-2026 15:16:27

335 Views

Suppose we have a list of elements called nums and another value k. We can perform an operation where we select an element from nums and negate it. We can perform exactly k number of operations and need to find the maximum resulting sum. So, if the input is like nums = [2, 1, -6, -2] and k = 3, then the output will be 9. If we negate -6 and -2 and 1, we get [2, -1, 6, 2] and its sum is 9. Algorithm To solve this problem, we follow these steps − ... Read More

Program to find value for which given array expression is maximized in Python

Arnab Chakraborty
Updated on 26-Mar-2026 15:16:07

318 Views

Suppose we have two arrays called nums and values, both contain integers where the values of nums are strictly increasing and their lengths are the same. We have to find the maximum value of v for a pair of indices i, j such that i ≤ j that maximizes v = values[i] + values[j] + nums[j] - nums[i]. So, if the input is like nums = [1, 2, 7] and values = [-4, 6, 5], then the output will be 16. If we pick i = 1 and j = 2 we get 6 + 5 + 7 - ... Read More

Program to find number of elements in matrix follows row column criteria in Python

Arnab Chakraborty
Updated on 26-Mar-2026 15:15:46

765 Views

Suppose we have a binary matrix; we have to find the number of elements in matrix that follows the following rules − matrix[r, c] = 1 matrix[r, j] = 0 for every j when j is not equal to c and matrix[i, c] = 0 for every i when i is not equal to r. In simple terms, we need to find cells that contain 1 and are the only 1 in both their row and column. Example Input So, if the input is like ... Read More

Program to find k where k elements have value at least k in Python

Arnab Chakraborty
Updated on 26-Mar-2026 15:15:25

387 Views

Suppose we have a list of numbers called nums, that contains only non-negative numbers. If there are exactly k number of elements in nums that are greater than or equal to k, find the value k. If we cannot find such value, then return -1. So, if the input is like nums = [6, 4, 0, 8, 2, 9], then the output will be 4, because there are exactly 4 elements that are greater than or equal to 4: [6, 4, 8, 9]. Algorithm To solve this, we will follow these steps − ... Read More

Program to find array of length k from given array whose unfairness is minimum in python

Arnab Chakraborty
Updated on 26-Mar-2026 15:15:08

654 Views

Suppose we have an array A and another value k. We have to form an array of size k by taking elements from A and minimize the unfairness. Here the unfairness is calculated by this formula − (𝑚𝑎𝑥𝑖𝑚𝑢𝑚 𝑜𝑓 𝑎𝑟𝑟) − (𝑚𝑖𝑛𝑖𝑚𝑢𝑚 𝑜𝑓 𝑎𝑟𝑟) So, if the input is like A = [25, 120, 350, 150, 2500, 25, 35] and k = 3, then the output will be 10, because we can take elements [25, 25, 35] so max(arr) = 35 and min(arr) = 25. So their difference is 10. Approach The key insight is that ... Read More

Program to find total duration of K most watched shows in Python

Arnab Chakraborty
Updated on 26-Mar-2026 15:14:45

216 Views

Suppose we have a list of strings called shows, a list of integers called durations, and a value k. Here shows[i] and durations[i] represent a show and its duration watched by the ith person. We need to find the total duration watched of the k most watched shows. So, if the input is like shows = ["The BGT", "Jack jumper", "The BGT", "Jokers Company", "Music magic"] durations = [10, 8, 10, 18, 9] k = 2, then the output will be 38, as the top 2 most watched shows are "Jokers Company" and "The BGT" with total durations of ... Read More

Advertisements