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 on Trending Technologies
Technical articles with clear explanations and examples
Find four factors of N with maximum product and sum equal to N - Set-2 in Python Program
Given a number N, we need to find four factors of N such that their sum equals N and their product is maximized. The factors can be repeated to achieve the maximum product. So, if the input is N = 60, then the output will be: All factors are [1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30, 60] and the maximum product is 50625, where 15 is selected four times (15 + 15 + 15 + 15 = 60 and 15 × 15 × 15 × 15 = 50625). Algorithm Steps To solve this ...
Read MoreFind pairs with given product in a sorted Doubly Linked List in Python
Finding pairs with a given product in a sorted doubly linked list is an efficient algorithm that uses the two-pointer technique. Given a sorted doubly linked list of unique positive numbers, we need to find all pairs whose product equals a target value x, without using extra space. For example, if we have a doubly linked list: 1 ⇄ 2 ⇄ 4 ⇄ 5 ⇄ 6 ⇄ 8 ⇄ 9 and target x = 8, the pairs would be (1, 8) and (2, 4). Algorithm Steps The solution uses two pointers starting from opposite ends of the ...
Read MoreFind original numbers from gcd() every pair in Python
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 MoreFind nth term of a given recurrence relation in Python
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 MoreFind n-th lexicographically permutation of a strings in Python
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 MoreFind N distinct numbers whose bitwise Or is equal to K in Python
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 MoreFind missing element in a sorted array of consecutive numbers in Python
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 MoreFind minimum time to finish all jobs with given constraints in Python
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 MoreFind minimum adjustment cost of an array in Python
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 MoreFind median of BST in O(n) time and O(1) space in Python
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