771. Number Of Unique Categories

An object category and an integer n are provided to you. Class Category Handler The handler.

The elements are numbered 0 through n-1. You have to figure out how many distinct categories there are for each piece.

The following function from the class CategoryHandler could be useful to you:

  • boolean haveSameCategory(integer a, integer b): Returns true if a and b are in the same category and false otherwise. Also, if either a or b is not a valid number (i.e. it's greater than or equal to nor less than 0), it returns false.

Give back how many distinct categories there are.

Example 1:

Input : n = 5, categoryHandler = [1, 2, 2, 3, 4]

Output : 4

Explanation :

There are 5 elements in this example. The first element belong to category 1, the second two belong to category 2, and the last two elements belong to category 3 and category 4 respeectively. So there are 4 unique categories.

Example 2:

Input : n = 5, categoryHandler = [1, 1, 1, 1, 1]

Output : 1

Explanation :

There are 5 elements in this example. All the elements belong to category 1.

Now Your Turn!

Pick the correct output for the given input

Input : n = 8, categoryHandler = [1, 1, 2, 2, 3, 3, 1, 1]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= n <= 100

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
/**
* Definition for a category handler.
* class CategoryHandler {
* public:
* CategoryHandler(vector<int> categories);
* bool haveSameCategory(int a, int b);
* };
*/
 
class Solution {
public:
int numberOfCategories(int n, CategoryHandler* categoryHandler) {
//your code goes here
}
};
Test Case

Input:

Category Handler