145 Define a function bool canMakeInRange(int nums[], int length, int min,int ma
ID: 3764128 • Letter: 1
Question
145
Define a function
bool canMakeInRange(int nums[], int length, int min,int max);
that returns true if the values in some subset of nums add up to a total in range [min, max], and false otherwise. If min <= 0 and max >= 0 then the function should return true . If min > 0 or max < 0 and length == 0 then the function should return false .
Your function is tested with the array {-4, 5, -7, 4, 7} and various values of min and max.
Hint: consider two cases: either the last element of nums in used or not. If you don't use the last element then min and max are unchanged. If you do use the last element then min and max are both decreased by that amount for your recursive call.
Explanation / Answer
Hi,
Below is the solution to your question:
bool canMakeInRange(int nums[], int length, int min, int max)
{
int i;
for( i = 0; i < length; i++)
{
if(min <= nums[i] && max >= nums[i])
{
return true;
}
if(min > nums[i] || max < nums[i] && length == 0)
{
return false;
}
}
}
Hope that helps...HAPPY ANSWERING!!!!!!!!!!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.