Number of Subarrays With GCD Equal to K - Brute Force - Clean Solution [JS]

Description 

Solution: Brute Force

To get the GCD of an array of elements, keep the running GCD starting from arr[0].
For each arr[i]gcd = getGCD(gcd, arr[i]).

Take each nums[i] as the start of a subarray.
Keep the running GCD from index i.
Go through each index j as the end of the subarray.

Time Complexity: O(n^2 * log(n)) 123ms
Space Complexity: O(1) 41.6MB

var subarrayGCD = function(nums, k) {
  let n = nums.length, ans = 0;
  for (let i = 0; i < n; i++) {
    let gcd = nums[i];
    for (let j = i; j < n; j++) {
      gcd = getGCD(gcd, nums[j]);
      if (gcd === k) ans++;
    }
  }
  return ans;  
};

function getGCD(a, b) {
  if (b === 0) return a;
  return getGCD(b, a % b);
}

Comments

Popular posts from this blog

Maximum Value of an Ordered Triplet II - Two Solutions [JS]

Maximum Sum of Distinct Subarrays With Length K - Sliding Window w/ Two Pointers & Set [JS]

Sum of Prefix Scores of Strings - Trie [JS]

Maximum Count of Positive Integer and Negative Integer - Binary Search [JS]

Count Subarrays With Median K - Count Left & Right Balance [JS]