HackerRank Sales by Match problem solution

In this HackerRank Sales by Match problem in the Interview preparation kit, you need to Complete the sockMerchant function. it has the following parameter(s):

  • int n: the number of socks in the pile
  • int ar[n]: the colors of each sock


HackerRank Sales by Match - Interview Preparation kit solution


Problem solution in Python programming.

#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the sockMerchant function below.
def sockMerchant(n, ar):
    num = 0
    for i in range(0,n):
        gum = 1
        for j in range(i+1,n):
            if ar[i] == None:
                continue
            if ar[i] == ar[j] and gum ==1:
                num = num + 1
                gum = gum + 1
                ar[j] = None
            
    return num

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    n = int(input())

    ar = list(map(int, input().rstrip().split()))

    result = sockMerchant(n, ar)

    fptr.write(str(result) + '\n')

    fptr.close()



Problem solution in Java8 Programming.

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner s = new Scanner(System.in);
        int[] freq = new int[101];
        int n = s.nextInt();
        for(int i = 0; i < n; i++){
            int x = s.nextInt();
            freq[x]++;
        }
        int total = 0;
        for(int i = 1; i < 101; i++){
            total+=freq[i]/2;
        }
        System.out.println(total);
    }
}


Problem solution in C++ programming.

#include <bits/stdc++.h>

using namespace std;
using LINT = long long int;
using PII = pair<int,int>;

#define PB push_back
#define FI first
#define SE second
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i, a, b) for(int i=(a);i<(b);++i)

int socks[107];

int main() {
    int n;
    cin >> n;
    REP(i,n){
        int x;
        cin>>x;
        socks[x]++;
    }

    int ans = 0;
    REP(i,107)ans+=socks[i]/2;
    cout<<ans<<endl;

    return 0;
}


Problem solution in C programming.

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {

    int n;
    scanf("%d",&n);
    int arr[101];
    for(int i=0;i<101;i++){
        arr[i]=0;
    }
    for(int i=0;i<n;i++){
        int j;
        scanf("%d",&j);
        arr[j] += 1;
    }
    int total=0;
    for(int i=0;i<101;i++){
        if(arr[i]> 1){
            if(arr[i] % 2 == 0) total += (arr[i] / 2);
            else total += ((arr[i]-1) / 2);
        }
    }
    printf("%d",total);
    
    
    
    return 0;
}


Problem solution in JavaScript programming.

function processData(input) {
    //Enter your code here
  var pInput = input.split('\n');
  var n = pInput[0];
  var pInput = pInput[1].split(' ');
  var oddItems = {};
  pInput.map(function (i) {
    if(oddItems[i]) {
      delete oddItems[i];
    } else {
      oddItems[i] = true;
    }
  })
  console.log((n - Object.keys(oddItems).length)/2);
} 

process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
    _input += input;
});

process.stdin.on("end", function () {
   processData(_input);
});


Post a Comment

3 Comments

  1. another approach for python:

    def sockMerchant(n, ar):
    # Write your code here
    unique_colors = set(ar)
    total_pairs = 0
    for color in unique_colors:
    total_pairs += (int(ar.count(color)/2))
    return total_pairs

    ReplyDelete