






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







k. All other operations are O(1) Total Running Time = O((n+k)log(n+k))
algorithm that is Θ(n^2 ) (you can do much better than that): Array A[n] of size n, Vector v 1.) int first, temp = 0; 2.)----------------------------for i from 0 to n- 1 O(n) a.) max_ending_here = max_ending_here + A[i] b.) if (max_ending_here < 0) i.) max-ending_here = 0 ii.)temp = i + 1 c.)if (max_so_far < max_ending_here) i.) max_so_far = max_ending_here ii.)last = i; iii.)first = temp; 2.) Array B[m] where m = (last-first)+ 3.)-------------------------------For i from 0 to m- 1 O(m) B[i] = A[first + i]; Time Complexity: O(n+m) since we go through the entire array
8.. Given an array A of numbers, for every index i, find the nearest index j such that A[j] > A[i]. If none exist, report −1. Output these indices sorted by the corresponding i.:
Time Complexity: O(n)
// We reach here when x > y and z < y, i.e., z is smallest else k++; } }
if (arr[mid] == target) return arr[mid]; /* If target is less than array element, then search in left / if (target < arr[mid]) { // If target is greater than previous // to mid, return closest of two if (mid > 0 && target > arr[mid - 1]) return getClosest(arr[mid - 1], arr[mid], target); / Repeat for left half */ j = mid; } // If target is greater than mid else { if (mid < n - 1 && target < arr[mid
10 / 12 temp; // Get to the next node in the list head = nex; } return false; }
11 / 12 head = nex; } return head; }