-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathcode_1.cpp
72 lines (65 loc) · 1.72 KB
/
code_1.cpp
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
//
// code_1.cpp
// Algorithm
//
// Created by Mohd Shoaib Rayeen on 23/11/18.
// Copyright © 2018 Shoaib Rayeen. All rights reserved.
//
#include <iostream>
#include <vector>
using namespace std;
struct Interval {
int start, end;
};
int getGCD(int num1 , int num2 ) {
if (num1 == 0) {
return num2;
}
if (num2 == 0) {
return num1;
}
if (num1 == num2) {
return num2;
}
if (num1 > num2) {
return getGCD(num1 - num2, num2);
}
return getGCD(num1, num2-num1);
}
void getLCMInRanges(vector<int> array , vector<Interval> list) {
int numberOfRange = int(list.size());
int lcm , temp , temp2;
for ( int i = 0; i < numberOfRange; i++) {
temp = getGCD( array[list[i].start] , array[list[i].start + 1]);
lcm = (array[list[i].start] * array[list[i].start + 1])/temp;
if ( list[i].end - list[i].start > 1) {
for ( int j = list[i].start + 2; j <= list[i].end; j++ ) {
temp = getGCD(lcm, array[j]);
temp2 = (lcm * array[j])/temp;
lcm = temp2;
}
}
cout << "\nFor Range ( " << list[i].start << " , " << list[i].end << ")\t|\tLCM\t:\t" << lcm << endl;
}
cout << endl;
}
int main() {
int n;
cout << "\nEnter Size of Array\t:\t";
cin >> n;
vector<int> arr(n);
cout << "\nEnter Array Elements\n";
for (int i = 0; i < n; i++ ) {
cin >> arr[i];
}
cout << "\nEnter Number of Intervals\t:\t";
cin >> n;
vector<Interval> array(n);
cout << "\nEnter Range (L,R) Form\n";
for (int i = 0; i < n; i++ ) {
cin >> array[i].start;
cin >> array[i].end;
}
getLCMInRanges(arr, array);
return 0;
}