A covert agent has some crucial information stored in the form of an array of integers. The array contains sensitive information and it must not be revealed to anyone. However, there are few things about the array which are known.
An array is said to be analogous to the secret array if all of the following conditions are true:
• The length of the array is equal to the length of the secret array.
• Each integer in the array lies in the interval [lowerBound, upperBound].
• The difference between each pair of consecutive integers of the array must be equal to the difference between the respective pair of consecutive integers in the secret array. In other words, let the secret array be [s[0], s[1],...s[n- 1]]and let the analogous array be [a[0], a[1],...a[n- 1]], then (a[i-1] - a[i]) must be equal to (s[i-1] - s[i]) for each / from 1 to n-1.
Given the value of the integers lowerBound and upperBound, inclusive, and the array of differences between each pair of consecutive integers of the secret array, find the number of arrays that are analogous to the secret array. If there is no array analogous to the secret array, return 0.
consecutive Difference = [-2, -1, -2, 5]
lowerBound=3
upperBound=10
The logic to create an analogous array starting from the lower bound is:
Start with a value of 3.
Subtract consecutive Distances[0], 3 - (-2) = 5
Subtract consecutive Distances[1], 5 - (-1) = 6
Subtract consecutive Distances[2], 6 - (-2) = 8
Subtract consecutiveDistances[3], 8 - 5 = 3
Note that none of the values is out of bounds. All
possible analogous arrays are:
[3, 5, 6, 8, 3]
[4, 6, 7, 9, 4]
[5, 7, 8, 10, 5]
The answer is 3.
Complete the function countAnalogousArrays in
the editor below.
int countAnalogousArrays (vector<int> consecutiveDifference, int lowerBound, int upperBound) {
}A report containing a stock's prices for the past n days is provided to a data analyst in the array stockPrice[ ].
The analyst is required to choose a subsequence of stock prices, called chosenDays[] in this explanation. The chosen subsequence of stock prices is balanced if the following condition holds :
stockPrice[chosenDays[i]] - stockPrice[chosenDays[i- 1]] = chosenDays[i] - chosenDays[i - 1], for i > 0.
The score of the chosen subsequence is the sum of stock prices on the chosen days. Find the
maximum possible score that can be obtained by choosing an optimally balanced subsequence.
Note:
• A subsequence is an ordered subset of an array's elements in the same sequential ordering as in the original array.
• A subsequence of length 1 is always balanced.
Consider n = 5, stockPrice = [1, 5, 3, 7, 8].
Test Results
Then, the subsequence 5, 7, 87 can be chosen.Corresponding chosen days are [1, 3, 4] (considering O-based indexing). Now,
• stockPrice[3] - stockPrice[1] = 7-5=2=3-1
• stockPrice[4] - stockPrice[3] = 8-7=1=4-3
Thus, the subsequence is balanced. Score = 5 +7+8 = 20, which is maximum possible. So, the answer is 20.
complete the following function :
long getMaximumScore(vector<int>stockPrice){
}Feel free to share your approaches , and please** UPVOTE** if you loved the post !!!