Menu

(Solved) : Consider Following Problem Given Array Duplicate Elements Want Find Number Distinct Elemen Q32730658 . . .

Consider the following problem, where we are given an array Awith some duplicate elements, and want to find the number ofdistinct elements in each interval of size k.

• Input: a positive integer k, and an unsorted A with n numbers,some of them repeating.

• Output: an array B of length n − k, where B[i] should containthe number of distinct elements in the interval A[i], A[i + 1],…, A[i + k].

Write pseudocode for an algorithm for this problem with expectedrunning time O(n). Note that your expected running time should beO(n) even if k is large.

HINT: similar to the algorithm for sorting a k-sorted array, youwill want to take advantage of the fact that two consecutiveintervals A[i]…A[i + k] and A[i + 1]…A[i + k + 1] only differby a small number of elements.

Expert Answer


Answer to Consider Following Problem Given Array Duplicate Elements Want Find Number Distinct Elemen Q32730658 . . .

OR