Amazon OA 2022 New grad
Anonymous User
5489

You can buy some robots with budget B. N robots are available with cost] and battery] in the store. For each valid index i the cost of the robot is cost[i] and the robot has a battery that can hold at most battery[i] units of charge. Initially, all robots are fully charged. You can purchase any subset of these robots, as long as it does not exceed the budget.

The robots can move along a straight line. They will all start at the position 0 on the line and can move as
long as possible on the straight line. It is your responsibility to send a robot as far as possible along the line.

Precise rules of robot movement are given below:

  1. The robot's movement is continuous. They are able to travel arbitrary positive real distances.

  2. Moving 1 unit of distance consumes 1 unit of charge of the battery in the robot. 3. The robots will initially all travel together in the positive direction. One by one, in the order given by
    their numbers, the robots will then turn back. Multiple robots th consecutive numbers may turn back at the same position.

  3. Each robot must return back to position 0.

  4. As a robot turns back, it can donate any amount of charge to the next robot. (l.e., for each valid k robot k may donate some of its charge to robot k+1. Note that after the donation robot k must still have enough charge to get back to position 0.)

  5. The amount of charge a robot carries can never exceed the initial capacity of its battery.
    It is your task to buy the optimal subset of robots that you can afford with budgetBand number them in alway so that the charge donation is optimal to make sure one of the robots reaches maximum distance along the line by following the precise rules above..

Input Format

• B-Budget in currency unit (int)
• N-size of the cost and battery array
• cost[]-cost of robots at the store, cost[i] - cost of robot at index i
• battery] - Charge holding capacity of battery of the robots, battery[i] - charge capacity of the battery of robot at index i

Output Format

• Max distance/displacement that can be reached by one of the robots in double data type
• Notes: Your return value must have an absolute or relative error smaller than or equal to 106.

Constraints:

B will be between 1 and 104, inclusive.
cost[] will have between 1 and 500 elements, inclusive.
battery[] will have exactly the same number of elements as cost.
• Each element of cost will be between 0 and B, inclusive.
• Each element of the battery will be between 0 and 109 inclusive.

Sample Test case

Sample Input 1

100
2 
50 25
11

Sample Output 1

0.666667

Explanation

In this case, You have a budget of 100 currency units. You can buy both robots for 50+25 = 75 currency units. You can number the robots in any order. If they cooperate, one of these robots can reach the position 2/3 = 0.666666667. Here's how an optimal program looks like: Both robots travel together to the position 1/3. At this moment, each of them has 2/3 of a unit of charge in its battery. Robot 1 donates 1/3 of a unit of charge to robot 2 and turns back. Robot 2 now has a full charge in the battery again. It continues to the position 2/3. There it turns back and returns to position 0.

Sample Input 2

25
5
23 5 8 20 15
108 30 42 100 94

Sample Output 2

55.0

Explanation

in this case, You have a budget of 25 currency units. You can buy Robot at index 1 and 3 for 5 + 20 = 25 currency units. If they cooperate, one of these robots can reach the position 55.0. You can number the robot at index 1 as Robot 1 and robot at index 3 as Robot 2. Here's how an optimal program looks like: Both robots travel together to the position 10. At this moment, one will have 20 units of charge and the other will have 90 units in its battery. Robot 1 donates 10 units of charge to robot 2 and turns back. Robot 2 now has full charge again. It continues to the position 55. There it turns back and returns to position 0.

Comments (4)