Analyze the following C++ function and determine what problem it solves: Problem Statement:...
Netradyne technical mcq question, verified with a worked answer. Free to practise - no sign-up.
Analyze the following C++ function and determine what problem it solves:
int solve(vector<int> &v, int m){
int n=v.size();
vector<int> preMin(n);
preMin[0]=v[0];
vector<int> sufMin(n);
sufMin[n-1]=v[n-1];
for(int i=1;i<n;i++){
preMin[i]=min(preMin[i-1], v[i]);
}
for(int i=n-2;i>=0;i--){
sufMin[i]=min(sufMin[i+1], v[i]);
}
int ans =INT_MAX;
int i=0;
int j=m-1;
while(i<=n-m){
ans = min(ans,preMin[i]*sufMin[j]);
i++;
j++;
}
return ans;
}
Problem Statement:
Given a vector v of integers and an integer m, the function constructs two auxiliary arrays:
preMin: prefix minimum array wherepreMin[i]stores the minimum value from index 0 to isufMin: suffix minimum array wheresufMin[i]stores the minimum value from index i to n-1
The function then slides a window of size m across the array and for each window position, computes the product of:
- The minimum element in all elements before the window
- The minimum element in all elements after the window
What does this function return?
Show answer & explanation
Answer: A. A) The maximum product of minimum values outside a sliding window of size m
Accurate technical and quantitative evaluation based on foundational principles.
Step-by-step Derivation:
Step 1: Formulate problem conditions.
Step 2: Apply logical deduction.
Step 3: Conclude correct option.