Arnab Chakraborty

Arnab Chakraborty

3,768 Articles Published

Articles by Arnab Chakraborty

Page 161 of 377

Min Stack in Python

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

A Min Stack is a special data structure that supports all standard stack operations (push, pop, top) plus retrieving the minimum element in constant O(1) time. This is achieved by storing the previous minimum value alongside each new minimum. Algorithm The key insight is to store the previous minimum when pushing a new minimum value ? Initialize the stack with min element as infinity For push(x): if x ≤ current min, store the old min in stack first, then update min to x For pop(): if popped element equals current min, restore the previous min from ...

Read More

Linked List Cycle in Python

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

A linked list cycle occurs when a node in the linked list points back to a previous node, creating a loop. To detect cycles, we can use a hash set to track visited nodes or implement Floyd's cycle detection algorithm (tortoise and hare). The cycle is represented by a pos parameter indicating where the tail connects. If pos = -1, no cycle exists. For example, in the list [5, 3, 2, 0, -4, 7] with pos = 1, the tail connects to the second node (index 1), creating a cycle. Using Hash Set Approach This method tracks ...

Read More

Hamming Distance in Python

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

The Hamming distance between two integers is the number of positions where the corresponding bits differ. For example, if we have numbers 7 and 15, they are 0111 and 1111 in binary respectively. Only the most significant bit differs, so the Hamming distance is 1. Algorithm Steps To calculate the Hamming distance, we follow these steps ? Iterate through each bit position from 31 down to 0 Extract the bit at position i for both numbers using right shift and bitwise AND Compare the bits ? if they differ, increment the distance counter Return the total ...

Read More

Fizz Buzz in Python

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

The Fizz Buzz problem is a classic programming challenge where we replace numbers with specific strings based on divisibility rules. For numbers from 1 to n, we print "Fizz" for multiples of 3, "Buzz" for multiples of 5, and "FizzBuzz" for multiples of both. Problem Rules If the number is divisible by 3, write "Fizz" instead of the number If the number is divisible by 5, write "Buzz" instead of the number If the number is divisible by both 3 and 5, write "FizzBuzz" instead ...

Read More

Move Zeroes in Python

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

Sometimes we need to move all zero values in an array to the right while maintaining the relative order of non-zero elements. For example, transforming [0, 1, 5, 0, 3, 8, 0, 0, 9] into [1, 5, 3, 8, 9, 0, 0, 0, 0]. Algorithm Approach We use a two-pointer technique to solve this problem efficiently ? Use an insert_index pointer to track where to place the next non-zero element Iterate through the array and move non-zero elements to the front Fill remaining positions ...

Read More

Missing Number in Python

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

Suppose we have a list of numbers from 0 to n. There is one number that is missing. We have to find the missing number in an efficient approach. So if A = [0, 1, 2, 3, 4, 5, 7, 8, 9], missing number is 6. Python provides multiple approaches to solve this problem efficiently. We'll explore three common methods: binary search, mathematical formula, and XOR operation. Method 1: Using Binary Search The binary search approach works by comparing array values with their indices after sorting ? Sort the list in ascending order Set high ...

Read More

Valid Anagram in Python

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

Anagrams are words or phrases formed by rearranging the letters of another word or phrase, using all the original letters exactly once. For example, "ANAGRAM" and "NAAGARM" are anagrams because they contain the same letters in different arrangements, but "cat" and "fat" are not anagrams because they have different letters. To check if two strings are valid anagrams, we can use several approaches. The most common method is to sort the characters of both strings and compare them. Method 1: Using Sorting Convert both strings to sorted character lists and compare them ? def is_anagram_sorting(s, ...

Read More

Invert Binary Tree in Python

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

Inverting a binary tree means swapping the left and right child nodes for every node in the tree. This creates a mirror image of the original tree structure. Original Tree 4 2 7 1 3 6 ...

Read More

Reverse Linked List in Python

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

A linked list is a linear data structure where elements are stored in nodes, and each node contains data and a pointer to the next node. Reversing a linked list means changing the direction of pointers so that the last node becomes the first node. For example, if we have: 1 → 3 → 5 → 7, the reversed list will be: 7 → 5 → 3 → 1 Algorithm We can reverse a linked list using a recursive approach ? Define a recursive function solve(head, back) where back tracks the previous node If head ...

Read More

House Robber in Python

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

The House Robber problem is a classic dynamic programming challenge. A robber wants to rob houses along a street, but cannot rob two adjacent houses as this would trigger an alarm. We need to find the maximum amount that can be robbed. Given an array where each element represents the amount of money in each house, we must determine the maximum sum possible without selecting adjacent elements. Problem Understanding For the array [2, 7, 10, 3, 1], the optimal strategy is to rob houses at indices 0, 2, and 4 (values 2, 10, 1) for a total ...

Read More
Showing 1601–1610 of 3,768 articles
« Prev 1 159 160 161 162 163 377 Next »
Advertisements