Minimum number of socks required to picked to have at least K pairs of the same color | C ++ program for the above approach ; Function to count the minimum number of socks to be picked ; Stores the total count of pairs of socks ; Find the total count of pairs ; If K is greater than pairs ; Otherwise ; Driver Code
#include <iostream> NEW_LINE using namespace std ; int findMin ( int arr [ ] , int N , int k ) { int pairs = 0 ; for ( int i = 0 ; i < N ; i ++ ) { pairs += arr [ i ] / 2 ; } if ( k > pairs ) return -1 ; else return 2 * k + N - 1 ; } int main ( ) { int arr [ 3 ] = { 4 , 5 , 6 } ; int K = 3 ; cout << findMin ( arr , 3 , K ) ; return 0 ; }