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.
1#include <iostream>
2#include <vector>
3#include <string>
4
5struct Employee {
6 int employee_id;
7 std::string name;
8 int salary;
9};
10
11std::vector<std::pair<int, int>> calculate_bonus(const std::vector<Employee> &employees) {
12 std::vector<std::pair<int, int>> result;
13 for (const auto &employee : employees) {
14 int bonus = 0;
15 if (employee.employee_id % 2 == 1 && employee.name[0] != 'M') {
16 bonus = employee.salary;
17 }
18 result.emplace_back(employee.employee_id, bonus);
19 }
20 return result;
21}
22
23int main() {
24 std::vector<Employee> employees = {
25 {2, "Meir", 3000},
26 {3, "Michael", 3800},
27 {7, "Addilyn", 7400},
28 {8, "Juan", 6100},
29 {9, "Kannon", 7700}
30 };
31
32 auto results = calculate_bonus(employees);
33
34 std::cout << "+-------------+-------+\n";
35 std::cout << "| employee_id | bonus |\n";
36 std::cout << "+-------------+-------+\n";
37 for (const auto &[id, bonus] : results) {
38 std::cout << "| " << id << " | " << bonus << " |\n";
39 }
40 std::cout << "+-------------+-------+\n";
41 return 0;
42}
This C++ solution creates a vector of Employees and checks for odd IDs and names not starting with 'M' to assign bonuses. It uses a vector to store pairs of (employee_id, bonus) for output.
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 constructor(employeeId, name
In this JavaScript solution, map is used similarly to apply the transformation across the employee list. The solution calculates the bonus conditionally and stores it in the result array.