Python Articles

Page 335 of 855

Program to minimize hamming distance after swap operations in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 370 Views

The Hamming distance between two arrays is the number of positions where elements differ. This problem asks us to minimize the Hamming distance between two arrays src and tgt by performing allowed swap operations on the source array. The key insight is that allowed swaps create connected components of indices. Within each component, we can rearrange elements optimally to match the target array as much as possible. Algorithm Overview We use a Union-Find (Disjoint Set) data structure to group indices that can be swapped with each other. For each group, we count mismatches and calculate the minimum ...

Read More

Python program to check a number n is weird or not

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 2K+ Views

A number is considered weird based on specific conditions involving whether it's odd or even and its range. Understanding these conditions helps us write a program to classify any given number. Weird Number Conditions A number n is weird when ? The number is odd The number is not in range 2 to 5 The number is even and in range 6 to 20 So, if the input is n = 18, then the output will be "Weird" because it is even and in range 6 to 20. Algorithm To solve this, ...

Read More

Program to find kpr sum for all queries for a given list of numbers in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 383 Views

We need to find the kpr sum for each query in a list. Given a list of numbers and queries containing [k, p, r], we calculate the sum using XOR operations on pairs of elements within a specified range. The formula for kpr_sum is: $$\mathrm{{𝑘𝑝𝑟}\_{𝑠𝑢𝑚} =\sum_{\substack{𝑖=𝑃}}^{𝑅−1}\sum_{\substack{𝑗=𝑖+1}}^{𝑅}(𝐾 ⊕(𝐴[𝑖]⊕𝐴[𝑗]))}$$ If the sum exceeds the limit, we return the result modulo 10^9+7. Understanding the Problem For each query [k, p, r], we: Take all pairs (i, j) where p ≤ i < j ≤ r Calculate k XOR (nums[i] XOR nums[j]) for each pair Sum all ...

Read More

Program to swap nodes in a linked list in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 1K+ Views

Swapping the kth node from the start with the kth node from the end in a linked list is a common problem. We need to locate both nodes and swap their values while maintaining the list structure. Problem Understanding Given a linked list and a value k, we swap the kth node from the beginning with the kth node from the end. For example, if we have nodes [1, 5, 6, 7, 1, 6, 3, 9, 12] and k=3, the 3rd node from start (6) swaps with the 3rd node from end (3). Algorithm Steps The ...

Read More

Program to find out if k monitoring stations are enough to monitor particular points in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 188 Views

Suppose there is a sensor module that can monitor its nearby environment up to a radius of r. There are some things in the lattice point of the module's monitoring circle that need to be monitored. So, k number of low-powered modules are placed so that they can monitor only those specific points. Given the square of the radius and k number of low-powered modules, we shall have to find out if the points can be monitored correctly. We return true if monitoring is possible, otherwise, we return false. Problem Understanding If the input is like square of ...

Read More

Program to find maximum weighted sum for rotated array in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 280 Views

When we have an array and need to find the maximum weighted sum after rotating it, we calculate the sum where each element is multiplied by its position (1-indexed). The weighted sum formula is: $$\mathrm{S=\sum_{i=0}^{n-1}(i+1) \times nums[i]}$$ For example, with array [5, 3, 4], we need to check all possible rotations and find the maximum weighted sum. Understanding the Problem Let's see how weighted sum works for array L = [5, 3, 4]: Original array [5, 3, 4]: sum = 1×5 + 2×3 + 3×4 = 5 + 6 + 12 = 23 Rotated ...

Read More

Program to count average of all special values for all permutations of a list of items in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 194 Views

Finding the average of special values across all permutations of a list involves calculating a specific transformation for each arrangement and then averaging the results. This problem demonstrates an elegant mathematical property where the average can be computed directly without generating all permutations. Understanding the Algorithm The special value S is calculated using this process ? while size of L > 1 is non-zero, do a := L[0] b := L[1] remove L[1] L[0] := a + b + a*b ...

Read More

Program to construct the lexicographically largest valid sequence in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 366 Views

Suppose we have a number n, we have to find a sequence that satisfies all of the following rules ? 1 occurs once in the sequence. Each number in between 2 and n occurs twice in the sequence. For every i in range 2 to n, the distance between the two occurrences of i is exactly i. The distance between two numbers in the sequence, a[i] and a[j], is |j - i|. We have to find the lexicographically largest sequence. So, if the ...

Read More

Program to find out the length between two cities in shortcuts in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 546 Views

Finding the shortest path using shortcuts between cities is a graph theory problem. We have highways connecting some cities, and shortcuts exist between cities that are not directly connected by highways. Our goal is to find the minimum distances using only shortcuts from a starting city to all other cities. Problem Understanding Given a graph where edges represent highways, shortcuts exist between vertices that are not connected by highways. We need to find shortest paths using only these shortcuts ? 1 2 ...

Read More

Program to find out the minimum path to deliver all letters in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 230 Views

Suppose there are n cities connected with n-1 roads, forming a tree structure where any city can be reached from any other city. A postal worker needs to deliver k letters daily to different destination cities. We need to find the minimum distance the worker must travel to deliver all letters, starting from any city. Problem Understanding Given a tree of cities and a list of delivery destinations, we need to find the shortest path that visits all destination cities. The key insight is that in a tree, we need to traverse edges optimally — some edges might ...

Read More
Showing 3341–3350 of 8,546 articles
« Prev 1 333 334 335 336 337 855 Next »
Advertisements