Given N bottles. The ith bottle has A[i] radius. Once a bottle is enclosed inside another bottle, it ceases to be visible. The task is to minimize the number of visible bottles. You can put the ith bottle into a jth bottle if the following condition is fulfilled.
- ith bottle itself is not enclosed in another bottle.
- jth bottle does not enclose any other bottle.
- Radius of bottle i is smaller than bottle j ( i.e. A[i] < A[j] ).
Input : 8 1 1 2 3 4 5 5 4 Output : 2 Explanation: 1 -> 2 [1, 2, 3, 4, 5, 5, 4] 2 -> 3 [1, 3, 4, 5, 5, 4] 3 -> 4 [1, 4, 5, 5, 4] 4 -> 5 [1, 5, 5, 4] 1 -> 4 [5, 5, 4] 4 -> 5 [5, 5] Finally, there are 2 bottles left which are visible. Hence the answer is 2.
Approach: If you carefully observe, you will find that the number of minimum visible bottles will be equal to the maximum number of repeated bottles. Here intuition is, as these repeated bottles cannot be fit in single bigger bottle hence we require at least as many bigger bottles as the number of repeated bottles.
Below is the implementation of the above approach:
Minimum number of Visible Bottles are: 2
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Maximize count of empty water bottles from N filled bottles
- Number of visible boxes after putting one inside another
- Minimum number of bottles required to fill K glasses
- Find the maximum angle at which we can tilt the bottle without spilling any water
- Check if a circle lies inside another circle or not
- Find the number of rectangles of size 2*1 which can be placed inside a rectangle of size n*m
- Maximum number of envelopes that can be put inside other bigger envelopes
- Largest sphere that can be inscribed inside a cube
- Area of largest semicircle that can be drawn inside a square
- Count of smaller rectangles that can be placed inside a bigger rectangle
- Maximum in an array that can make another array sorted
- Count occurrences of a string that can be constructed from another given string
- Check if matrix can be converted to another matrix by transposing square sub-matrices
- Check if Array elements can be maximized upto M by adding all elements from another array
- Check if permutaion of one string can break permutation of another
- Find the minimum number of rectangles left after inserting one into another
- Minimum number of adjacent swaps required to convert a permutation to another permutation by given condition
- Count the number of rhombi possible inside a rectangle of given size
- Find the number of squares inside the given square grid
- Minimize number of boxes by putting small box inside bigger one