HackerRank: Sales by Match

There is a large pile of socks that must be paired by color. Given an array of integers representing the color of each sock, determine how many pairs of socks with matching colors there are.

int sockMerchant(int n, int ar_count, int* ar) {
    
    int* count = malloc(sizeof(int)*200); 
    int pair=0;
    
    for(int i=0; i<n; i++){
       count[ar[i]]+=1;
    }
    for(int i=0; i<200; i++){
    printf("%d ", count[i]);}  
    for(int j=0; j<200; j++){
         pair+=ceil(count[j]/2);
    }
 printf("%d", pair);       
 return pair;
}

Leave a Comment