1. Brute force approach:
In this process, we use a for loop and iterate the whole 1 to n number. Then, we find out all the divisors by one by one. It is not a optimal solution for this problem. Because, it will take O(n) time to find out all divisors.
int main()
{
optimize();
ll n;
cin>>n;
for(ll i=1; i<=n; i++)
{
if(n%2==0) cout<<i<<" ";
}
cout<<endl;
return 0;
}
Time complexity: O(n)
2. SQRT approach:
In this process, we only iterate in the loop by half of the numbers. Because, if we see we don't need to go through whole n numbers.
int main()
{
optimize();
ll n;
cin>>n;
for(ll i=1; i * i<=n; i++)
{
if(n%2==0) cout<<i<<" "<<n/i<<endl;
}
return 0;
}
Time Complexity: O(sqrt(n))
Comments
Post a Comment