Question: CreateLongestSpike Create the longest sequence of first increasing ( strictly ) then decreasing ( strictly ) integers using numbers from a given array. Return only
CreateLongestSpike
Create the longest sequence of first increasing strictly then decreasing strictly integers using numbers from a given array. Return only its length.
We will call a sequence of integers a spikeif they first increase strictly and then decrease also strictly, including the last element of the increasing part For example is a spike, but and are not. Note that the increasing and decreasing parts always intersect, eg: for spike sequence is an increasing part and sequence is a decreasing part, and for spike sequence is both an increasing and a decreasing part.
Your are given an array A of N integers. Your task is to calculate the length of the longest possible spike, which can be created from numbers from array A Note that you are NOT supposed to find the longest spike as a subsequence of A but rather choose some numbers from A and reorder them to create the longest spike.
Write a function:
int solutionint A int N;
which, given an array A of integers of length N returns the length of the longest spike which can be created from the numbers from A
Examples:
Given A your function should return because is already a spike.
Given A your function should return because we can create the following spike of length :
Given A your function should return because we can create the following spike of length : and we cannot create any longer spike. Note that increasing and decreasing parts should be strictly increasingdecreasing and they always intersect.
Write an efficientalgorithm 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
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
