# SKP's Algorithms and Data Structures #6: Java Problem: Active Traders

### This Article Series Focuses on Algorithms, Data Structures, or Applying them to Problem Solving. In this Article, We Discuss the Solution to [Active Traders] Problem from Hacker Rank.

· Java Zone · Code Snippet
Save
1.97K Views

[Question/Problem Statement is the Property of HackerRank]

Algorithms/Data Structures — [Problem Solving]
An Institutional Broker wants to Review their Book of Customers to see which are Most Active. Given a List of Trades By "Customer Name, Determine which Customers Account for At Least 5% of the Total Number of Trades. Order the List Alphabetically Ascending By Name."

Example
n = 23
"customers = {"Bigcorp", "Bigcorp", "Acme", "Bigcorp", "Zork", "Zork", "Abe", "Bigcorp", "Acme", "Bigcorp", "Bigcorp", "Zork", "Bigcorp", "Zork", "Zork", "Bigcorp", "Acme", "Bigcorp", "Acme", "Bigcorp", "Acme", "Littlecorp", "Nadircorp"}."

"So the Answer is ["Acme","Bigcorp","Zork"] (In Alphabetical Order) Because only These Three Companies Placed at least 5% of the Trades.

Function Description

Complete the Function mostActive in the Editor Below.

mostActive
has the following parameter:
String customers[n]: An Array Customer Names
(Actual Question Says String Array, But Signature is List of Strings)

Returns String[]: An Alphabetically Ascending Array

Constraints

• 1 < n < 10^5
• 1 < Length of customers[] < 20
• The First Character of customers[i] is a Capital English letter.
• All Characters of customers[i] except for the First One are Lowercase.
• Guaranteed that At least One Customer makes at least 5% of Trades.

Input Format

"The First Line contains an integer, n, The Number of Elements in customers."
"Each Line i of the n Subsequent Lines (where 0 s i< n) contains a string, customers[i]."

Sample Case 0 Input For Custom Testing
20
Omega Alpha Omega Alpha Omega Alpha Omega Alpha Omega Alpha Omega Alpha Omega Alpha Omega Alpha Omega Alpha Omega Beta

Function mostActive
customers[] size n = 20
customers[] = [As Provided Above]

Sample Output

Alpha
Beta
Omega

Explanation

"Alpha made 10 Trades out of 20 (50% of the Total), Omega made 9 Trades (45% of the Total). and Beta made 1 Trade (5% of the Total). All of them have met the 5% Threshold, so all the Strings are Returned in an Alphabetically Ordered Array."

[Explanation of the Solution]
This is Good Practice for the Brain for Problem Solving — Involves Simple Arithmetic and Mathematical Application. Ideally, A Programmer would want to Optimize the Solution in Space and Time (Which I Did Not :-)

[Source Code, Sumith Puri (c) 2021 — Free to Use and Distribute]
``````import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.stream.IntStream;

/*
* HackerRank Problem Solving - Ain't a Horse that Can't be Rode
* Sumith Kumar Puri (c) 2021 - ~ Bengaluru, Karnataka, India ~
*
*/

public static List<String> mostActive(List<String> customers) {

// How About Arrays or Custom LinkedList for a 'Very Fast' Traversal?
Map<String, Integer> customerMap = new TreeMap<String, Integer>();
List<String> solutionStr = new ArrayList<String>();
int customerMapSize = customers.size();

for (int i = 0; i < customerMapSize; i++) {

String customerKey = customers.get(i);

if (customerMap.containsKey(customerKey)) {

Integer customerCount = customerMap.get(customerKey);
customerMap.put(customerKey, ++customerCount);
} else {
customerMap.put(customerKey, 1);
}
}

Set<String> customerMapKeys = customerMap.keySet();
for (String customerKey : customerMapKeys) {

Integer customerCount = customerMap.get(customerKey);
double currentCustomerPercent = (double) (customerCount) / (double) customerMapSize;

if (currentCustomerPercent * 100 >= 5.0) {

}
}

return solutionStr;
}
}

public static final String OUTPUT_PATH = "PROVIDE_ABSOLUTE_INPUT_FILE_NAME";

public static void main(String[] args) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv(OUTPUT_PATH)));

List<String> customers = IntStream.range(0, customersCount).mapToObj(i -> {
try {
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}).collect(toList());

bufferedWriter.write(result.stream().collect(joining("\n")) + "\n");

bufferedWriter.close();
}
}
``````

Happy Problem Solving using Java! 6

Topics:
hacker rank, core java, algorithms, data structures, problem solving, mathematical programming, complexity metrics, competitive, iq, java program

Published at DZone with permission of Sumith Puri, DZone MVB.

Opinions expressed by DZone contributors are their own.