Sum of Even Numbers After Queries in Python

Arnab Chakraborty
Updated on 25-Mar-2026 07:20:11

3K+ Views

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 More

Jewels and Stones in Python

Arnab Chakraborty
Updated on 25-Mar-2026 07:19:46

843 Views

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 More

Array Partition I in Python

Sunidhi Bansal
Updated on 25-Mar-2026 07:19:25

2K+ Views

Given an array of 2n integers, we need to group them into n pairs such that the sum of minimum values from each pair is maximized. This is known as the Array Partition I problem. Problem Understanding For an array like [1, 2, 3, 4], we can form pairs in different ways ? (1, 2) and (3, 4) → min(1, 2) + min(3, 4) = 1 + 3 = 4 (1, 4) and (2, 3) → min(1, 4) + min(2, 3) = 1 + 2 = 3 (1, 3) and (2, 4) → min(1, 3) + ... Read More

Diameter of Binary Tree in Python

Arnab Chakraborty
Updated on 25-Mar-2026 07:19:02

678 Views

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 More

Intersection of Two Arrays II in Python

Arnab Chakraborty
Updated on 25-Mar-2026 07:18:41

5K+ Views

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 More

First Bad Version in Python

Arnab Chakraborty
Updated on 25-Mar-2026 07:18:21

1K+ Views

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 More

Lowest Common Ancestor of a Binary Search Tree in Python

Arnab Chakraborty
Updated on 25-Mar-2026 07:17:59

738 Views

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 More

Happy Number in Python

Arnab Chakraborty
Updated on 25-Mar-2026 07:17:28

2K+ Views

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 More

Reverse Bits in C++

Arnab Chakraborty
Updated on 25-Mar-2026 07:17:09

6K+ Views

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 More

Intersection of Two Linked Lists in Python

Arnab Chakraborty
Updated on 25-Mar-2026 07:16:49

1K+ Views

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

Advertisements