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 3 Program For Range LCM Queries
Range LCM queries involve finding the Least Common Multiple (LCM) of all elements in a given range of an array. This is a common problem in competitive programming that requires efficient data structures like segment trees to handle multiple queries quickly.
The LCM (Least Common Multiple) of numbers in a range [a, r] is calculated as:
LCM(a, r) = LCM(arr[a], arr[a+1], ..., arr[r])
We use the mathematical relationship: LCM(a, b) = (a * b) / GCD(a, b) where GCD is the Greatest Common Divisor.
Using Segment Tree Approach
A segment tree efficiently handles range queries by preprocessing the array and storing LCM values for different segments ?
import math
MAX = 1000
tree = [0] * (4 * MAX)
arr = [0] * MAX
def gcd(a, b):
if a == 0:
return b
return gcd(b % a, a)
def lcm(a, b):
return (a * b) // gcd(a, b)
def build(node, start, end):
if start == end:
tree[node] = arr[start]
return
mid = (start + end) // 2
build(2 * node, start, mid)
build(2 * node + 1, mid + 1, end)
left_lcm = tree[2 * node]
right_lcm = tree[2 * node + 1]
tree[node] = lcm(left_lcm, right_lcm)
def query(node, start, end, l, r):
if end < l or start > r:
return 1
if l <= start and r >= end:
return tree[node]
mid = (start + end) // 2
left_lcm = query(2 * node, start, mid, l, r)
right_lcm = query(2 * node + 1, mid + 1, end, l, r)
return lcm(left_lcm, right_lcm)
# Initialize array
arr[0] = 5
arr[1] = 7
arr[2] = 5
arr[3] = 2
arr[4] = 10
arr[5] = 12
arr[6] = 11
arr[7] = 17
arr[8] = 14
arr[9] = 1
arr[10] = 44
# Build segment tree
build(1, 0, 10)
# Process queries
print("LCM of range [2, 5]:", query(1, 0, 10, 2, 5))
print("LCM of range [5, 10]:", query(1, 0, 10, 5, 10))
print("LCM of range [0, 10]:", query(1, 0, 10, 0, 10))
LCM of range [2, 5]: 60 LCM of range [5, 10]: 15708 LCM of range [0, 10]: 78540
Simple LCM Calculation
For basic LCM calculation between two numbers, we can use a brute force approach ?
def compute_lcm(x, y):
greater = max(x, y)
while True:
if (greater % x == 0) and (greater % y == 0):
return greater
greater += 1
num1 = 16
num2 = 7
result = compute_lcm(num1, num2)
print(f"The LCM of {num1} and {num2} is: {result}")
The LCM of 16 and 7 is: 112
Optimized LCM Using GCD
A more efficient approach uses the mathematical relationship between LCM and GCD ?
def gcd(a, b):
while b:
a, b = b, a % b
return a
def lcm(a, b):
return abs(a * b) // gcd(a, b)
# Example usage
numbers = [12, 18, 24]
result = numbers[0]
for i in range(1, len(numbers)):
result = lcm(result, numbers[i])
print(f"LCM of {numbers} is: {result}")
LCM of [12, 18, 24] is: 72
Time Complexity Analysis
| Approach | Build Time | Query Time | Space |
|---|---|---|---|
| Segment Tree | O(N log N) | O(log N) | O(N) |
| Naive | O(1) | O(N) | O(1) |
Conclusion
Range LCM queries are efficiently solved using segment trees with O(log N) query time. The segment tree approach is ideal for multiple queries, while simple iteration works for single queries.
