-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathProblem_40.java
More file actions
45 lines (38 loc) · 1.75 KB
/
Problem_40.java
File metadata and controls
45 lines (38 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package strings;
// Function to find Number of customers who could not get a computer
public class Problem_40 {
public static int numCustomersNotGettingComputer(int totalCustomers, int numComputers, char[] customerPrefs) {
if (totalCustomers <= 0 || numComputers < 0 || customerPrefs == null
|| customerPrefs.length != totalCustomers) {
return -1; // Invalid input
}
// Count the number of customers needing laptops and desktops
int laptopsNeeded = 0;
int desktopsNeeded = 0;
for (char pref : customerPrefs) {
if (pref == 'L') {
laptopsNeeded++;
} else if (pref == 'D') {
desktopsNeeded++;
} else {
// Invalid preference character, ignore
}
}
// Check if there are enough computers to satisfy all preferences
if (laptopsNeeded <= numComputers && desktopsNeeded <= numComputers) {
return 0; // All customers get a computer
}
// Determine the type of computer in short supply
int computersShort = Math.min(laptopsNeeded, desktopsNeeded);
int customersShort = Math.max(laptopsNeeded, desktopsNeeded);
// Calculate the number of customers who cannot get their preferred computer
return customersShort - computersShort;
}
public static void main(String[] args) {
int totalCustomers = 8;
int numComputers = 5;
char[] customerPrefs = { 'L', 'L', 'D', 'D', 'L', 'D', 'L', 'D' };
int numNotGettingComputer = numCustomersNotGettingComputer(totalCustomers, numComputers, customerPrefs);
System.out.println(numNotGettingComputer + " customers could not get their preferred computer.");
}
}