We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Problem Breakdown:
If there is only 1 color of socks, the person just needs to remove 2 socks to guarantee a matching pair (since all socks are of the same color).
If there are 2 colors of socks, the person could theoretically remove one of each color first, meaning that they need to remove 3 socks to ensure that they have a matching pair.
For n colors, the worst-case scenario is that the person picks one sock from each color without getting a pair. In this case, after removing n socks, all would be of different colors. Therefore, to guarantee a matching pair, the person needs to remove n + 1 socks.
Approach:
The general formula is:
minimum socks to remove
𝑛
+
1
minimum socks to remove=n+1 Where n is the number of different colors of socks. def maximumDraws(n):
return n + 1
Input handling
t = int(input()) # number of test cases
for _ in range(t):
n = int(input())
print(maximumDraws(n))
input
2
1
2
output
2
3
Function Definition:
We need to create a function called maximumDraws that takes the number of colors of socks n as input and returns the minimum number of socks to remove to guarantee a matching pair.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
An unexpected error occurred. Please try reloading the page. If problem persists, please contact support@hackerrank.com
Maximum Draws
You are viewing a single comment's thread. Return to all comments →
Problem Breakdown: If there is only 1 color of socks, the person just needs to remove 2 socks to guarantee a matching pair (since all socks are of the same color). If there are 2 colors of socks, the person could theoretically remove one of each color first, meaning that they need to remove 3 socks to ensure that they have a matching pair. For n colors, the worst-case scenario is that the person picks one sock from each color without getting a pair. In this case, after removing n socks, all would be of different colors. Therefore, to guarantee a matching pair, the person needs to remove n + 1 socks. Approach: The general formula is:
minimum socks to remove
𝑛 + 1 minimum socks to remove=n+1 Where n is the number of different colors of socks. def maximumDraws(n): return n + 1
Input handling
t = int(input()) # number of test cases for _ in range(t): n = int(input()) print(maximumDraws(n)) input 2 1 2 output 2 3
Function Definition: We need to create a function called maximumDraws that takes the number of colors of socks n as input and returns the minimum number of socks to remove to guarantee a matching pair.