Minimum number of socks required to picked to have at least K pairs of the same color | Java 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
import java . io . * ; class GFG { public static 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 ; } 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 ) ) ; } }