HackerRank Equalize the Array Solution

Hello Programmers, In this post, you will know how to solve the HackerRank Equalize the Array Solution. This problem is a part of the HackerRank Algorithms Series.

Ezoicreport this adHackerRank Equalize the Array Solution
HackerRank Equalize the Array Solution

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Hackerrank by yourself. If you find any difficulty after trying several times, then you can look for solutions.

HackerRank Equalize the Array Solution

Ezoicreport this adTask

Given an array of integers, determine the minimum number of elements to delete to leave only elements of equal value.

Example

arr = [1, 2, 2, 3]

Delete the 2 elements 1 and 3 leaving arr = [2, 2]. If both twos plus either the 1 or the 3 are deleted, it takes 3 deletions to leave either [3] or [1]. The minimum number of deletions is 2.

Function Description

Complete the equalizeArray function in the editor below.

equalizeArray has the following parameter(s):

  • int arr[n]: an array of integers

Returns

  • int: the minimum number of deletions required

Input Format

The first line contains an integer n, the number of elements in arr.
The next line contains n space-separated integers arr[i].

Constraints

  • 1 <= n <= 100
  • 1 <= arr[i] <= 100

Sample Input

STDIN Function
—– ——–
5 arr[] size n = 5
3 3 2 1 3 arr = [3, 3, 2, 1, 3]

Sample Output

2

Explanation

Delete arr[2] = 2 and arr[3] = 1 to leave arr‘ = [3, 3, 3]. This is minimal. The only other options are to delete 4 elements to get an array of either [1] or [2].

HackerRank Equalize the Array Solutions

Equalize the Array Solution in C

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
    int i,n,l;
scanf("%d",&n);
    int arr[n];
    for(i=0;i<n;i++){scanf("%d",&arr[i]);}
    int brr[101]={0};
    for(i=0;i<n;i++){brr[arr[i]]++;}
    l=0;
    for(i=0;i<101;i++){
        if(brr[i]>l){l=brr[i];}
    }
    printf("%d",n-l);
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */    
    return 0;
}

Equalize the Array Solution in Cpp

#include <bits/stdc++.h>
using namespace std;
#define DEBUG(x)    cerr << #x << " = " << x << endl
#define INPUT       freopen("Data.inp", "r", stdin)
#define OUTPUT      freopen("Data.out", "w", stdout)
typedef long long LL;
typedef pair<int, int> II;
typedef vector<int> VI;
const int N = (int) 1e2 + 10;
int n, a[N], c[N];
int main() {
    #ifdef LOCAL
        INPUT;
        OUTPUT;
    #endif
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
    for (int i = 1; i <= n; ++i) c[a[i]]++;
    printf("%d", n - *max_element(c + 1, c + 101));
    return 0;
}

Equalize the Array Solution in Java

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 sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] counts = new int[101];
        for (int i = 0; i < n; i++) {
            counts[sc.nextInt()]++;
        }
        Arrays.sort(counts);
        System.out.println(n-counts[100]);
    }
}
Ezoicreport this ad

Equalize the Array Solution in Python

from collections import Counter
n = input()
A = map(int,raw_input().strip().split())
cts = Counter(A)
print n-max(cts.values())

Equalize the Array Solution using JavaScript

// Getting automated Lines
var arrL = [];
var arrN = 0;
function readLine() { var r = arrL[arrN];arrN++;return r;}
function processData(input) {
	arrL = input.split("\n");
    var N = parseInt(readLine());
    var arr = readLine().split(" ");
    console.log(calculate(arr));
}
function calculate(arr) {
    var rep = [];
    for(var i =0; i < arr.length;i++) {
        rep[i] = 0;
        arr[i] = parseInt(arr[i]);
    }
    for(var i =0; i < arr.length;i++) {
        for(var e = 0; e < arr.length;e++) {
            if(i != e) {
                if(arr[e] == arr[i]) {
                    rep[i] ++;
                    
                }
            }
        }
    }
    var maxOne = 0;
    var maxVal = 0;
    for(var i =0; i < rep.length;i++) {
        if(rep[i] > maxVal) {
            maxVal = rep[i];
            maxOne = i;
        }
    }
    var needsToStay = arr[maxOne];
    var count = 0;
    for(var i =0; i < arr.length;i++) {
        if(arr[i] != needsToStay) {
            count++;
        }
    }
    return count;
}
process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
    _input += input;
});
process.stdin.on("end", function () {
   processData(_input);
});

Equalize the Array Solution in Scala

object Solution {
    def main(args: Array[String]) {
        val sc = new java.util.Scanner (System.in);
        val n = sc.nextInt();
        var a0 = 0;
        var seq=List[Int]()
        while(a0 < n){
            seq=sc.nextInt()::seq
            a0+=1;
        }
        val distinct=seq.groupBy(identity).mapValues(_.size)
        println(n-distinct.valuesIterator.max)
    }
}

Equalize the Array Solution in Pascal

(* Enter your code here. Read input from STDIN. Print output to STDOUT *)
var n,max,i:integer;
    a,b:array[1..101] of integer;
begin
 readln(n);
 max:=0;
 for i:=1 to n do
  begin
    read(a[i]);
    inc(b[a[i]]);
    if b[a[i]]>max then max:=b[a[i]];
  end;
 writeln(n-max);
end.

Disclaimer: This problem (Equalize the Array) is generated by HackerRank but the Solution is Provided by BrokenProgrammers. This tutorial is only for Educational and Learning purposes.

Next: HackerRank Library Fine Solution

Sharing Is Caring

Leave a Comment

Ezoicreport this ad