Open In App

Minimum number of socks required to picked to have at least K pairs of the same color

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: 

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++ program for the above approach
 
#include <iostream>
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[3] = { 4, 5, 6 };
    int K = 3;
    cout << findMin(arr, 3, K);
    return 0;
}
 
// This code is contributed by RohitOberoi.




// 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# 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.




# 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.




<script>
 
// JavaScript program to implement
// the above approach
 
    // Function to count the minimum
    // number of socks to be picked
    function findMin(
        arr, N, k)
    {
        // Stores the total count
        // of pairs of socks
        let pairs = 0;
  
        // Find the total count of pairs
        for (let 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
 
        let arr = [ 4, 5, 6 ];
        let K = 3;
        let N = arr.length;
        document.write(findMin(arr, N, K));
            
</script>

Output: 
8

 

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


Article Tags :