Geeksforgeeks

Error

×

Leaderboard

Showing:

Handle | Score |
---|---|

@Ibrahim Nash | 6420 |

@blackshadows | 6376 |

@mb1973 | 5578 |

@Quandray | 5231 |

@akhayrutdinov | 5111 |

@saiujwal13083 | 4510 |

@sanjay05 | 3762 |

@kirtidee18 | 3673 |

@marius_valentin_dragoi | 3522 |

@sushant_a | 3459 |

@verma_ji | 3412 |

Complete Leaderboard | |

Handle | Score |

@codersgroup18 | 714 |

@Manikanta punnam | 579 |

@sriramgoparaju99 | 532 |

@yashkaril4 | 517 |

@praveenbgp6 | 517 |

@prankursharma31 | 489 |

@rdakka | 465 |

@sonamnigam1999 | 441 |

@purohitmn02 | 427 |

@sonamkumari63928 | 426 |

@ShubhankarPandey | 419 |

Complete Leaderboard |

Given an array of integers and a number K. Find the count of distinct elements in every window of size K in the array.

**Example 1:**

Input:N = 7, K = 4 A[] = {1,2,1,3,4,2,3}Output:3 4 4 3Explanation:Window 1 of size k = 4 is 1 2 1 3. Number of distinct elements in this window are 3. Window 2 of size k = 4 is 2 1 3 4. Number of distinct elements in this window are 4. Window 3 of size k = 4 is 1 3 4 2. Number of distinct elements in this window are 4. Window 4 of size k = 4 is 3 4 2 3. Number of distinct elements in this window are 3.

**Example 2:**

Input:N = 3, K = 2 A[] = {4,1,1}Output:2 1

**Your Task:**

Your task is to complete the function **countDistinct()** which takes the array A[], the size of the array(N) and the window size(K) as inputs and returns an array containing the count of distinct elements in every contiguous window of size K in the array A[].

**Expected Time Complexity:** O(N).

**Expected Auxiliary Space:** O(N).

**Constraints:**

1 <= N <= K <= 10^{5}

1 <= A[i] <= 10^{5 }, for each valid i

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes
Count distinct elements in every window

...