From 1830849e40e2e99ff264f85c729cea81ea818557 Mon Sep 17 00:00:00 2001 From: Aditya12604 Date: Thu, 30 Oct 2025 22:27:07 +0530 Subject: [PATCH 1/2] Added Count Distinct Subarrays Divisible by K (LeetCode) solution with explanation and test cases --- ...ount_Distinct_Subarrays_Divisible_By_K.cpp | 114 ++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 CPP/algorithms/arrays/Count_Distinct_Subarrays_Divisible_By_K.cpp diff --git a/CPP/algorithms/arrays/Count_Distinct_Subarrays_Divisible_By_K.cpp b/CPP/algorithms/arrays/Count_Distinct_Subarrays_Divisible_By_K.cpp new file mode 100644 index 00000000..c18745f2 --- /dev/null +++ b/CPP/algorithms/arrays/Count_Distinct_Subarrays_Divisible_By_K.cpp @@ -0,0 +1,114 @@ +/* +Problem Name: Count Distinct Subarrays Divisible by K in Sorted Array +Problem Link: https://leetcode.com/problems/count-distinct-subarrays-divisible-by-k-in-sorted-array/ +Platform: LeetCode +Language: C++ +Author: Aditya Shimoga +Date: 30th October 2025 + +Description: +----------------------------------- +Given a sorted array nums and an integer k, find the number of distinct subarrays +whose sum is divisible by k. + +Key Insight: +----------------------------------- +- For any subarray [l, r], sum(l..r) is divisible by k ⇔ prefixSum[r] % k == prefixSum[l-1] % k. +- To count valid subarrays efficiently, we maintain a frequency map of prefix sums modulo k. +- Since the array is sorted and may contain repeated elements, we process contiguous "blocks" + of equal numbers separately. This ensures we don’t count identical subarrays multiple times. +- For each block: + 1. Calculate all valid subarrays ending in this block using prefix remainders from previous blocks. + 2. Update the remainder frequency map after processing the block. + +Complexity: +----------------------------------- +Time Complexity: O(N) +Space Complexity: O(K) , depending on distinct remainders +*/ + +#include +using namespace std; + +class Solution { +public: + long long numGoodSubarrays(vector& nums, int k) { + long long divisor = 1LL * k; + long long prefixSum = 0LL, resultCount = 0LL; + int n = nums.size(); + + map remainderFreq; // Tracks frequency of prefix sums modulo k + + int i = 0; + while (i < n) { + int j = i; + int currentValue = nums[i]; + long long tempPrefixSum = prefixSum; + + // Step 1: Count valid subarrays ending within the current block + while (j < n && nums[j] == currentValue) { + prefixSum += nums[j]; + int remainder = prefixSum % divisor; + + // Subarray from start (remainder == 0) + if (remainder == 0) resultCount++; + + // Subarray ending here matches an earlier prefix remainder + if (remainderFreq.count(remainder)) + resultCount += 1LL * remainderFreq[remainder]; + + j++; + } + + // Step 2: Update frequency map for this block + j = i; + while (j < n && nums[j] == currentValue) { + tempPrefixSum += nums[j]; + int remainder = tempPrefixSum % divisor; + remainderFreq[remainder]++; + j++; + } + + // Move to next distinct value block + i = j; + } + + return resultCount; + } +}; + +/* +Example: +Input: +nums = [1,1,2,2,3], k = 3 + +Output: +Number of distinct subarrays divisible by 3 = 4 + +Explanation: +Subarrays divisible 3: [3], [1,2], [1,1,2,2],[1,1,2,2,3] +*/ + +int main() { + Solution solver; + + // Test Case 1 + vector nums1 = {1, 1, 2, 2, 3}; + int k1 = 3; + long long result1 = solver.numGoodSubarrays(nums1, k1); + cout << "Test Case 1:" << endl; + cout << "Input: nums = [1,1,2,2,3], k = 3" << endl; + cout << "Output: " << result1 << endl; + cout << "Expected: 4" << endl << endl; + + // Test Case 2 + vector nums2 = {2, 2, 2, 4, 4}; + int k2 = 4; + long long result2 = solver.numGoodSubarrays(nums2, k2); + cout << "Test Case 2:" << endl; + cout << "Input: nums = [2,2,2,4,4], k = 4" << endl; + cout << "Output: " << result2 << endl; + cout << "Expected: 6" << endl; + + return 0; +} From be0ad70b63dd020583efb125462ca2747549c70d Mon Sep 17 00:00:00 2001 From: Aditya12604 Date: Fri, 31 Oct 2025 03:44:53 +0530 Subject: [PATCH 2/2] slight correction in Time Complexity --- .../arrays/Count_Distinct_Subarrays_Divisible_By_K.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CPP/algorithms/arrays/Count_Distinct_Subarrays_Divisible_By_K.cpp b/CPP/algorithms/arrays/Count_Distinct_Subarrays_Divisible_By_K.cpp index c18745f2..89eef7a0 100644 --- a/CPP/algorithms/arrays/Count_Distinct_Subarrays_Divisible_By_K.cpp +++ b/CPP/algorithms/arrays/Count_Distinct_Subarrays_Divisible_By_K.cpp @@ -23,7 +23,7 @@ Key Insight: Complexity: ----------------------------------- -Time Complexity: O(N) +Time Complexity: O(N*log(k)) Space Complexity: O(K) , depending on distinct remainders */