Help me in solving APPLEORANGE problem

My issue

How can I remove TLE?

My code

#include <iostream>
#include <algorithm>
#define ll long long
using namespace std;

int main() {
	// your code goes here
	ll int t;
	std::cin >> t;
	   ll int n,m,mx1=0;
	    ll int mx=max(n,m);
	    for(ll int i=1;i<mx;i++)
	        if(n%i == 0 && m%i == 0)
	            mx1=max(mx1, i);
	    std::cout << mx1 << std::endl;
	return 0;

Problem Link: APPLEORANGE Problem - CodeChef

the logic is to find gcd of n,m so u can find gcd in log(n) time complexity too

1 Like