Arnab Chakraborty

Arnab Chakraborty

3,768 Articles Published

Articles by Arnab Chakraborty

Page 112 of 377

Program to find minimum required chances to form a string with K unique characters in Python

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

Suppose we have a string s of lowercase alphabet characters, and another number k. We need to find the minimum number of required changes in the string so that the resulting string has at most k distinct characters. A change means modifying a single character to any other character. So, if the input is like s = "wxxyyzzxx", k = 3, then the output will be 1, as we can change the letter "w" to get 3 distinct characters (x, y, and z). Approach To solve this problem, we will follow these steps − count ...

Read More

Program to find the K-th last node of a linked list in Python

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

Suppose we have a singly linked list, we have to find the value of the k-th last node (0-indexed) in a single pass. This is a classic two-pointer technique problem. So, if the input is like node = [5, 4, 6, 3, 4, 7], k = 2, then the output will be 3, as the second last (index 3) node has the value of 3. Algorithm To solve this, we will follow these steps − Initialize two pointers: klast and last, both pointing to the head Move the last pointer k steps ahead Move both ...

Read More

Program to find maximum sum of popped k elements from a list of stacks in Python

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

This problem involves finding the maximum sum by popping exactly k elements from a list of stacks. We can only pop elements from the top of each stack, making this a dynamic programming problem where we explore different combinations of popping elements. The key insight is that for each stack, we can choose to pop 0, 1, 2, ... up to all elements from that stack, and recursively solve for the remaining stacks with the remaining number of elements to pop. Algorithm Approach We use a recursive function that takes the current stack index and the number ...

Read More

Program to find a list of numbers where each K-sized window has unique elements in Python

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

Given a list of numbers and a window size k, we need to find the count of distinct numbers in each sliding window of size k. This is a common sliding window problem that can be efficiently solved using a dictionary to track element frequencies. So, if the input is like nums = [2, 2, 3, 3, 4], k = 2, then the output will be [1, 2, 1, 2], as the windows are [2, 2], [2, 3], [3, 3], and [3, 4]. Algorithm To solve this, we will follow these steps − ...

Read More

Program to find the perimeter of an island shape in Python

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

Finding the perimeter of an island shape in a binary matrix involves counting the exposed edges of all connected 1s. Each cell with value 1 contributes to the perimeter based on how many of its four sides are exposed (not adjacent to another 1). Problem Understanding Given a binary matrix where 0 represents empty cells and 1 represents land blocks, we need to calculate the total perimeter. Each land block initially has 4 sides, but we subtract 1 for each adjacent land block. For the input matrix: 00000 00111 00110 ...

Read More

Program to interleave list elements from two linked lists in Python

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

Suppose we have two linked lists l1 and l2, we have to return one linked list by interleaving elements of these two lists starting with l1. If there are any leftover nodes in a linked list, they should be appended to the result. So, if the input is like l1 = [5, 4, 6, 3, 4, 7] l2 = [8, 6, 9], then the output will be [5, 8, 4, 6, 6, 9, 3, 4, 7] Algorithm To solve this, we will follow these steps − ans := l1 ...

Read More

Program to count n digit integers where digits are strictly increasing in Python

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

Suppose we have a number n, we have to find the number of n-digit positive integers such that the digits are in strictly increasing order. So, if the input is like n = 3, then the output will be 84, as numbers are 123, 124, 125, ..., 678, 789. Understanding the Problem For n-digit numbers with strictly increasing digits, we need to choose n different digits from {1, 2, 3, ..., 9}. We cannot use 0 as the first digit in an n-digit number. This becomes a combination problem: C(9, n). Algorithm To solve this, ...

Read More

Program to find minimum number of heights to be increased to reach destination in Python

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

Sometimes we need to find the minimum height increase required to reach from the top-left corner to the bottom-right corner of a matrix. We can only move to adjacent cells if their height is less than or equal to our current cell's height, but we can increase any cell's height before moving. Problem Understanding Given a matrix M where M[r][c] represents the height of that cell, we need to find the minimum total height increase to create a path from (0, 0) to (R-1, C-1). Movement is allowed to adjacent cells (up, down, left, right) only if the ...

Read More

Program to group a set of points into k different groups in Python

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

Suppose we have a list of points and a number k. The points are in the form (x, y) representing Cartesian coordinates. We can group any two points p1 and p2 if the Euclidean distance between them is ≤ k. We have to find the total number of disjoint groups. So, if the input is like points = [[2, 2], [3, 3], [4, 4], [11, 11], [12, 12]], k = 2, then the output will be 2, as it can make two groups: ([2, 2], [3, 3], [4, 4]) and ([11, 11], [12, 12]). Algorithm To solve ...

Read More

Program to count number of strings we can make using grammar rules in Python

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

Suppose we have a number n, we have to find the number of strings of length n can be generated using the following grammar rules ? Each character is a lower case vowel [a, e, i, o, u] "a" may only be followed by one "e" "e" may only be followed by any of "a" and "i" "i" may not be followed by another "i" "o" may only be followed by any of "i" and "u" "u" may only be followed by one "a" If the result is very large, mod the result by 10^9 + ...

Read More
Showing 1111–1120 of 3,768 articles
« Prev 1 110 111 112 113 114 377 Next »
Advertisements