A drawer contains socks of n different colors. The number of socks available of i'th color is given by
a[i]
where a is an array of n elements. Tony wants to take k pairs of socks out of the drawer. However, he cannot see the color of the sock that he is picking. You have to tell what is the minimum number of socks Tony has to pick in one attempt from the drawer such that he can be absolutely sure, without seeing their colors, that he will have at leastk
matching pairs.
My solution:
#include <bits/stdc++.h>
using namespace std;
class Solution{
public:
int find_min(int a[], int n, int k) {
int total , total_pairs , total_socks ;
for (int i = 0 ; i < n ;i++)
{
total_socks += a[i];
total_pairs += (a[i]/2) ;
}
total = total_socks - total_pairs + k ;
a = (int)total_socks ;
n = total_socks;
k = total;
}
};
// { Driver Code Starts.
int main() {
int t;
cin >> t;
while (t--) {
int n, k;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) cin >> a[i];
cin >> k;
Solution obj;
cout << obj.find_min(a, n, k) << endl;
}
return 1;
}
But I get this error:
Error : error: invalid conversion from int to int* [-fpermissive]
a = (int)total_socks ;
find_min
invokes undefined behavior because there are noreturn
statement in that. – MikeCATa = (int)total_socks ;
is doing?a
is aint*
andtotal_socks
is anint
, you cannot assign one to the other like this – 463035818_is_not_a_numberusing namespace std
- always try and use e.g.std::cout
instead. – user438383