distinct dilemma
This problem was fairly simple compared to the Rating that it had, I think this was part of the Starters 81 Contest, maybe, the overall logic and the core of the Problem was understanding what we are doing with the operations that are mentioned in the Problem Statement, the 2 operations if understood and imagined well, they just tell us that we can make any element equal to any other element and for this case as we require "Distinct" Elements so we should start in the manner of a Natural Numbers Series, like 1,2,3,4... so, overall what we are doing is that we are just starting to make these Natural Numbers, or we can say the Distinct Numbers and we will be doing this as long as we have the capability, which means we have enough quantity of numbers to do this thing.
For example, if we have input array as : 1,2,4.
So, I know that the summation of the array is 7, and will start by making 1, so that's <=7 then 2, now the summation is 3, still <=7, now, 3, summation became 6 which is <=7, but when I try going for 4, the summation becomes 10 which is not <=7, so we just say that we can make at most 3 distinct elements in the resultant array. That's it.
Problem Statement:-
You are given an array of integers. You can do the following two types of operations any (possibly zero) number of times:
- Pick two indices and . Change and remove the element from the array.
- Pick an index . Split into two positive integers and such that . Remove the element from the array and append elements and to the array.
Find the maximum number of distinct elements present in the array after performing any number of operations of the above two types.
Input Format
- The first line contains an integer denoting the number of test cases. The test cases then follow.
- The first line of each test case contains an integer - the size of the array.
- The second line of each test case contains space-separated integers .
Output Format
For each test case, output the maximum number of distinct elements present in the array after performing any number of operations of the above two types.
Constraints
Sample 1:
2 3 1 2 4 4 1 1 3 4
3 3
Explanation:
Test case : The maximum number of distinct elements that can be achieved by performing some finite number of operations on the given array is . Some examples of the final array are:
- : Perform no operation on the given array.
- : Perform operation . Choose . Here, . Break it as and . On removing and appending and , we get . This array has distinct elements.
Test case : The maximum number of distinct elements that can be achieved by performing some finite number of operations on the given array is . Some examples of the final array are:
- : Perform no operation on the given array.
- : Perform operation . Choose . Here, . Break it as and . On removing and appending and , we get . This array has distinct elements.
- : Perform operation . Choose and . On changing and removing , we get . This array has distinct elements.
Code Implementation:-
C++ Implementation:-
Explanation:-
The only logic is that by both the operations we can make change any number to any other number, like we can just start making a Series of Natural Numbers, but this will continue up till the summation of that series is <= the summation of the input list, when this condition fails, we just print the number of distinct elements that were in the resultant array.
Comments
Post a Comment