Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by Arnab Chakraborty
Page 161 of 377
Min Stack in Python
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 MoreLinked List Cycle in Python
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 MoreHamming Distance in Python
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 MoreFizz Buzz in Python
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 MoreMove Zeroes in Python
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 MoreMissing Number in Python
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 MoreValid Anagram in Python
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 MoreInvert Binary Tree in Python
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 MoreReverse Linked List in Python
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 MoreHouse Robber in Python
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