← Back to topics
Topic

maxGCD

b
bour1992
In the maxGCD problem i take timeout for the last 3 test cases.
Can you see my code here: http://www.z-trening.com/new/www/html/submit.php?submit=7100024153&subm_code=1 and tell me if i can improve that?
A
Amtrix
Here is something i notced:

for(i=0;i<n;i++)
for(j=0;j<n;j++)
// with this you check the same combination two times...


for(i=0;i<n;i++)
for(j=i-1;j>=0;j--)
// on this way you become much more time ...
// and you dont need to check that:
(i!=j)
A
Al3kSaNdaR
First, use scanf/printf instead of cin/cout.

Second, try replacing this.


for (int i=0;i<n;i++)
{
for (int j=0;j<n;j++)
{


with this :


for (int i=0;i<n - 1;i++)
{
for (int j=i + 1;j<n;j++)
{
.
That should fix the TLE.
b
bour1992
Thank you guys.
It fixed the TLE.

One more question.
Why should i use printf and scanf instead of cout and cin?
g
goran_f2
printf and scanf are faster