Maximizing Vessels with Unique Element Sizes in C++
In this article, we will discuss how to maximize vessels with unique element sizes in C++ with several approaches.
Problem Statement:
Given an array elements[] of size N, where elements[i] represent that we can use element i for at most elements[i] number of times, the task is to form the maximum number of vessels with elements. Each vessel must have distinct elements (no vessel should have duplicate elements), and the size of each vessel must be different.
Example 1:
Input:
elements[] = {3, 1, 5}, N = 3
Output:
3
Explanation:
We can use 0 at most 3 times, 1 at most twice, and 2 at most 5 times:
 Vessel 1: [2]
 Vessel 2: [0, 2]
 Vessel 3: [0, 1, 2]
So, the maximum output is 3 as each vessel size is different, and each vessel contains unique elements.
Example 2:
Input:
element[] = {3, 3, 12, 200}, N = 4
Output:
4
Explanation:
We can use 0 at most 3 times, 1 at most 3 times, 2 at most 12 times, and 3 at most 200 times.
 Vessel 1: [3]  Using the element with value 3.
 Vessel 2: [3, 2]  Using the elements with values 3 and 2.
 Vessel 3: [3, 2, 1]  Using the elements with values 3, 2, and 1.
 Vessel 4: [3, 2, 1, 0]  Using the elements with values 3, 2, 1, and 0.
So, the maximum output is 4 because each vessel size is different, and each vessel contains unique elements. The vessels are constructed to utilize the available elements in a manner that maximizes the number of vessels formed.
Brute Force Approach:
A brute force approach to this problem would involve generating all possible combinations of vessels and checking which combination satisfies the conditions mentioned in the problem statement. However, this approach becomes impractical as the size of the array increases, and the number of combinations grows exponentially with the input size.
The brute force approach would require nested loops to iterate through all possible combinations, making it inefficient and computationally expensive.
C++ Code:
Output:
Maximum number of vessels: 3
Explanation:
1. Header Inclusions:
In this example, the code includes necessary header files like <iostream>, <vector>, and <algorithm>.
2. Namespace:
The using namespace std; statement is used to simplify the code by allowing the use of standard C++ identifiers without the std:: prefix.
3. Function maximumVesselsBruteForce:
 This function takes a vector of integer elements representing vessels' capacities and an integer Nrepresenting the number of vessels.
 It initializes maxVessels to 0, which will eventually store the maximum number of vessels that can be formed.
 After that, the function iterates through all possible subsets of vessels using a bitmask (subset).
 For each subset, it calculates the total load (currentLoad) and the count of vessels in the subset (count).
 It checks if the current subset is valid based on the condition currentLoad >= (count * (count + 1)) / 2.
 If valid, it updates maxVessels with the maximum count.
 The function returns the maximum number of vessels.
4. main Function:
 In the main function, an example is given with N = 3 and elements = {3, 1, 5}.
 The maximumVesselsBruteForce function is called with these parameters, and the result is printed.
5. Example:
 For the input N = 3 and elements = {3, 1, 5}, the subsets considered are {}, {3}, {1}, {5}, {3, 1}, {3, 5}, {1, 5}, {3, 1, 5}.
 Among these subsets, {3, 1, 5} is the only valid subset (satisfying the condition currentLoad >= (count * (count + 1)) / 2).
 Therefore, the output of the program is 3 since the maximum number of vessels in a valid subset is 3.
Note: This approach has exponential time complexity and is not scalable for larger input sizes.
Time and Space Complexities
Time Complexity:
 The main contributor to the time complexity is the nested loop
 The outer loop runs in O(2^N) time, where N is the number of vessels, as it iterates through all possible subsets.
 The inner loop runs in O(N) time, where N is the number of vessels, as it iterates through the elements in each subset.
Therefore, the overall time complexity is O(N * 2^N).
Space Complexity:
 The space complexity is determined by the additional variables used in the function (currentLoad and count), which occupy constant space.
 The recursion stack also contributes to space complexity.
 Hence, the overall space complexity is O(N) due to the recursion stack.
Optimal Solution:
To find an optimal solution, we need to devise a strategy that efficiently identifies the maximum number of vessels satisfying the given conditions.
The optimal solution sorts elements based on frequency and original index, using a vector of pairs. It efficiently tracks used elements with a set, ensuring each vessel has distinct elements. The approach is scalable, providing a more efficient solution compared to the bruteforce method, particularly for larger input sizes. The use of sorting and a set enables a faster algorithm for identifying and maximizing vessels with unique element sizes. The preservation of original indices ensures the final result maintains the order of elements in the input array.
C++ Code:
Output:
Maximum number of vessels: 4
Explanation:
1. Function countMaximumVessels:
 This function takes a vector of integers vesselCapacities representing the capacities of vessels and an integer numberOfVessels representing the total number of vessels.
 It sorts the vesselCapacities vector in ascending order.
2. Variables:
 currentLoad: It keeps track of the current total load of vessels.
 maximumVessels: It keeps track of the maximum number of vessels that can be formed.
3. Iteration through Vessel Capacities:
 The function iterates through each vessel capacity in the sorted vector.
 For each capacity, it adds the capacity to the currentLoad
 After that, it checks if the current load is enough to accommodate the next vessel based on the condition currentLoad >= ((maximumVessels + 1) * (maximumVessels + 2)) / 2.
 If the condition is true, it increments maximumVessels because it means the current load can accommodate an additional vessel.
4. Return:
 The function returns the calculated maximumVessels, representing the maximum number of vessels that can be formed.
5. Main function:
 In the main function, an example is given with numberOfVessels = 4 and vesselCapacities = {3, 3, 12, 200}.
 The countMaximumVessels function is called with these parameters, and the result is stored in the variable result.
Time and Space Complexities
Time Complexity:
 The primary timeconsuming operation is sorting the vesselCapacities vector, which has a time complexity of O(N log N), where N is the number of elements in the vector.
 The subsequent iteration through the sorted vector has an O(N) linear time complexity.
 Therefore, the overall time complexity is O(N log N + N), which simplifies to O(N log N).
Space Complexity:
 The space complexity is determined by the additional variables used in the function (currentLoad and maximumVessels), which occupy constant space.
 Sorting is usually done inplace in C++, so it doesn't contribute to the space complexity.
 Hence, the overall space complexity is O(1) or constant.
