HackerRank Equalize the Array Problem Solution
In this post, we will solve HackerRank Equalize the Array Problem Solution.
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].
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].
Equalize the Array C Solution
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main()
{
int n;
scanf("%d", &n);
int* count = (int*)malloc(100*sizeof(int));
int i;
for(i=0; i<100; i++){count[i]=0;}
for(i=0; i<n; i++)
{
int t;
scanf("%d",&t);
count[t-1]++;
}
int max = 0;
for(i=0; i<100; i++)
{
if(max<count[i])max=count[i];
}
printf("%d\n",n-max);
return 0;
}
Equalize the Array C++ Solution
#include <iostream>
#include <cstring>
#include <climits>
#include <cmath>
#include <vector>
#include <cstdio>
#include <algorithm>
#include <float.h>
#include <map>
#include <unordered_map>
#include <queue>
#include <bitset>
#define ui unsigned int
#define ull unsigned long long
#define ll long long
#define iloop(i,j,n) for(ll i=j;i<n;i++)
#define dloop(i,j,n) for(ll i=j;i>=n;i--)
#define all(v) v.begin(),v.end()
#define sc(n) scanf("%d",&n)
#define sc2(i,j) scanf("%d %d",&i,&j)
#define sc3(i,j,k) scanf("%d %d %d",&i,&j,&k)
#define sc4(i,j,k,l) scanf("%d %d %d %d",&i,&j,&k,&l)
#define llsc(n) scanf("%lld",&n)
#define llsc2(i,j) scanf("%lld %lld",&i,&j)
#define llsc3(i,j,k) scanf("%lld %lld %lld",&i,&j,&k)
#define llsc4(i,j,k,l) scanf("%lld %lld %lld %lld",&i,&j,&k,&l)
#define pll(n) printf("%lld\n",n);
#define pi(n) printf("%d\n",n);
#define pb(i) push_back(i)
#define dbl double
#define mp(i,j) make_pair(i,j)
#define set(a,i) memset(a,i,sizeof(a))
#define vi vector <int>
#define vll vector <ll>
#define vpi vector < pair <int,int> >
#define vpll vector < pair <ll,ll> >
#define f first
#define s second;
using namespace std;
const int mod = 1000000007;
int main()
{
int n;sc(n);
int a[n];
int mapping[101];
set(mapping,0);
iloop(i,0,n)
{
sc(a[i]);
mapping[a[i]]++;
}
int ct=0;
iloop(i,0,101)
{
if(ct < mapping[i])
ct = mapping[i];
}
cout << (n-ct) << endl;
return 0;
}
Equalize the Array C Sharp Solution
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {
static void Main(String[] args) {
Console.ReadLine();
int[] arr = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse);
Console.WriteLine(EqualizeArray(arr));
}
static int EqualizeArray(int[] arr) {
Dictionary<int, int> freq = new Dictionary<int, int>();
foreach(var item in arr) {
if (freq.ContainsKey(item))
freq[item]++;
else
freq.Add(item, 1);
}
int maxFreq = freq.Values.Max();
return arr.Length - maxFreq;
}
}
Equalize the Array Java Solution
import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
import static java.util.stream.Collectors.toList;
class Result {
/*
* Complete the 'equalizeArray' function below.
*
* The function is expected to return an INTEGER.
* The function accepts INTEGER_ARRAY arr as parameter.
*/
public static int equalizeArray(List<Integer> arr) {
List<Integer> values = List.copyOf(arr);
HashMap<Integer, Integer> objectFrequencyMap = new HashMap<>();
arr.stream().distinct().collect(toList()).forEach(uniqueObject -> {
objectFrequencyMap.put(uniqueObject, (int) values.stream().filter(f -> f.equals(uniqueObject)).count());
});
objectFrequencyMap.entrySet()
.stream()
.sorted(Map.Entry.comparingByValue())
.forEach(System.out::println);
Integer maxFrequency = objectFrequencyMap.values().stream().sorted().collect(toList()).get(objectFrequencyMap.size() - 1);
return arr.size() - maxFrequency;
}
}
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
int n = Integer.parseInt(bufferedReader.readLine().trim());
List<Integer> arr = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
.map(Integer::parseInt)
.collect(toList());
int result = Result.equalizeArray(arr);
bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
bufferedReader.close();
bufferedWriter.close();
}
}
Equalize the Array Javascript Solution
function processData(input) {
// Read input
input = input.split('\n');
let line = 0,
readLine = () => input[line++], // Read a string
readNumber = () => Number(readLine()), // Read a single integer
readNumberList = () => readLine().split(' ').map(Number), // Read multiple integers
log = (x) => console.log(x);
const n = readNumber();
const arr = readNumberList();
let c = Array(Math.max(...arr)+1).fill(0);
for (let i=0; i<n; i++){
c[arr[i]]++;
}
let max = Math.max(...c);
console.log(n-max);
}
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 Python Solution
s = int(input())
A = [int(i) for i in input().split(' ')]
if A.count(A[0]) == s: print(0)
else: print(s - A.count(max(set(A), key=A.count)))
Other Solutions
Hello mates, pleasant piece of writing and good arguments commented at this place, I am actually enjoying by these.
Hi there! I could have sworn I’ve visited this web site before but after browsing through some of the articles I realized it’s new to me. Regardless, I’m definitely pleased I stumbled upon it and I’ll be bookmarking it and checking back often!