equalize the array hackerrank solution in java
for explanation watch video ::
code::
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
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) {
HashMap<Integer,Integer> hm = new HashMap<>();
for(int i=0;i<arr.size();i++){
hm.put(arr.get(i),hm.getOrDefault(arr.get(i),0)+1);
}
int max = Integer.MIN_VALUE;
for(Map.Entry<Integer,Integer> e : hm.entrySet()){
int value = e.getValue();
if(value > max){
max = value;
}
}
return arr.size()-max;
}
}
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();
}
}
Comments
Post a Comment