-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy path1362-closest-divisors.js
39 lines (34 loc) · 1005 Bytes
/
1362-closest-divisors.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
* 1362. Closest Divisors
* https://leetcode.com/problems/closest-divisors/
* Difficulty: Medium
*
* Given an integer num, find the closest two integers in absolute difference whose product
* equals num + 1 or num + 2.
*
* Return the two integers in any order.
*/
/**
* @param {number} num
* @return {number[]}
*/
function closestDivisors(num) {
const pair1 = findClosestPair(num + 1);
const pair2 = findClosestPair(num + 2);
return Math.abs(pair1[1] - pair1[0]) <= Math.abs(pair2[1] - pair2[0]) ? pair1 : pair2;
function findClosestPair(target) {
let minDifference = Infinity;
let pair = [];
for (let divisor = 1; divisor <= Math.sqrt(target); divisor++) {
if (target % divisor === 0) {
const complement = target / divisor;
const difference = Math.abs(complement - divisor);
if (difference < minDifference) {
minDifference = difference;
pair = [divisor, complement];
}
}
}
return pair;
}
}