HackerRank Sales by Match Problem Solution

 


HackerRank Sales by Match Problem Solution in C

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

int main(){
    int n,i,j,count=1,sum=0; 
    scanf("%d",&n);
    int *c = malloc(sizeof(int) * n);
    for( i = 0; i < n; i++){
       scanf("%d",&c[i]);
        
    }
    for(i=0;i<n;i++)
        {
        for(j=i+1;j<n;j++)
            {
               if(c[i]==c[j]&&c[i]!=-1)
                   {
                   count++;
                     c[j]=-1;}
               }
           sum=sum+count/2;
        count=1;
        
    }
    printf("%d",sum);
    return 0;
}

HackerRank Sales by Match Problem Solution in JavaScript

process.stdin.resume();
process.stdin.setEncoding('ascii');

var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;

process.stdin.on('data', function (data) {
    input_stdin += data;
});

process.stdin.on('end', function () {
    input_stdin_array = input_stdin.split("\n");
    main();    
});

function readLine() {
    return input_stdin_array[input_currentline++];
}

/////////////// ignore above this line ////////////////////

function main() {
    var n = parseInt(readLine());
    c = readLine().split(' ');
    c = c.map(Number);
    
    let counts = {}
    for (var i = 0; i < c.length; i++) {
        counts[c[i]] = counts[c[i]] === undefined ? 0.5 : counts[c[i]] + 0.5
    }
    let count = 0
    Object.keys(counts).map((a)=>count += Math.floor(counts[a]))
    console.log(count)
}

HackerRank Sales by Match Problem Solution in Python

input()

C = [0]*101
for color in map(int, input().strip().split()) :
    C[color] += 1

print(sum([c//2 for c in C]))

0 Comments