From 74dc670d3182e1c2a2c4379b450ceec521fce302 Mon Sep 17 00:00:00 2001 From: Yuya Minamide Date: Sat, 24 Jun 2023 17:15:36 -0700 Subject: [PATCH] solved 868. Binary Gap --- 868.binary-gap.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 868.binary-gap.js diff --git a/868.binary-gap.js b/868.binary-gap.js new file mode 100644 index 0000000..46cb4be --- /dev/null +++ b/868.binary-gap.js @@ -0,0 +1,26 @@ +/** + * URL of this problem + * https://leetcode.com/problems/binary-gap/ + */ + +/** + * @param {number} n + * @return {number} + */ +var binaryGap = function (n) { + const BinaryNArr = n.toString(2).split(""); + let counter = 0; + + for (let i = 0; i < BinaryNArr.length - 1; i++) { + if (BinaryNArr[i] === "1") { + for (let j = i + 1; j < BinaryNArr.length; j++) { + if (BinaryNArr[j] === "1") { + counter = Math.max(counter, j - i); + break; + } + } + } + } + + return counter; +};