# Minimum number of Bottles visible when a bottle can be enclosed inside another Bottle

Given N bottles. The i^{th} 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 i^{th} bottle into a j^{th} bottle if the following condition is fulfilled.

- i
^{th}bottle itself is not enclosed in another bottle. - j
^{th}bottle does not enclose any other bottle. - Radius of bottle i is smaller than bottle j ( i.e. A[i] < A[j] ).

**Examples:**

Input :8 1 1 2 3 4 5 5 4Output :2Explanation: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:

## C++

`#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `void` `min_visible_bottles(` `int` `* arr, ` `int` `n) ` `{ ` ` ` `map<` `int` `, ` `int` `> m; ` ` ` `int` `ans = 0; ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `m[arr[i]]++; ` ` ` `ans = max(ans, m[arr[i]]); ` ` ` `} ` ` ` ` ` `cout << ` `"Minimum number of "` ` ` `<< ` `"Visible Bottles are: "` ` ` `<< ans << endl; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 8; ` ` ` `int` `arr[] = { 1, 1, 2, 3, 4, 5, 5, 4 }; ` ` ` ` ` `// Find the solution ` ` ` `min_visible_bottles(arr, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 code for the above approach ` `def` `min_visible_bottles(arr, n): ` ` ` ` ` `m ` `=` `dict` `() ` ` ` `ans ` `=` `0` ` ` `for` `i ` `in` `range` `(n): ` ` ` `m[arr[i]] ` `=` `m.get(arr[i], ` `0` `) ` `+` `1` ` ` `ans ` `=` `max` `(ans, m[arr[i]]) ` ` ` ` ` `print` `(` `"Minimum number of"` `, ` ` ` `"Visible Bottles are: "` `, ans) ` ` ` `# Driver code ` `n ` `=` `8` `arr ` `=` `[` `1` `, ` `1` `, ` `2` `, ` `3` `, ` `4` `, ` `5` `, ` `5` `, ` `4` `] ` ` ` `# Find the solution ` `min_visible_bottles(arr, n) ` ` ` `# This code is contributed ` `# by Mohit Kumar ` |

*chevron_right*

*filter_none*

**Output:**

Minimum number of Visible Bottles are: 2

## Recommended Posts:

- Number of visible boxes after putting one inside another
- Minimum number of bottles required to fill K glasses
- Count the number of rhombi possible inside a rectangle of given size
- Find the number of rectangles of size 2*1 which can be placed inside a rectangle of size n*m
- Maximum and minimum of an array using minimum number of comparisons
- Find minimum number to be divided to make a number a perfect square
- Find a number which give minimum sum when XOR with every number of array of integers
- Minimum number of given powers of 2 required to represent a number
- Minimum number of Binary strings to represent a Number
- Convert a number m to n using minimum number of given operations
- Minimum number of squares whose sum equals to given number n
- Minimum number of bombs
- Minimum number with digits as 4 and 7 only and given sum
- Add minimum number to an array so that the sum becomes even
- Sum of Area of all possible square inside a rectangle

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.