Arnab Chakraborty

Arnab Chakraborty

3,768 Articles Published

Articles by Arnab Chakraborty

Page 53 of 377

Program to count number of swaps required to change one list to another in Python?

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

Suppose we have two lists of numbers L1 and L2, the length of each list is n and each value is unique to its list, and values are in range 1 to n. We need to find the minimum number of adjacent swaps required to transform L1 to L2. So, if the input is like L1 = [0, 1, 2, 3] and L2 = [2, 0, 1, 3], then the output will be 2. We can swap 1 and 2 to get [0, 2, 1, 3], then swap 0 and 2 to get [2, 0, 1, 3], which matches ...

Read More

Program to find maximum profit we can get by buying and selling stocks with a fee in Python?

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

When trading stocks with transaction fees, we need to find the maximum profit possible by buying and selling stocks any number of times. Each sell transaction incurs a fee that reduces our profit. The problem can be solved using dynamic programming with recursion. We track two states: whether we currently hold stock (flag=1) or not (flag=0). Example Let's say we have stock prices [2, 10, 4, 8] and a transaction fee of 3. The optimal strategy is: Buy at price 2, sell at price 10: profit = 10 - 2 - 3 = 5 Buy ...

Read More

Program to find number of places are safe when bomb explodes in Python?

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

Suppose we have a 2D binary matrix where 1 represents a bomb and 0 represents an empty cell. When a bomb explodes, all spaces along the same row and column are damaged. We need to find the number of safe positions where we can stand without getting damaged. So, if the input is like ? 1 1 0 0 0 0 0 0 0 Then the output will be 2, as the bottom-right cell (2, 2) and the middle-right cell (1, 2) are safe positions. Approach To ...

Read More

Program to find minimum possible sum by changing 0s to 1s k times from a list of numbers in Python?

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

Given a list of numbers and a value k, we need to perform k operations where each operation flips a 0 bit to 1 in any number's binary representation. The goal is to minimize the final sum of all numbers. For example, if nums = [4, 7, 3] and k = 2, the binary representations are: 4 → 100 (binary) 7 → 111 (binary) 3 → 011 (binary) We can flip two 0 bits in 4 (positions 0 and 1) to make it 111 (7). Final sum: 7 + 7 + 3 = 17. ...

Read More

Program to find starting index of the child who receives last balloon in Python?

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

Suppose we have n children standing in a circle, waiting to receive balloons. The distribution starts with the kth child (indexing from 0), and after giving them a balloon, they leave the circle. The process continues clockwise, with every kth child receiving a balloon until only one child remains. We need to find the starting index of the child who receives the last balloon. This is a variation of the famous Josephus problem, where we eliminate every kth person from a circle. Problem Example If the input is n = 3 and k = 2, then the ...

Read More

Program to find higher number with same number of set bits as n in Python?

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

Given a number n, we need to find the smallest next higher number that has the same number of set bits (1s) in its binary representation. For example, if n = 7 (binary: 0111), the next higher number with three 1s is 11 (binary: 1011). Algorithm The approach involves bit manipulation to rearrange the bits ? Count trailing zeros and ones from the rightmost bit Find the rightmost non-trailing zero and flip it to 1 Clear all bits to the right of this position Set the required number of 1s at the rightmost positions ...

Read More

Program to find column index where left most 1 is present in a binary matrix in Python?

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

Suppose we have a 2D binary matrix where each row is sorted in ascending order with 0s coming before 1s. We need to find the leftmost column index that contains the value 1. If no such column exists, return -1. So, if the input is like ? 0 0 0 1 0 0 1 1 0 0 1 1 0 0 1 0 Then the output will be 2, as column index 2 has the leftmost 1 in the entire matrix. Algorithm To solve this ...

Read More

Program to convert linked list by alternating nodes from front and back in Python

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

Given a singly linked list, we need to rearrange it by alternating nodes from the back and front. The pattern is: last node, first node, second-last node, second node, and so on. For example, if the input is [1, 2, 3, 4, 5, 6, 7, 8, 9], the output will be [9, 1, 8, 2, 7, 3, 6, 4, 5]. Algorithm To solve this problem, we follow these steps − Store all node values in a list for easy access from both ends Traverse the linked list again and assign values alternately from the back ...

Read More

Program to find number of ways we can arrange symbols to get target in Python?

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

Suppose we have a list of non-negative numbers called nums and also have an integer target. We have to find the number of ways to arrange + and - signs in front of nums such that the expression equals the target. So, if the input is like nums = [2, 3, 3, 3, 2] target = 9, then the output will be 2, as we can have −2 + 3 + 3 + 3 + 2 and 2 + 3 + 3 + 3 − 2. Algorithm Approach This problem can be transformed into a subset sum ...

Read More

Program to find number of arithmetic sequences from a list of numbers in Python?

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

Finding arithmetic sequences from a list of numbers is a common problem in programming. An arithmetic sequence is a sequence where the difference between consecutive numbers remains constant. We need to count all contiguous arithmetic subsequences of length ≥ 3. For example, in the list [6, 8, 10, 12, 13, 14], we have arithmetic sequences: [6, 8, 10], [8, 10, 12], [6, 8, 10, 12], and [12, 13, 14]. Algorithm Approach The key insight is to use a sliding window approach: Track consecutive elements that form arithmetic sequences When a sequence breaks, calculate how many ...

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