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 684 of 855
Number of Equivalent Domino Pairs in Python
When working with domino pairs, we need to find equivalent dominos where one can be flipped to match another. Two dominos D[i] = [a, b] and D[j] = [c, d] are equivalent if a = c and b = d, or a = d and b = c (since dominos can be reversed). We need to count all pairs (i, j) where 0 ≤ i < j < length of dominos list. For example, with dominos [[1, 2], [2, 1], [3, 4], [6, 5]], we have one equivalent pair: [1, 2] and [2, 1]. Algorithm To solve ...
Read MoreRelative Sort Array in Python
Suppose we have two arrays arr1 and arr2, where elements of arr2 are unique, and all elements in arr2 are also present in arr1. We need to sort the elements of arr1 such that the relative ordering of items follows the sequence in arr2. Elements not present in arr2 should be placed at the end in ascending order. For example, if arr1 is [2, 3, 1, 3, 2, 4, 6, 7, 9, 2, 19] and arr2 is [2, 1, 4, 3, 9, 6], the result will be [2, 2, 2, 1, 4, 3, 3, 9, 6, 7, 19]. ...
Read MoreDistribute Candies to People in Python
The candy distribution problem involves giving candies to people in increasing amounts across multiple rounds. In the first round, person 1 gets 1 candy, person 2 gets 2 candies, and so on. In subsequent rounds, the candy count continues incrementally from where it left off. Problem Understanding Given candies total candies and n people, we distribute candies in rounds ? Round 1: Give 1, 2, 3, ..., n candies Round 2: Give n+1, n+2, n+3, ..., 2n candies Continue until candies run out The last person gets remaining candies (if any) Algorithm Steps ...
Read MoreOccurrences After Bigram in Python
When working with text analysis, you might need to find words that appear after a specific bigram (a sequence of two consecutive words). This problem involves finding all words that come immediately after a given pair of words in a text. For example, in the text "lina is a good girl she is a good singer", if we're looking for words that follow the bigram "a good", the result would be ["girl", "singer"]. Algorithm To solve this problem, we follow these steps: Split the text into individual words Create an empty result list Iterate through ...
Read MoreLast Stone Weight in Python
The Last Stone Weight problem simulates repeatedly smashing the two heaviest stones together until at most one stone remains. When two stones with weights x and y collide (where x ≤ y), they either both get destroyed if equal, or the lighter stone is destroyed and the heavier one becomes y - x. Problem Rules If x = y, then both stones are totally destroyed If x ≠ y, the stone of weight x is destroyed, and the stone of weight y becomes y - x For example, with stones [2, 7, 4, 1, 8, ...
Read MorePartition Array Into Three Parts With Equal Sum in Python
Given an array of integers, we need to determine if we can partition it into three non-empty parts with equal sums. This means finding two partition points that divide the array into three contiguous subarrays where each subarray has the same sum. Formally, we need to find indices i and j where i+1 < j such that: Sum of elements from index 0 to i equals Sum of elements from index i+1 to j-1 equals Sum of elements from index j to end For example, with input [0, 2, 1, -6, 6, -7, 9, 1, ...
Read MorePairs 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 MoreArray Partition I in Python
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