**K-Distinct Sublists - Google Top Interview Questions**

### Problem Statement :

Given a list of integers nums and an integer k, return the number of sublists such that there's exactly k distinct numbers in the sublist. Constraints k ≤ n ≤ 100,000 where n is the length of nums Example 1 Input nums = [1, 1, 2, 3] k = 2 Output 3 Explanation We have the following sublists which have exactly 2 distinct numbers: [1, 1, 2], [1, 2], [2, 3]

### Solution :

` ````
Solution in C++ :
int solve(vector<int>& nums, int k) {
unordered_map<int, int> fr;
nums.push_back(0);
int i = 0, j = 0, n = nums.size(), res = 0, cnt = 1;
while (j < n && k > 0) {
fr[nums[j]]++;
if (fr[nums[j]] == 1) k--;
j++;
}
while (j < n) {
if (k == 0) {
while (fr[nums[i]] > 1) {
fr[nums[i]]--;
i++;
cnt++;
}
res += cnt;
fr[nums[j]]++;
if (fr[nums[j]] == 1) k--;
j++;
} else {
cnt = 1;
fr[nums[i]]--;
if (fr[nums[i]] == 0) k++;
i++;
}
}
return res;
}
```

` ````
Solution in Java :
import java.util.*;
class Solution {
public int solve(int[] nums, int k) {
final int N = nums.length;
Map<Integer, Integer> counts = new HashMap<>(N);
int res = 0;
for (int a = 0, b = -1, c = -1, kinds = 0; a != N; a++) {
while (b + 1 != N && kinds < k) {
b++;
int prev = counts.getOrDefault(nums[b], 0);
if (0 == prev)
kinds++;
counts.put(nums[b], prev + 1);
}
c = Math.max(c, b);
while (c + 1 != N && counts.containsKey(nums[c + 1])) c++;
if (kinds == k)
res += (c + 1 - b);
if (kinds < k)
break;
{
int prev = counts.get(nums[a]) - 1;
if (prev == 0) {
counts.remove(nums[a]);
kinds--;
} else
counts.put(nums[a], prev);
}
}
return res;
}
}
```

` ````
Solution in Python :
class Solution:
def windows(self, nums, k):
i = j = dis = ans = 0
d = defaultdict(int)
while j < len(nums):
if d[nums[j]] == 0:
dis += 1
d[nums[j]] += 1
while i <= j and dis > k:
if d[nums[i]] == 1:
dis -= 1
d[nums[i]] -= 1
i += 1
if dis <= k:
ans += j - i + 1
j += 1
return ans
def solve(self, nums, k):
return self.windows(nums, k) - self.windows(nums, k - 1)
```

## View More Similar Problems

## Cycle Detection

A linked list is said to contain a cycle if any node is visited more than once while traversing the list. Given a pointer to the head of a linked list, determine if it contains a cycle. If it does, return 1. Otherwise, return 0. Example head refers 1 -> 2 -> 3 -> NUL The numbers shown are the node numbers, not their data values. There is no cycle in this list so return 0. head refer

View Solution →## Find Merge Point of Two Lists

This challenge is part of a tutorial track by MyCodeSchool Given pointers to the head nodes of 2 linked lists that merge together at some point, find the node where the two lists merge. The merge point is where both lists point to the same node, i.e. they reference the same memory location. It is guaranteed that the two head nodes will be different, and neither will be NULL. If the lists share

View Solution →## Inserting a Node Into a Sorted Doubly Linked List

Given a reference to the head of a doubly-linked list and an integer ,data , create a new DoublyLinkedListNode object having data value data and insert it at the proper location to maintain the sort. Example head refers to the list 1 <-> 2 <-> 4 - > NULL. data = 3 Return a reference to the new list: 1 <-> 2 <-> 4 - > NULL , Function Description Complete the sortedInsert function

View Solution →## Reverse a doubly linked list

This challenge is part of a tutorial track by MyCodeSchool Given the pointer to the head node of a doubly linked list, reverse the order of the nodes in place. That is, change the next and prev pointers of the nodes so that the direction of the list is reversed. Return a reference to the head node of the reversed list. Note: The head node might be NULL to indicate that the list is empty.

View Solution →## Tree: Preorder Traversal

Complete the preorder function in the editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's preorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the preOrder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the tree's

View Solution →## Tree: Postorder Traversal

Complete the postorder function in the editor below. It received 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's postorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the postorder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the

View Solution →