Skip to content
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Learn everything about programming

HackerEarth Erase to max problem solution

YASH PAL, 31 July 2024
In this HackerEarth Erase to max problem solution, you are given an array A of size N. You have to select any value ‘x’ from A and remove its all occurrences. You need to perform this operation exactly once. You need to obtain the maximum possible final sum of A.
HackerEarth Erase to max problem solution

HackerEarth Erase to max problem solution.

import java.util.*;
import java.io.*;
import java.lang.*;
import java.math.*;
import static java.lang.Math.*;
import java.util.concurrent.ThreadLocalRandom;

public class Sol implements Runnable {

long mod = (long)1e9 + 7;

void solve(InputReader in, PrintWriter w) {

int t = in.nextInt();

while(t-- > 0) {

int N = in.nextInt();
HashMap<Long, Integer> map = new HashMap<>();

for(int i=0;i<N;i++) {
long x = in.nextInt();
map.put(x, map.getOrDefault(x, 0) + 1);
}

long minSum = Long.MAX_VALUE;
long totalSum = 0;

for(long key : map.keySet()) {
long sumHere = key * (long)map.get(key);

if(minSum > sumHere) {
minSum = sumHere;
}

totalSum += sumHere;
}

totalSum -= minSum;

w.println(totalSum);
}
}

void init() throws Exception {
InputReader in;
PrintWriter w;
boolean online = false;

String common_in_fileName = "\in";
String common_out_fileName = "\out";
int test_files = 0;

for (int file_no = 0; file_no <= test_files; file_no++) {

String x = "" + file_no;
if (x.length() == 1) x = "0" + x;

String in_fileName = common_in_fileName + "" + x;
String out_fileName = common_out_fileName + "" + x;

if (online) {
in = new InputReader(new FileInputStream(new File(in_fileName + ".txt")));
w = new PrintWriter(new FileWriter(out_fileName + ".txt"));
} else {
in = new InputReader(System.in);
w = new PrintWriter(System.out);
}

solve(in, w);
w.close();
}
}

public void run() {
try {
init();
} catch (Exception e) {
System.out.println(e);
e.printStackTrace();
}
}

public static void main(String args[]) throws Exception {
new Thread(null, new Sol(), "Sol", 1 << 28).start();
}

static class InputReader {

private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;

public InputReader(InputStream stream) {
this.stream = stream;
}

public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}

if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}

if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}

public String nextLine() {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}

public int nextInt() {
int c = read();

while (isSpaceChar(c)) {
c = read();
}

int sgn = 1;

if (c == '-') {
sgn = -1;
c = read();
}

int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));

return res * sgn;
}

public long nextLong() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;

do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}

public double nextDouble() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E') {
return res * Math.pow(10, nextInt());
}
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E') {
return res * Math.pow(10, nextInt());
}
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}

public String readString() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));

return res.toString();
}

public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return c == ' ' || c == 'n' || c == 'r' || c == 't' || c == -1;
}

public String next() {
return readString();
}

public interface SpaceCharFilter {

public boolean isSpaceChar(int ch);
}
}
}
coding problems solutions

Post navigation

Previous post
Next post

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes