Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

RubbleRover You have built a small robot named RubbleRover, which collects trash and has a radar. You want to use it for recycling. Here is

RubbleRover
You have built a small robot named RubbleRover, which collects trash and has a radar. You want to use it for recycling. Here is what you need to know.
* There are garbage blocks lined up In a row, with equal distance between them.
* RubbleRover can tell if there is a garbage block at a certain distance from it. If it finds a block, it moves to that block and collects it.
* If RubbleRover cannot tell if the garbage is on its left or right, it will not go there to save energy and work efficiently.
* RubbleRover will keep checking further if it is unsure about the garbage's location.
You are given the following:
The first line tells where RubbleRover starts, P.
The second line tells how many positions have
Garbage.
The third line has an array of 0s and 15, where 1 means there is garbage and 0 means there is no garbage.
Your job is to figure out how many garbage blocks RubbleRover will recycle while being most efficient.
Input
3
5
10111
Output
3
Explanation: RubbleRover is located in the 3rd position.
As the third line of the Input above shows, there is a garbage block at the 3rd position, RubbleRover is sure about it. So, RubbleRover will recycle it.
RubbleRover knows that there is one garbage block at a distance 1 from it, but it cannot determine whether it should go to the left or right. So it will not check these positions.
RubbleRover starts checking further and sees that there are two garbage blocks at a distance of 2 from It. Since one position can have only one garbage block, RubbleRover is sure that one is to its left and the other is to the right. So it will recycle both of these garbage blacks.
The answer is 1+2=3. RubbleRover will recycle 3
for input
Input
4
4
0110
Output
2
Explanation: RubbleRover is located in the 4th position.
As the third line of the input above shows, there is no garbage block in the 4th position.
There is one garbage block at a distance of 1 from RubbleRover, and RubbleRover is sure that it is located to its left (since there are only four garbage blocks). RubbleRover will recycle that garbage block.
The same is true for a distance of 2 from RubbleRover,
There is no garbage block at distance 3 from RubbleRover.
The answeris 1+1=2 RubbleRover will recycle 2
Barbage blocks in this case
write a program in java
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions