forked from krimanisha/Hacktoberfest21
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRange LCM Queries.java
More file actions
119 lines (96 loc) · 2.35 KB
/
Range LCM Queries.java
File metadata and controls
119 lines (96 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// LCM of given range queries
// using Segment Tree
class GFG
{
static final int MAX = 1000;
// allocate space for tree
static int tree[] = new int[4 * MAX];
// declaring the array globally
static int arr[] = new int[MAX];
// Function to return gcd of a and b
static int gcd(int a, int b) {
if (a == 0) {
return b;
}
return gcd(b % a, a);
}
// utility function to find lcm
static int lcm(int a, int b)
{
return a * b / gcd(a, b);
}
// Function to build the segment tree
// Node starts beginning index
// of current subtree. start and end
// are indexes in arr[] which is global
static void build(int node, int start, int end)
{
// If there is only one element
// in current subarray
if (start == end)
{
tree[node] = arr[start];
return;
}
int mid = (start + end) / 2;
// build left and right segments
build(2 * node, start, mid);
build(2 * node + 1, mid + 1, end);
// build the parent
int left_lcm = tree[2 * node];
int right_lcm = tree[2 * node + 1];
tree[node] = lcm(left_lcm, right_lcm);
}
// Function to make queries for
// array range )l, r). Node is index
// of root of current segment in segment
// tree (Note that indexes in segment
// tree begin with 1 for simplicity).
// start and end are indexes of subarray
// covered by root of current segment.
static int query(int node, int start,
int end, int l, int r)
{
// Completely outside the segment, returning
// 1 will not affect the lcm;
if (end < l || start > r)
{
return 1;
}
// completely inside the segment
if (l <= start && r >= end)
{
return tree[node];
}
// partially inside
int mid = (start + end) / 2;
int left_lcm = query(2 * node, start, mid, l, r);
int right_lcm = query(2 * node + 1, mid + 1, end, l, r);
return lcm(left_lcm, right_lcm);
}
// Driver code
public static void main(String[] args)
{
//initialize the 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 the segment tree
build(1, 0, 10);
// Now we can answer each query efficiently
// Print LCM of (2, 5)
System.out.println(query(1, 0, 10, 2, 5));
// Print LCM of (5, 10)
System.out.println(query(1, 0, 10, 5, 10));
// Print LCM of (0, 10)
System.out.println(query(1, 0, 10, 0, 10));
}
}