0

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 ;
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770

1 Answers1

1

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 { ... }).

D-RAJ
  • 3,263
  • 2
  • 6
  • 24
  • The `reinterpret_cast` that is done in `a = (int*)total_socks` is questionable. `a = &total_socks` or `*a = total_socks` would compile without casts but the assignment is ultimately pointless since it isn't used for anything. – Ted Lyngmo Jan 29 '21 at 14:15
  • 1
    @TedLyngmo Yeah it totally is. I just answered the OP's error. – D-RAJ Jan 29 '21 at 14:17
  • I suggest that you describe what `a = (int*)total_socks ;` really does to save OP from some headache :-) – Ted Lyngmo Jan 29 '21 at 14:18
  • 1
    Thank you for Your time , efforts and words .!! @D-RAJ – Harsh Kumar Sharma Jan 29 '21 at 18:15