Related Articles
Minimum number of socks required to picked to have at least K pairs of the same color
• Last Updated : 05 May, 2021

Given an array arr[] consisting of N integers such that arr[i] representing the number of socks of the color i and an integer K, the task is to find the minimum number of socks required to be picked to get at least K pairs of socks of the same color.

Examples:

Input: arr[] = {3, 4, 5, 3}, K = 6
Output: 15
Explanation: One will need to pick all the socks to get at least 6 pairs of matching socks.

Input: arr[] = {4, 5, 6}, K = 3
Output: 8

Approach: The given problem can be solved based on the following observations:

• According to Pigeonhole’s Principle i.e., in the worst-case scenario if N socks of different colors have been picked then the next pick will form a matching pair of socks.
• Suppose one has picked N socks of different colors then, for each (K – 1) pairs one will need to pick two socks, one for forming a pair and another for maintaining N socks of all different colors, and for the last pair, there is only need to pick a single sock of any color available.

Therefore, the idea is to find the total number of pairs that can be formed by the same colors and if the total count is at most K then print (2*K + N – 1) as the minimum count of pairs to be picked. Otherwise, print “-1” as there are not enough socks to formed K pairs.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach` `#include ``using` `namespace` `std;` `// Function to count the minimum``// number of socks to be picked``int` `findMin(``int` `arr[], ``int` `N, ``int` `k)``{``    ``// Stores the total count``    ``// of pairs of socks``    ``int` `pairs = 0;` `    ``// Find the total count of pairs``    ``for` `(``int` `i = 0; i < N; i++) {``        ``pairs += arr[i] / 2;``    ``}` `    ``// If K is greater than pairs``    ``if` `(k > pairs)``        ``return` `-1;` `    ``// Otherwise``    ``else``        ``return` `2 * k + N - 1;``}` `int` `main()``{``    ``int` `arr = { 4, 5, 6 };``    ``int` `K = 3;``    ``cout << findMin(arr, 3, K);``    ``return` `0;``}` `// This code is contributed by RohitOberoi.`

## Java

 `// Java program for the above approach` `import` `java.io.*;` `class` `GFG {` `    ``// Function to count the minimum``    ``// number of socks to be picked``    ``public` `static` `int` `findMin(``        ``int``[] arr, ``int` `N, ``int` `k)``    ``{``        ``// Stores the total count``        ``// of pairs of socks``        ``int` `pairs = ``0``;` `        ``// Find the total count of pairs``        ``for` `(``int` `i = ``0``; i < N; i++) {``            ``pairs += arr[i] / ``2``;``        ``}` `        ``// If K is greater than pairs``        ``if` `(k > pairs)``            ``return` `-``1``;` `        ``// Otherwise``        ``else``            ``return` `2` `* k + N - ``1``;``    ``}` `    ``// Driver Code``    ``public` `static` `void` `main(String[] args)``    ``{``        ``int``[] arr = { ``4``, ``5``, ``6` `};``        ``int` `K = ``3``;``        ``int` `N = arr.length;``        ``System.out.println(findMin(arr, N, K));``    ``}``}`

## C#

 `// C# program for the above approach` `using` `System;` `class` `GFG {` `    ``// Function to count the minimum``    ``// number of socks to be picked``    ``public` `static` `int` `findMin(``int``[] arr, ``int` `N, ``int` `k)``    ``{``        ``// Stores the total count``        ``// of pairs of socks``        ``int` `pairs = 0;` `        ``// Find the total count of pairs``        ``for` `(``int` `i = 0; i < N; i++) {``            ``pairs += arr[i] / 2;``        ``}` `        ``// If K is greater than pairs``        ``if` `(k > pairs)``            ``return` `-1;` `        ``// Otherwise``        ``else``            ``return` `2 * k + N - 1;``    ``}` `    ``// Driver Code``    ``public` `static` `void` `Main(``string``[] args)``    ``{``        ``int``[] arr = { 4, 5, 6 };``        ``int` `K = 3;``        ``int` `N = arr.Length;``        ``Console.WriteLine(findMin(arr, N, K));``    ``}``}` `// This code is contributed by ukasp.`

## Python3

 `# Python program for the above approach` `# Function to count the minimum``# number of socks to be picked``def` `findMin(arr, N, k):``  ` `    ``# Stores the total count``    ``# of pairs of socks``    ``pairs ``=` `0``    ` `    ``# Find the total count of pairs``    ``for` `i ``in` `range``(N):``        ``pairs ``+``=` `arr[i] ``/` `2``    ` `    ``# If K is greater than pairs``    ``if` `(k > pairs):``        ``return` `-``1` `    ``# Otherwise``    ``else``:``        ``return` `2` `*` `k ``+` `N ``-` `1``        ` `arr ``=` `[``4``, ``5``, ``6``]``k ``=` `3``print``(findMin(arr, ``3``, k));``    ` `# This code is contributed by SoumikMondal.`

## Javascript

 ``
Output:
`8`

Time Complexity: O(N)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up