Skip to main content

Sock Merchant

Sock Merchant Hacker rank solution in c



John works at a clothing store. He has a large pile of socks that he must pair them by color for sale.


You will be given an array of integers representing the color of each sock. Determine how many pairs of socks with matching colors there are.

John works at a clothing store and he's going through a pile of socks to find the number of matching pairs.
More specifically, he has a pile of loose socks where each sock is labeled with an integer, n, denoting its color. He wants to sell as many socks as possible, but his customers will only buy them in matching
pairs. Two socks, and , are a single matching pair if they have the same color ( Ci==Cj).
Given and the color of each sock, how many pairs of socks can John sell?








Solution to sock merchant problem in just 2 STEPS:

Step 1:-
 Rearrange the given order in ascending order.
 

for(i=0;i<n;i++){
        for(j=i+1;j<n;j++)
        {
            if(arr[i]>arr[j]){
                a=arr[i];
                arr[i]=arr[j];
                arr[j]=a;
            }
        }
Given order is :
  EX- 10 20 20 10 10 30 50 10 20

After rearranging in ascending order:
         10 10 10 10 20 20 20 30 50

Step 2:-
 Now compare first element to it's next element if it matches then it's a pair.

  for(i=0;i<n;i++){
        for(j=i+1;j<n;j++){
            if(arr[i]==arr[j]){
                count++;
                i++;
                break;
            }
        }
    }

If you are beginner then I will advice to remove all data after header files and put your own code. It becomes easy to code then.

#include <assert.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(){
    int a,n,i,j,count=0;
    scanf("%d",&n);
    int arr[n];
    for(i=0;i<n;i++){
        scanf("%d",&arr[i]);
    }
    for(i=0;i<n;i++){
        for(j=i+1;j<n;j++)
        {
            if(arr[i]>arr[j]){
                a=arr[i];
                arr[i]=arr[j];
                arr[j]=a;
            }
        }
    }for(i=0;i<n;i++){
        for(j=i+1;j<n;j++){
            if(arr[i]==arr[j]){
                count++;
                i++;
                break;
            }
        }
    }
    printf("%d",count);                                     
    return 0;
}

Comments

Popular posts from this blog

Day 2: Conditional Statements: Switch

  Objective In this challenge, we learn about  switch statements . Check out the attached tutorial for more details. Task Complete the  getLetter(s)  function in the editor. It has one parameter: a string,  , consisting of lowercase English alphabetic letters (i.e.,  a  through  z ). It must return  A ,  B ,  C , or  D  depending on the following criteria: If the first character in string   is in the set  , then return  A . If the first character in string   is in the set  , then return  B . If the first character in string   is in the set  , then return  C . If the first character in string   is in the set  , then return  D . Hint:  You can get the letter at some index   in   using the syntax  s[i]  or  s.charAt(i) . Input Format Stub code in the editor reads a single string denoting   from ...

Day 2: Conditional Statements: If-Else

Day 2: Conditional Statements: If-Else || Hackerrank Solution Objective In this challenge, we learn about  if-else  statements. Check out the attached tutorial for more details. Task Complete the  getGrade(score)  function in the editor. It has one parameter: an integer,  , denoting the number of points Julia earned on an exam. It must return the letter corresponding to her   according to the following rules: If  , then  . If  , then  . If  , then  . If  , then  . If  , then  . If  , then  .

Jumping on the Clouds Hackerrank

Jumping on the Clouds solutions in c. Emma is playing a new mobile game that starts with consecutively numbered clouds. Some of the clouds are thunderheads and others are cumulus. She can jump on any cumulus cloud having a number that is equal to the number of the current cloud plus 1  or 2  . She must avoid the thunderheads. Determine the minimum number of jumps it will take Emma to jump from her starting position to the last cloud. It is always possible to win the game. For each game, Emma will get an array of clouds numbered 0   if they are safe or 1 if they must be avoided. For example, c=[0,1,0,0,0,1,0] indexed from 0....6. The number on each cloud is its index in the list so she must avoid the clouds at indexes 1 and 5 . She could follow the following two paths:0 ➜ 2 ➜ 4➜6   or 0 ➜ 2 ➜ 3 ➜ 4 ➜ 6 . The first path takes 3   jumps while the second takes 4 . Function Description Complete the  jumpingOnClouds ...