Number of Subarrays With LCM Equal to K - Complete Solution Guide
Number of Subarrays With LCM Equal to K is LeetCode problem 2470, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
Given an integer array nums and an integer k , return the number of subarrays of nums where the least common multiple of the subarray's elements is k . A subarray is a contiguous non-empty sequence of elements within an array. The least common multiple of an array is the smallest positive integer that is divisible by all the array elements. Example 1: Input: nums = [3,6,2,7,1], k = 6 Output: 4 Explanation: The subarrays of nums where 6 is the least common multiple of all the subarray's elements
Detailed Explanation
The problem asks us to find the number of subarrays within a given array `nums` whose least common multiple (LCM) is equal to a given integer `k`. A subarray is a contiguous sequence of elements within the array. The LCM of an array is the smallest positive integer that is divisible by all elements of the array. The input consists of an integer array `nums` and an integer `k`, and the output is the number of subarrays of `nums` that satisfy the LCM condition. The constraints are that the length of `nums` is between 1 and 1000, and each element in `nums` and `k` are between 1 and 1000.
Solution Approach
The provided solution iterates through all possible subarrays using nested loops. For each subarray, it checks if all elements of the subarray divide `k`. If any element doesn't divide `k`, the inner loop breaks because the LCM cannot be `k`. If all elements divide `k`, it calculates the LCM of the elements in the subarray using the formula `lcm(a, b) = (a * b) / gcd(a, b)` iteratively. If the LCM equals `k`, the count is incremented. Finally, the total count of subarrays with LCM equal to `k` is returned.
Step-by-Step Algorithm
- Step 1: Initialize a counter `count` to 0. This variable will store the number of subarrays with LCM equal to `k`.
- Step 2: Iterate through the input array `nums` using a nested loop structure. The outer loop iterates from `i = 0` to `n - 1`, where `n` is the length of `nums`. This loop represents the starting index of the subarray.
- Step 3: The inner loop iterates from `j = i` to `n - 1`. This loop represents the ending index of the subarray.
- Step 4: Inside the inner loop, initialize `current_lcm` to 1. This variable will store the LCM of the current subarray.
- Step 5: Check if `k` is divisible by `nums[j]`. If it's not, break the inner loop because the LCM of the subarray cannot be `k` if any element in the subarray doesn't divide `k`.
- Step 6: If `k` is divisible by `nums[j]`, update `current_lcm` using the formula `current_lcm = (current_lcm * nums[j]) / gcd(current_lcm, nums[j])`.
- Step 7: After updating `current_lcm`, check if it is equal to `k`. If it is, increment `count`.
- Step 8: After the inner loop completes, the outer loop continues to the next starting index.
- Step 9: After the outer loop completes, return `count`.
Key Insights
- Insight 1: We can iterate through all possible subarrays of `nums` and calculate the LCM of each subarray to check if it equals `k`.
- Insight 2: To efficiently calculate the LCM of a subarray, we can use the formula `lcm(a, b) = (a * b) / gcd(a, b)`, where `gcd(a, b)` is the greatest common divisor of `a` and `b`.
- Insight 3: If any element in the subarray does not divide `k` evenly, then it is impossible for the LCM of that subarray to be equal to `k`. This optimization avoids unnecessary LCM calculations.
Complexity Analysis
Time Complexity: O(n^2 * log(max(nums)))
Space Complexity: O(1)
Topics
This problem involves: Array, Math, Number Theory.
Companies
Asked at: Unity.