Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

code javascript: Given the distinct positions of holes, find the shortest length of two identical boards that can cover all the holes. Task description There

code javascript:
Given the distinct positions of holes, find the shortest length of two identical boards that can cover all the holes.
Task description
There are N holes arranged in a row in the top of an old table. We want to fix the table by covering the holes with two boards. For technical reasons, the boards need to be of the same length.
The position of the K-th hole is A[K]. What is the shortest length of the boards required to cover all the holes? The length of the boards has to be a positive integer. A board of length L, set at position X, covers all the holes located between positions X and X+L (inclusive). The position of every hole is unique.
Write a function:
function solution(A);
which, given an array A of integers of length N, representing the positions of the holes in the table, returns the shortest board length required to cover all the holes.
Examples:
1. Given A =[11,20,15], your function should return 4. The first board would cover the holes in positions 11 and 15, and the second board the hole at position 20.
2. Given A =[15,20,9,11], your function should return 5. The first board covers the holes at positions 9 and 11, and the second one the holes in positions 15 and 20.
3. Given A =[0,44,32,30,42,18,34,16,35], your function should return 18. The first board would cover the holes in positions between 0 and 18, and the second the positions between 30 and 44.
4. Given A =[9], your function should return 1.
Write an efficient algorithm for the following assumptions:
N is an integer within the range [1..100,000];
each element of array A is an integer within the range [0..1,000,000,000];
the elements of A are all distinct.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

Big Data Fundamentals Concepts, Drivers & Techniques

Authors: Thomas Erl, Wajid Khattak, Paul Buhler

1st Edition

0134291204, 9780134291208

More Books

Students also viewed these Databases questions

Question

What is the purpose of the foreign tax credit?

Answered: 1 week ago

Question

2. Should a disciplinary system be established at Carter Cleaning?

Answered: 1 week ago