[Question]
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 = {“Big Corp”, “Big
Corp”, ”Acme”, “Big Corp”, “Zork” ,”Zork” ,”Abe”, “Big Corp”, “Acme”, “Big
Corp” ,”Big Corp”, “Zork”, “Big Corp”, “Zork”, “Zork”, “Big Corp”,” Acme”, ”Big
Corp”, “Acme”, “Big Corp”, “Acme”, “Little Corp”, “Nadir Corp”}
“Big Corp had 10 Trades out of 23,
which is 43.48% of the Total Trades.”
“Both Acme and Zork had 5 trades,
which is 21.74% of the Total Trades.”
“The Little Corp, Nadir Corp and
Abe had 1 Trade Each, which is 4.35%…”
“So the Answer is [“”Acme””, “”
Big Corp ,””Zork””] (In Alphabetical Order) Because only These Three Companies
Placed at least 5% of the Trades.
Function Description
Complete the function most ACTIVE
in the Editor Below.
most ACTIVE has the following
parameter:
String customers[n]: An Array
Customers Names
(Actual Questions Says String
Array, But Signatures is List of Strings)
Returns String[] : An
Alphabetically Ascending Array
Constraints
• 1 < n < 10⁵
• 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.
[Sample Input]
“The First Line contains an
integer, n, The Number of Elements in customers.”
“Each Line 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 most ACTIVE
customers[] size n = 20
customers[] = [As Provided Above]
[Sample Output]
Alpha
Beta
Omega
[Explanation of Solution]
In this problem 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.
Github Repo for Code:
[Solution (Java Code)]
package codeathon;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class Codeathon02_vijay{
public static List<String> mostActive(String[] customers) {
List<String> result = new ArrayList<>();
int totalTrades = customers.length;
int threshold = (int) (0.05 * totalTrades);
// Sort the array of customers
Arrays.sort(customers);
int currentCount = 1;
for (int i = 1; i < totalTrades; i++) {
if (customers[i].equals(customers[i - 1])) {
currentCount++;
} else {
if (currentCount >= threshold) {
result.add(customers[i - 1]);
}
currentCount = 1;
}
}
// Check the last customer
if (currentCount >= threshold) {
result.add(customers[totalTrades - 1]);
}
return result;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number of trades:");
int numTrades = scanner.nextInt();
scanner.nextLine();
if (numTrades <= 0) {
System.out.println("Please enter trades must be greater than zero.");
return; // Exit the program if numTrades is not valid.
}
String[] customers = new String[numTrades];
System.out.println("Enter the names of customers (one on each line):");
for (int i = 0; i < numTrades; i++) {
customers[i] = scanner.nextLine();
}
System.out.println("OUTPUT:");
List<String> activeCustomers = mostActive(customers);
if (activeCustomers.isEmpty()) {
System.out.println("No active customers found.");
}
else {
for (String customer : activeCustomers) {
System.out.println(customer);
}
}
}
}
Thank you,
k.vijay(Intern)
vijay.keradhi@eminds.ai
Enterprise Minds.
No comments:
Post a Comment