Wednesday 26 October 2016



   SOCKS MERCHANT 


A clothing store has a pile of  loose socks where each sock  is labeled with an integer, , denoting its color. The seller 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 .
The first line contains an integer, , denoting the number of socks.
The second line contains  space-separated integers describing the respective values of .
Sample Input
9
40 20 20 40 40 30 50 40 20
Sample Output
3

explanation :
pairs are :
20-20;
40-40;
40-40;



public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int c[] = new int[n];
        for(int c_i=0; c_i < n; c_i++){
            c[c_i] = in.nextInt();
        }
        int count=0;
        for(int i=0; i<n ; i++)
            {
                for(int j=i+1; j < n && c[i]!=-1;j++){
            if(c[i]==c[j])
               { count++;
                c[i]=-1;
                c[j]=-1;
                break;
            }
                }
        }
        System.out.println(count);
    }
}

No comments:

Post a Comment