<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>O(sqrt(n)) Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/osqrtn/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/osqrtn/</link>
	<description></description>
	<lastBuildDate>Sun, 23 Feb 2020 20:02:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>O(sqrt(n)) Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/osqrtn/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1362. Closest Divisors</title>
		<link>https://zxi.mytechroad.com/blog/math/leetcode-1362-closest-divisors/</link>
					<comments>https://zxi.mytechroad.com/blog/math/leetcode-1362-closest-divisors/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 23 Feb 2020 20:00:46 +0000</pubDate>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[O(sqrt(n))]]></category>
		<category><![CDATA[sqrt]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6373</guid>

					<description><![CDATA[<p>Given an integer&#160;num, find the closest two integers in absolute difference whose product equals&#160;num + 1&#160;or&#160;num + 2. Return the two integers in any order.&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-1362-closest-divisors/">花花酱 LeetCode 1362. Closest Divisors</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Given an integer&nbsp;<code>num</code>, find the closest two integers in absolute difference whose product equals&nbsp;<code>num + 1</code>&nbsp;or&nbsp;<code>num + 2</code>.</p>



<p>Return the two integers in any order.</p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> num = 8
<strong>Output:</strong> [3,3]
<strong>Explanation:</strong> For num + 1 = 9, the closest divisors are 3 &amp; 3, for num + 2 = 10, the closest divisors are 2 &amp; 5, hence 3 &amp; 3 is chosen.
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> num = 123
<strong>Output:</strong> [5,25]
</pre>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> num = 999
<strong>Output:</strong> [40,25]
</pre>



<p><strong>Constraints:</strong></p>



<ul><li><code>1 &lt;= num &lt;= 10^9</code></li></ul>



<h2><strong>Solution: Brute Force</strong></h2>



<p>Time complexity: O(sqrt(n))<br>Space complexity: O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  vector&lt;int&gt; closestDivisors(int num) {
    auto divisors = [](int n) {
      for (int i = sqrt(n); i &gt;= 2; --i)
        if (n % i == 0) return vector&lt;int&gt;{i, n / i};
      return vector&lt;int&gt;{1, n};
    };
    
    auto ans1 = divisors(num + 1);
    auto ans2 = divisors(num + 2);
    return ans1[1] - ans1[0] &lt; ans2[1] - ans2[0] ? ans1 : ans2;
  }
};</pre>
</div></div>



<div class="responsive-tabs">
<h2 class="tabtitle">Python3</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag"># Author: Huahua
class Solution:
  def closestDivisors(self, num: int) -&gt; List[int]:
    def divisors(n: int) -&gt; List[int]:
      for i in range(int(math.sqrt(n)), 1, -1):
        if n % i == 0: return [i, n // i]
      return [1, n]
    
    a1, a2 = divisors(num + 1), divisors(num + 2)
    return a1 if a1[1] - a1[0] &lt; a2[1] - a2[0] else a2</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-1362-closest-divisors/">花花酱 LeetCode 1362. Closest Divisors</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/math/leetcode-1362-closest-divisors/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
