How to make this more time efficient?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to make this more time efficient??

#include <iostream> #include<math.h> using namespace std; int lcm(int a,int b) { for(int i=max(a,b) ; ;i++) { if(i%a==0 && i%b==0) return i; } } int main() { // your code goes here int n; cin>>n; int a[n]; for(int i=0 ;i<n ;i++) cin>>a[i]; int maxa=0; for(int i= 0 ; i<n ;i++) { for(int j=i+1 ;j<n ;j++) { maxa=max(maxa,lcm(a[i],a[j])); } } cout<<maxa; return 0; }

28th Apr 2020, 5:29 PM
Shubhank Kulshreshtha
Shubhank Kulshreshtha - avatar
3 Answers
0
I hv used the nested loop to make all posssible pairs from the array
29th Apr 2020, 11:46 AM
Shubhank Kulshreshtha
Shubhank Kulshreshtha - avatar
0
Nooo , the task is to make pairs from the array and find the maximum lcm from it
29th Apr 2020, 11:55 AM
Shubhank Kulshreshtha
Shubhank Kulshreshtha - avatar
0
Sorry i could not upload the question . But thanks for the response☺
29th Apr 2020, 11:57 AM
Shubhank Kulshreshtha
Shubhank Kulshreshtha - avatar