Answered step by step
Verified Expert Solution
Question
1 Approved Answer
code javascript: Find the number of castles that can be built. Task description Charlemagne, the King of Frankia, is considering building some castles on the
code javascript: Find the number of castles that can be built. Task description Charlemagne, the King of Frankia, is considering building some castles on the border with Servia. The border is divided into N segments. The King knows the height of the terrain in each segment of the border. The height of each segment of terrain is stored in array A with AP denoting the height of the Pth segment of the border. The King has decided to build a castle on top of every hill and in the bottom of every valley. Let PQ denote a group of consecutive segments from P to Q inclusive such that P Q N Segments PQ form a hill or a valley if all the following conditions are satisfied: The terrain height of each segment from P to Q is the same AP AP AQ; If P then AP APfor a hill or AP APfor a valley; If Q N then AQ AQfor a hill or AQ AQfor a valley; That is a hill is higher than its surroundings and a valley is lower than its surroundings. Note that if the surroundings on either side of the hill or valley don't exist ie at the edges of the area under consideration, where P or Q N then the condition is considered satisfied for that side of the hillvalley The king is wondering how many castles is he going to build. Can you help him? For example, consider the following array A There are two hills: and There are also two valleys: and There are no other suitable places for castles. Write a function: function solutionA; that, given an array A consisting of N integers, as explained above, returns the total number of hills and valleys. For example, given array A as described above, the function should return Given array A describing segments with a terrain height below segment forms both a hill and a valley, and only one castle can be built, so the function should return Write an efficient algorithm for the following assumptions: N is an integer within the range ; each element of array A is an integer within the range
code javascript: Find the number of castles that can be built.
Task description
Charlemagne, the King of Frankia, is considering building some castles on the border with Servia. The border is divided into N segments. The King knows the height of the terrain in each segment of the border. The height of each segment of terrain is stored in array A with AP denoting the height of the Pth segment of the border. The King has decided to build a castle on top of every hill and in the bottom of every valley.
Let PQ denote a group of consecutive segments from P to Q inclusive such that P Q N Segments PQ form a hill or a valley if all the following conditions are satisfied:
The terrain height of each segment from P to Q is the same AP AP AQ;
If P then AP APfor a hill or AP APfor a valley;
If Q N then AQ AQfor a hill or AQ AQfor a valley;
That is a hill is higher than its surroundings and a valley is lower than its surroundings. Note that if the surroundings on either side of the hill or valley don't exist ie at the edges of the area under consideration, where P or Q N then the condition is considered satisfied for that side of the hillvalley
The king is wondering how many castles is he going to build. Can you help him?
For example, consider the following array A
There are two hills: and There are also two valleys: and There are no other suitable places for castles.
Write a function:
function solutionA;
that, given an array A consisting of N integers, as explained above, returns the total number of hills and valleys.
For example, given array A as described above, the function should return
Given array A describing segments with a terrain height below segment forms both a hill and a valley, and only one castle can be built, so the function should return
Write an efficient algorithm for the following assumptions:
N is an integer within the range ;
each element of array A is an integer within the range
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started