java.lang.Object
g2201_2300.s2261_k_divisible_elements_subarrays.Solution

public class Solution extends Object
2261 - K Divisible Elements Subarrays.<p>Medium</p> <p>Given an integer array <code>nums</code> and two integers <code>k</code> and <code>p</code>, return <em>the number of <strong>distinct subarrays</strong> which have <strong>at most</strong></em> <code>k</code> <em>elements divisible by</em> <code>p</code>.</p> <p>Two arrays <code>nums1</code> and <code>nums2</code> are said to be <strong>distinct</strong> if:</p> <ul> <li>They are of <strong>different</strong> lengths, or</li> <li>There exists <strong>at least</strong> one index <code>i</code> where <code>nums1[i] != nums2[i]</code>.</li> </ul> <p>A <strong>subarray</strong> is defined as a <strong>non-empty</strong> contiguous sequence of elements in an array.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> nums = [<strong>2</strong> ,3,3, <strong>2</strong> , <strong>2</strong> ], k = 2, p = 2</p> <p><strong>Output:</strong> 11</p> <p><strong>Explanation:</strong></p> <p>The elements at indices 0, 3, and 4 are divisible by p = 2.</p> <p>The 11 distinct subarrays which have at most k = 2 elements divisible by 2 are:</p> <p>[2], [2,3], [2,3,3], [2,3,3,2], [3], [3,3], [3,3,2], [3,3,2,2], [3,2], [3,2,2], and [2,2].</p> <p>Note that the subarrays [2] and [3] occur more than once in nums, but they should each be counted only once.</p> <p>The subarray [2,3,3,2,2] should not be counted because it has 3 elements that are divisible by 2.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> nums = [1,2,3,4], k = 4, p = 1</p> <p><strong>Output:</strong> 10</p> <p><strong>Explanation:</strong></p> <p>All element of nums are divisible by p = 1.</p> <p>Also, every subarray of nums will have at most 4 elements that are divisible by 1.</p> <p>Since all subarrays are distinct, the total number of subarrays satisfying all the constraints is 10.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= nums.length <= 200</code></li> <li><code>1 <= nums[i], p <= 200</code></li> <li><code>1 <= k <= nums.length</code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • countDistinct

      public int countDistinct(int[] nums, int k, int p)