Sponsored
Sponsored
This approach involves iterating through each employee, checking if their employee_id is odd and their name does not start with 'M'. If both conditions are met, the bonus is set to their full salary; otherwise, it is set to 0. Loop through each employee and apply these checks systematically.
Time Complexity: O(n), where n is the number of employees.
Space Complexity: O(n), for storing the results.
1import java.util.*;
2
3class Employee {
4 int employee_id;
5 String name;
6 int salary;
7
8 Employee(int employee_id, String name, int salary) {
9 this.employee_id = employee_id;
10 this.name = name;
11 this.salary = salary;
12 }
13}
14
15class Result {
16 int employee_id;
17 int bonus;
18
19 Result(int employee_id, int bonus) {
20 this.employee_id = employee_id;
21 this.bonus = bonus;
22 }
23}
24
25public class CalculateBonus {
26
27 public static List<Result> calculateBonus(List<Employee> employees) {
28 List<Result> result = new ArrayList<>();
29 for (Employee employee : employees) {
30 int bonus = 0;
31 if (employee.employee_id % 2 != 0 && !employee.name.startsWith("M")) {
32 bonus = employee.salary;
33 }
34 result.add(new Result(employee.employee_id, bonus));
35 }
36 return result;
37 }
38
39 public static void main(String[] args) {
40 List<Employee> employees = Arrays.asList(
41 new Employee(2, "Meir", 3000),
42 new Employee(3, "Michael", 3800),
43 new Employee(7, "Addilyn", 7400),
44 new Employee(8, "Juan", 6100),
45 new Employee(9, "Kannon", 7700)
46 );
47
48 List<Result> results = calculateBonus(employees);
49
50 System.out.println("+-------------+-------+");
51 System.out.println("| employee_id | bonus |");
52 System.out.println("+-------------+-------+");
53 for (Result res : results) {
54 System.out.printf("| %11d | %5d |\n", res.employee_id, res.bonus);
55 }
56 System.out.println("+-------------+-------+");
57 }
58}
This Java solution defines Employee and Result classes. It checks each employee's conditions in a loop and stores the results in a list. The bonus logic is applied per the rules and printed subsequently.
This approach utilizes functional programming paradigms such as map/filter. It maps through each row of data, filters the rows based on the bonus eligibility criteria, and transforms them with the mapped values.
Time Complexity: O(n), where n is the number of employees.
Space Complexity: O(n), for storing the results.
1class Employee:
2 def __init__(self, employee_id
This Python solution uses lambda functions and 'map' to apply the bonus logic across all employees in a functional style approach without using an explicit loop. It's concise and leverages built-in Python capabilities.