TLE in "Mod in an Array" (MOD)

I am trying to solve this question

and when i submit it I got partially correct (20 pts) . Here’s the code

#include <bits/stdc++.h>
using namespace std;

int main() {
unsigned long long int n,i,j,num; vector maxi,arr;
cin>>n;
// arr[n];
for(i=0;i<n;i++)
{
int NUM;
cin>>NUM;
arr.push_back(NUM);
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
num=arr[i]%arr[j];
maxi.push_back(num);
}
}
cout<<*max_element(maxi.begin(),maxi.end());
return 0;
}

What’s wrong in my code ? Thanks in advance

Please either format your code or link to your submission - the forum software has mangled it and it won’t compile! :slight_smile:

There is nothing wrong with your code ( your code is giving TLE not WA )
You are checking for all i and j due to which you are getting only 20 points
Try to optimize your code that is you should not check for all i and j but should use maths or something :slight_smile:

Thanks for giving the link on how to format the code. I was just looking for this :smile::smile:

1 Like