You are viewing a single comment's thread. Return to all comments →
My memory efficient C solution
int sockMerchant(int n, int* ar) { int * socks = calloc(2,sizeof(int)); int ssize = 2; *socks = *ar; for(int i = 0; i < n; i++){ bool found = false; for(int j = 0; j < ssize; j+=2){ if(ar[i] == socks[j]){ socks[j+1]++; found = true; break; } } if(!found){ ssize+=2; socks = realloc(socks,ssize*sizeof(int)); socks[ssize-2] = ar[i]; socks[ssize-1] = 1; } } int pairs = 0; for(int i = 1; i < ssize;i+=2){ pairs+=socks[i]/2; } return pairs; }
Sales by Match
You are viewing a single comment's thread. Return to all comments →
My memory efficient C solution