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 160 of 377
Pairs of Songs With Total Durations Divisible by 60 in Python
Finding pairs of songs with total durations divisible by 60 is a common algorithmic problem. We can solve this efficiently using the remainder approach with a hash map to track remainders when dividing by 60. Problem Understanding Given a list of song durations, we need to count pairs where the sum is divisible by 60. For example, with [30, 20, 150, 100, 40], we get 3 pairs: (30, 150), (20, 100), and (20, 40) since 180, 120, and 60 are all divisible by 60. Algorithm Approach The key insight is using modular arithmetic: Two ...
Read MoreSum of Even Numbers After Queries in Python
Given an array of integers A and a list of queries, we need to process each query and calculate the sum of even numbers after each modification. For the i-th query, value = queries[i][0] and index = queries[i][1], we add the value to A[index] and then find the sum of all even values in the updated array. Problem Understanding Let's understand with an example. If the array is [1, 2, 3, 4] and queries are [[1, 0], [-3, 1], [-4, 0], [2, 3]]: Initial array: [1, 2, 3, 4], sum of even numbers: 2 + 4 ...
Read MoreJewels and Stones in Python
The Jewels and Stones problem is a classic programming challenge where we need to count how many stones are actually jewels. Given a string J representing jewel types and string S representing stones, we count how many characters in S appear in J. The comparison is case-sensitive. For example, if J = "aZc" and S = "catTableZebraPicnic", we need to count occurrences of 'a', 'Z', and 'c' in the stones string. Method 1: Using Dictionary (Hash Set) Create a dictionary to store jewel types for O(1) lookup, then iterate through stones ? class Solution: ...
Read MoreDiameter 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 More