Minimum number of socks required to picked to have at least K pairs of the same color | 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
function findMin ( arr , N , k ) { let pairs = 0 ; for ( let i = 0 ; i < N ; i ++ ) { pairs += arr [ i ] / 2 ; } if ( k > pairs ) return - 1 ; else return 2 * k + N - 1 ; } let arr = [ 4 , 5 , 6 ] ; let K = 3 ; let N = arr . length ; document . write ( findMin ( arr , N , K ) ) ;