[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 mostActive in the Editor Below.
mostActive 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^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 atleast 5% of Trades.
[Sample Input]
"The First Line contains an integer, n, The Number of Elements in customers."
"Each Line iof 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 of Output
“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 Solution]
The program first counts the number of trades made by each customer and stores this information in a map. Then, it calculates the threshold for the 5% requirement based on the total number of trades. Next, it identifies customers whose trade count surpasses this threshold, adding them to a list of active customers. Finally, it sorts this list alphabetically and returns it as the result.
Github Repo for Code: https://tinyurl.com/ywr7fknv
[Solution (Java Code)]
/*
* Core Java Problem Solving - Codeathon.
* A.R. Kishore Kumar ~ (c) 2023 ~ -- ~ Tirupati, Andhra Pradesh, India ~
*
*/
package codeathon;
import java.util.Scanner;
import java.util.Collections;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
public class Codeathon02_Kishore
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(); // Number of Elements in customers
List<String> customers = new ArrayList<>();
for (int i = 0; i < n; i++) { // Number of times trade by Customers
customers.add( sc.next() );
}
List<String> result = ActiveTraders.mostActive(customers);
// Print the result in alphabetical order
Collections.sort(result);
for (String customer : result) {
System.out.println(customer);
}
}
}
class ActiveTraders
{
public static List<String> mostActive(List<String> customers)
{
int n = customers.size();
Map<String, Integer> customerCounts = new HashMap<>();
// Count the occurrences of each customer
for (int i = 0; i < customers.size(); i++)
{
String customer = customers.get(i);
if (customerCounts.containsKey(customer)) {
int count = customerCounts.get(customer);
customerCounts.put(customer, count + 1);
} else {
customerCounts.put(customer, 1);
}
}
// Calculate the threshold for 5% of total trades
int threshold = (int) Math.ceil(n * 0.05);
// Find customers with trades >= threshold
List<String> activeCustomers = new ArrayList<>();
for (String customer : customerCounts.keySet()) {
if (customerCounts.get(customer) >= threshold) {
activeCustomers.add(customer);
}
}
return activeCustomers;
}
}
Algorithms/Data Structures - [Problem Solving]
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 mostActive in the Editor Below.
mostActive has the following parameter:
(Actual Questions Says String Array, But Signatures 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 atleast 5% of Trades.
[Sample Input]
"The First Line contains an integer, n, The Number of Elements in customers."
"Each Line iof 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 of Output
“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 Solution]
The program first counts the number of trades made by each customer and stores this information in a map. Then, it calculates the threshold for the 5% requirement based on the total number of trades. Next, it identifies customers whose trade count surpasses this threshold, adding them to a list of active customers. Finally, it sorts this list alphabetically and returns it as the result.
Github Repo for Code: https://tinyurl.com/ywr7fknv
[Solution (Java Code)]
/*
* Core Java Problem Solving - Codeathon.
* A.R. Kishore Kumar ~ (c) 2023 ~ -- ~ Tirupati, Andhra Pradesh, India ~
*
*/
package codeathon;
import java.util.Scanner;
import java.util.Collections;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
public class Codeathon02_Kishore
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(); // Number of Elements in customers
List<String> customers = new ArrayList<>();
for (int i = 0; i < n; i++) { // Number of times trade by Customers
customers.add( sc.next() );
}
List<String> result = ActiveTraders.mostActive(customers);
// Print the result in alphabetical order
Collections.sort(result);
for (String customer : result) {
System.out.println(customer);
}
}
}
class ActiveTraders
{
public static List<String> mostActive(List<String> customers)
{
int n = customers.size();
Map<String, Integer> customerCounts = new HashMap<>();
// Count the occurrences of each customer
for (int i = 0; i < customers.size(); i++)
{
String customer = customers.get(i);
if (customerCounts.containsKey(customer)) {
int count = customerCounts.get(customer);
customerCounts.put(customer, count + 1);
} else {
customerCounts.put(customer, 1);
}
}
// Calculate the threshold for 5% of total trades
int threshold = (int) Math.ceil(n * 0.05);
// Find customers with trades >= threshold
List<String> activeCustomers = new ArrayList<>();
for (String customer : customerCounts.keySet()) {
if (customerCounts.get(customer) >= threshold) {
activeCustomers.add(customer);
}
}
return activeCustomers;
}
}
No comments:
Post a Comment