design/algorithm analysis - please answer as many as you can. Thank you! Questio
ID: 3590830 • Letter: D
Question
design/algorithm analysis - please answer as many as you can. Thank you!
Explanation / Answer
Answer 6)
Reason : worst case would be traversing whole linked list and inserting in the end where traversing n elements will take (n) and insertion in (1).
Reason : Insertion and deletion in queue takes constant time using front and rear pointera.
Reason : Big – O gives upperbound i.e. satisfies less than or equal to criteria whereas provides tighter bound. Thus is contained in Big-O.
Reason : Smallest element in a min heap would be A[1] and second smallest would be A[2]
Reason : In each iteration algorithm selects random position as pivot and the problem is broken using this pivot into sub problems. But it is not necessary that sub problems are of size n/2. These subproblems could be of any size based on the position of the pivot element.
Reason : Because relative ordering of equal elements is preserved in the final list.
Answer 7)
void subArraySum(int arr[], int n)
{
unordered_map<int, int> map;
int curr_sum = 0;
for (int i = 0; i < n; i++)
{
curr_sum = curr_sum + arr[i];
if (curr_sum == 0)
{
cout << "Sum found between indexes "
<< 0 << " to " << i << endl;
return;
}
if (map.find(curr_sum) != map.end())
{
cout << "Sum found between indexes "<< map[curr_sum] + 1 << " to " << i << endl;
return;
}
map[curr_sum] = i;
}
cout << "No subarray with sum 0 exists";
}
If this answers your question please give thumbs up. Happy Chegging !!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.