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
Python Articles
Page 685 of 855
Diameter of Binary Tree in Python
The diameter of a binary tree is the length of the longest path between any two nodes in the tree. This path may or may not pass through the root node. We can solve this problem using depth-first search (DFS) to calculate the height of each subtree and track the maximum diameter found. 1 2 3 4 5 ...
Read MoreIntersection of Two Arrays II in Python
Finding the intersection of two arrays means identifying common elements between them, preserving duplicates based on their frequency in both arrays. For example, if A = [1, 4, 5, 3, 6] and B = [2, 3, 5, 7, 9], the intersection will be [3, 5]. Algorithm Steps To solve this problem efficiently, we follow these steps: Take two arrays A and B If length of A is smaller than length of B, swap them for optimization Calculate the frequency of elements in the larger ...
Read MoreFirst Bad Version in Python
In software development, when a version fails quality checks, all subsequent versions are also considered bad since they're built upon the faulty version. The First Bad Version problem asks us to find the earliest bad version efficiently using binary search. We have versions numbered from 1 to n, and an API function isBadVersion(version) that returns True if the version is bad, False otherwise. Our goal is to find the first bad version with minimum API calls. Algorithm Overview We use binary search to efficiently locate the first bad version ? If n < 2, return ...
Read MoreLowest Common Ancestor of a Binary Search Tree in Python
The Lowest Common Ancestor (LCA) of two nodes in a binary tree is the lowest node that has both nodes as descendants. In a binary search tree, we can use the BST property to find the LCA efficiently. 6 2 8 0 4 7 9 ...
Read MoreHappy Number in Python
A Happy Number is a positive integer where repeatedly replacing it with the sum of squares of its digits eventually leads to 1. If this process results in an endless cycle that never reaches 1, the number is not happy. How Happy Numbers Work Let's trace through the number 19 to see why it's happy ? 1² + 9² = 1 + 81 = 82 8² + 2² = 64 + 4 = 68 6² + 8² = 36 + 64 = 100 1² + 0² + 0² = 1 + 0 + 0 = 1 ...
Read MoreReverse Bits in C++
Reversing bits means flipping the binary representation of a number from left to right. For a 32-bit unsigned integer, the leftmost bit becomes the rightmost bit and vice versa. Understanding Bit Reversal Given a 32-bit number like 00000000000000000000001001110100, we need to reverse it to 00101110010000000000000000000000. The process involves extracting each bit from the original number and placing it in the reversed position. Algorithm The bit reversal algorithm works as follows ? Initialize result to 0 For each bit position from 31 down to 0: Extract the least significant bit of the input number ...
Read MoreIntersection of Two Linked Lists in Python
When two linked lists share common nodes, we need to find their intersection point. The intersection occurs where both lists reference the same node object, not just nodes with the same value. Given two linked lists A and B, we return the reference to the first common node. If there's no intersection, we return None. Algorithm We use a hash map to track visited nodes from the first list, then check if any node from the second list exists in our map ? Create a dictionary to store nodes from list A Traverse list A ...
Read MoreMin 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 More