Header Ad

HackerRank Missing Numbers problem solution

In this HackerRank Missing Numbers problem solution Given two arrays of integers, find which elements in the second array are missing from the first array.

HackerRank Missing Numbers problem solution


Problem solution in Python.

from collections import Counter

k1 = input()
c1 = Counter(map(int, input().split()))
k2 = input()
c2 = Counter(map(int, input().split()))

if k1 > k2:
    c2, c1 = c1, c2
    
l = []
for k in c2.keys():
    if k not in c1 or c1[k] < c2[k]:
        l.append(k)
        
print(' '.join(map(str, sorted(l))))

{"mode":"full","isActive":false}


Problem solution in Java.

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

public class Solution {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] A = new int[n];

        for (int i = 0; i < n; i++) {
            A[i] = in.nextInt();
        }

        int m = in.nextInt();
        int[] B = new int[m];

        for (int i = 0; i < m; i++) {
            B[i] = in.nextInt();
        }

        HashMap<Integer, Integer> freqs = new HashMap<Integer, Integer>();

        for (int i = 0; i < m; i++) {
            if (freqs.containsKey(B[i])) {
                int freq = freqs.get(B[i]);
                freqs.replace(B[i], freq + 1);
            } else {
                freqs.put(B[i], 1);
            }
        }

        for (int i = 0; i < n; i++) {
            if (freqs.containsKey(A[i])) {
                int freq = freqs.get(A[i]);
                if (freq == 1) {
                    freqs.remove(A[i]);
                } else {
                    freqs.replace(A[i], freq - 1);
                }
            } else {
                System.out.println("error");
            }
        }

        StringBuilder answer = new StringBuilder();
        Iterator it = freqs.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry pair = (Map.Entry)it.next();
            answer.append(pair.getKey());
            answer.append(" ");
        }

        System.out.println(answer.toString());
    }
}

{"mode":"full","isActive":false}


Problem solution in C++.

#include<iostream>

using namespace std;

const int maxn = 10000;

int A[maxn*2 + 5];

int main() {
  int n, m;
  int xmin = maxn, xmax = -maxn;
  cin >> n;
  for( int i = 0; i<n; i++ ) {
    int tmp;
    cin >> tmp;
    A[tmp] --;
  }
  cin >> m;
  for( int i = 0; i<m; i++ ) {
    int tmp;
    cin >> tmp;
    A[tmp] ++;
    if (xmax < tmp) { xmax = tmp; }
    if (xmin > tmp) { xmin = tmp; }
  }
  for( int i=xmin; i<=xmax; i++ ) {
    if( A[i] > 0 ) {
      cout << i << " ";
    }
  }
  return 0;
}

{"mode":"full","isActive":false}


Problem solution in C.

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

#define read_int(x) scanf("%d", &x)
#define RANGE 100
#define BASE_IDX (RANGE + 1)
#define COUNT_SIZE (RANGE * 2)

int main() {
    
    int m, n; 
    int base, k, i;
    int count[COUNT_SIZE];
    
    memset(count, 0, COUNT_SIZE * sizeof(int));
    
    read_int(m);
    read_int(k);
    base = k;
    count[BASE_IDX] = 1;
    for (i = 1; i < m; i++) {
        read_int(k);
        count[BASE_IDX + (k - base)]++;
    }
    
    read_int(n);  
    for (i = 0; i < n; i++) {
        read_int(k);
        count[BASE_IDX + (k - base)]--;
    }

    for (i = 0; i < COUNT_SIZE; i++) {
        if (count[i] < 0) printf("%d ", base + (i - BASE_IDX));
    }
    printf("\n");
  
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */    
    return 0;
}

{"mode":"full","isActive":false}


Post a Comment

0 Comments