LeetCode 2261: K Divisible Elements Subarrays Solution

Master LeetCode problem 2261 (K Divisible Elements Subarrays), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

2261. K Divisible Elements Subarrays

Problem Explanation

Explanation:

To solve this problem, we can use a sliding window approach. We iterate through the array while maintaining a window that contains at most k elements divisible by p. We keep track of the count of subarrays that satisfy the conditions.

Algorithm:

  1. Initialize left and right pointers to 0.
  2. Initialize variables count and result to 0.
  3. Create a hashmap freq to store the frequency of remainders of elements when divided by p.
  4. Iterate through the array using the right pointer:
    • Update the frequency of the current element's remainder in the freq hashmap.
    • If the size of freq exceeds k, move the left pointer to the right until the size is less than or equal to k. Update the count accordingly while moving the left pointer.
    • Update the result by adding the difference of the current count and the count when the left pointer was moved.
  5. Return the result.

Time Complexity: O(n) where n is the length of the input array. Space Complexity: O(k) where k is a constant (<= 200).

:

Solution Code

class Solution {
    public int countSubarrays(int[] nums, int k, int p) {
        int left = 0, right = 0, count = 0, result = 0;
        Map<Integer, Integer> freq = new HashMap<>();
        
        while (right < nums.length) {
            int rem = nums[right] % p;
            freq.put(rem, freq.getOrDefault(rem, 0) + 1);
            
            while (freq.size() > k) {
                int leftRem = nums[left] % p;
                freq.put(leftRem, freq.get(leftRem) - 1);
                if (freq.get(leftRem) == 0) {
                    freq.remove(leftRem);
                }
                left++;
                count--;
            }
            
            count++;
            result += count;
            right++;
        }
        
        return result;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2261 (K Divisible Elements Subarrays)?

This page provides optimized solutions for LeetCode problem 2261 (K Divisible Elements Subarrays) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 2261 (K Divisible Elements Subarrays)?

The time complexity for LeetCode 2261 (K Divisible Elements Subarrays) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2261 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2261 in Java, C++, or Python.

Back to LeetCode Solutions