Python Articles

Page 650 of 855

Find original numbers from gcd() every pair in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 325 Views

Given an array containing GCD (Greatest Common Divisor) values of every possible pair from an original array, we need to reconstruct the original numbers. This is a reverse engineering problem where we work backwards from the GCD results. Problem Understanding If we have an original array like [13, 6], the GCD array would contain ? gcd(13, 13) = 13 gcd(13, 6) = 1 gcd(6, 13) = 1 gcd(6, 6) = 6 So the GCD array becomes [13, 1, 1, 6]. Our task is to reverse this process. Algorithm Steps The key insight ...

Read More

Find nth term of a given recurrence relation in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 749 Views

A recurrence relation defines a sequence where each term is expressed using previous terms. Given the relation b1=1 and bn+1/bn=2n, we need to find log2(bn) for any given n. Understanding the Mathematical Solution To solve this recurrence relation, we can derive the general formula step by step ? Starting with the given relation: bn+1/bn = 2n bn/bn-1 = 2n-1 ... b2/b1 = 21 Multiplying all these equations together: (bn+1/bn) × (bn/bn-1) × ... × (b2/b1) = 2n + (n-1) + ... + 1 This simplifies to: bn+1/b1 = 2n(n+1)/2 Since b1 = 1, we ...

Read More

Find n-th lexicographically permutation of a strings in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 354 Views

Finding the n-th lexicographically ordered permutation of a string is a classic combinatorial problem. Given a string with lowercase letters, we need to find the specific permutation that would appear at position n when all permutations are sorted alphabetically. For example, if we have string = "pqr" and n = 3, all permutations in lexicographic order are: [pqr, prq, qpr, qrp, rpq, rqp]. The 3rd permutation is "qpr". Algorithm Overview The algorithm uses factorial number system to efficiently find the n-th permutation without generating all permutations ? Precompute factorials for quick ...

Read More

Find N distinct numbers whose bitwise Or is equal to K in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 225 Views

Given two integers N and K, we need to find N distinct numbers whose bitwise OR equals K. If no such combination exists, we return -1. For example, if N = 4 and K = 6, one possible output is [6, 0, 1, 2] because 6 | 0 | 1 | 2 = 6. Algorithm Approach The key insight is that we need at least 2^(number of set bits in K) different numbers to construct all possible combinations. We start with K itself, then generate additional numbers by systematically setting different bit patterns. Implementation ...

Read More

Find missing element in a sorted array of consecutive numbers in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 642 Views

When working with a sorted array of consecutive numbers that has one missing element, we can use binary search to efficiently find the missing number in O(log n) time complexity. So, if the input is like A = [1, 2, 3, 4, 5, 6, 7, 9], then the output will be 8. Algorithm The approach uses binary search with the following logic ? For each element at index i, if no element is missing before it, then A[i] - i should equal A[0] If A[mid] - mid equals A[0], the missing element is in the ...

Read More

Find minimum time to finish all jobs with given constraints in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 553 Views

We need to find the minimum time to finish all jobs when we have k assignees and each assignee takes t time units per job unit. The key constraints are that each assignee can only work on contiguous jobs, and no job can be split between assignees. The problem can be solved using binary search on the answer. We search for the minimum possible time limit such that all jobs can be completed by k assignees within that limit. Algorithm Overview The solution uses binary search between the minimum possible time (maximum single job) and maximum possible ...

Read More

Find minimum adjustment cost of an array in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 632 Views

The minimum adjustment cost problem involves modifying array elements to ensure adjacent differences don't exceed a target value while minimizing the total cost of changes. We use dynamic programming to find the optimal solution. Problem Statement Given an array of positive numbers, we need to replace elements so that the difference between any two adjacent elements is at most equal to a given target. The goal is to minimize the adjustment cost, which is the sum of absolute differences between original and new values: ∑|A[i] - Anew[i]|. Example If input array is [56, 78, 53, 62, ...

Read More

Find median of BST in O(n) time and O(1) space in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 619 Views

Finding the median of a Binary Search Tree (BST) efficiently requires understanding that an inorder traversal of a BST gives nodes in sorted order. The median is the middle element for odd number of nodes, or the average of two middle elements for even number of nodes. For a BST with n nodes: Odd nodes: median = (n+1)/2th node Even nodes: median = average of (n/2)th and (n/2+1)th nodes We'll use Morris Traversal to achieve O(n) time complexity with O(1) space complexity by avoiding recursion stack. Example Tree ...

Read More

Find maximum sum of triplets in an array such than i < j < k and a[i] < a[j] < a[k] in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 635 Views

Given an array of positive numbers, we need to find the maximum sum of a triplet (a[i] + a[j] + a[k]) such that 0 ≤ i < j < k < n and a[i] < a[j] < a[k]. This means we're looking for three increasing elements at increasing indices. For example, if the input is A = [3, 6, 4, 2, 5, 10], the valid triplets are (3, 4, 5): sum = 12, (3, 6, 10): sum = 19, (3, 4, 10): sum = 17, (4, 5, 10): sum = 19, and (2, 5, 10): sum = 17. The ...

Read More

Find maximum points which can be obtained by deleting elements from array in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 335 Views

When we delete an element from an array, we might need to remove related elements as well. This problem asks us to find the maximum points we can obtain by strategically deleting elements, where deleting an element ax gives us ax points but also forces us to remove elements in the range [ax-L, ax+R]. So, if the input is like A = [2, 4, 3, 10, 5], l = 1, r = 2, then the output will be 18. Algorithm Steps To solve this, we will follow these steps − Find the ...

Read More
Showing 6491–6500 of 8,549 articles
« Prev 1 648 649 650 651 652 855 Next »
Advertisements