Header Ad

HackerRank Equalize the Array problem solution

In this HackerRank Equalize the Array problem you have Given an array of integers, determine the minimum number of elements to delete to leave only elements of equal value.

HackerRank Equalize the Array problem solution


Problem solution in Python programming.

#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the equalizeArray function below.
def equalizeArray(arr):
    return (len(arr) - arr.count(max(set(arr), key = arr.count)))



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

    n = int(input())

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

    result = equalizeArray(arr)

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

    fptr.close()


Problem solution in Java Programming.

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        int[] occurances = new int[101];
        int n = in.nextInt();
        int maxOccurances = 0;
        //int[] a = new int[n];
        for (int i = 0; i < n; i++)
        {
            int ai = in.nextInt();
            occurances[ai]++;
            if (occurances[ai] > maxOccurances)
            {
                maxOccurances = occurances[ai];
            }
        }
        System.out.println(n - maxOccurances);
    }
}


Problem solution in C++ programming.

#include<bits/stdc++.h>
#define mp make_pair
#define PII pair<int,int>
#define fi first
#define se second
using namespace std;

const int NMAX=105;

int n,a[NMAX],fr[NMAX];

int main()
{
    int i,mx;
   // freopen("date.in","r",stdin);
   // freopen("date.out","w",stdout);
    cin.sync_with_stdio(false);
    cin>>n;mx=0;
    for (i=1;i<=n;i++)
    {
        cin>>a[i];
        fr[a[i]]++;
        mx=max(mx,fr[a[i]]);
    }
    mx=n-mx;
    cout<<mx<<"\n";
    return 0;
}


Problem solution in C programming.

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

int main() {

    /* Enter your code here. Read input from STDIN. Print output to STDOUT */    
    int N;
    scanf("%d", &N);
    int A[N];
    for(int n = 0; n < N; n++) {
        scanf("%d", &(A[n]));
    }
    int res = 0, count = 0;
    for(int i = 0; i < N; i++) {
        count = 0;
        for(int j = i; j < N; j++) {
            if(A[j] == A[i])
                count++;
        }
        if (count > res)
            res = count;
    }
    printf("%d\n", N - res);
    return 0;
}


Problem solution in JavaScript programming.

function processData(input) {
    //Enter your code here
    var arr = input.split('\n');
    var len = Number(arr[0]);
    var numbers = arr[1].split(" ").map(Number);
    
    function deleteElement(a, l){
        var dict = {}; 
        var key; 
        var max = 0; 
        for(var i = 0; i < l; i++){
            key = a[i]
            if(key in dict){
                dict[key]++; 
            } else {
                dict[key] = 1; 
            }
        }
        for(var prop in dict){
            if(dict[prop] > max){
                max = dict[prop];
            }
        }
        return l - max; 
    }
    console.log(deleteElement(numbers, len).toString());
} 

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

0 Comments