0
votes

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 least k 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 ;
1
The function find_min invokes undefined behavior because there are no return statement in that.MikeCAT
Also it is bad to add things to uninitialized variables.MikeCAT
what do you think a = (int)total_socks ; is doing? a is a int* and total_socks is an int, you cannot assign one to the other like this463035818_is_not_a_number
It's a good habit to never use using namespace std - always try and use e.g. std::cout instead.user438383

1 Answers

1
votes

The problem is because your trying to assign an int to a variable of type int* here: a = (int)total_socks ; I think what your looking for is a = (int*)total_socks ;. But still it is pointless as your not using the variable again (unless your actually planning on returning it). So is the rest of them, n and k.

Theres another problem with your code, the function int find_min(int a[], int n, int k) is of return type int. But in the function body, your not returning anything. This will result in undefined behavior. So make sure that you return something in that function.

Additional: Make sure you initialize all your variables. Over here: int total , total_pairs , total_socks ; you don't initialize it to any value so later on, it might not give you the result you require because it already contains junk.

And try not to use using namespace std; as its not a good practice. So is #include <bits/stdc++.h>. Include the files you want and use the :: operator to access their namespace instead.

Unrelated: Do you need a class just for that function? It seems like you don't (by looking at the code you have provided). Just keep the function in the global namespace but if you want it to be inside a namespace, put it inside one (ie: namespace Solution { ... }).