a) The smallest number of positive non-intersecting intervals that include all non-negative values is 3. Subarrays are:
A[0,4], A[6,6], A[8,12]
b) In the given optimization problem, we have to find the minimum number of positive, non-intersecting intervals which together include all non-negative values
Here is the formal statement:
Let A1, A2, . .Ak be the non-intersecting subarray of A, then minimize k such that sum(Ai)>=0 for all i from 1 to k and No element in A - (A1, A2, . .Ak) is non-negative.
c) The initialisation for the above strategy will be all the subarrays of A containing one positive element i.e. there will be an array corresponding to each non-negative element in A
d) No, the algorithm may fail in some cases. Consider the following example:
A = [ 2, -4, 3, -7, 5, -9, 7]
the optimal solution for the given array is 2, A[0,2], A[4,6]
Let us work as per the greedy strategy:
Initially, we have
A[0,0], A[2,2], A[4,4] and A[6,6]
Now, we can combine any of the following as per strategy:
1) A[0,0] and A[2,2] and resulting subarray will be A[0,2]
2) A[2,2] and A[4,4] and resulting subarray will be A[2,4]
3) A[4,4] and A[6,6] and resulting subarray will be A[4,6]
No, if we choose from 1 and 3, the strategy will lead to a correct answer but if we choose 2, there is no way back and strategy will not give an optimal solution.
QUESTION 3 (10 Marks) Suppose you are given an array A[0..n 1 of integers, each of...
Suppose you have a sorted array of positive and negative integers and would like to determine if there exist some value of x such that both x and -x are in the array. Consider the following three algorithms: Algorithm #1: For each element in the array, do a sequential search to see if its negative is also in the array. Algorithm #2:For each element in the array, do a binary search to see if its negative is also in the...
7. (10) Given an array of integers A[1..n], such that, for all i, 1 <i< n, we have |Ali]- Ali+1]| < 1. Let A[1] = and Alny such that r < y. Using the divide-and-conquer technique, describe in English algorithm to find j such that Alj] z for a given value z, xz < y. Show that your algorithm's running time is o(n) and that it is correct o(n) search an 2 8. (10) Solve the recurrence in asymptotically tight...
Sorting Sort the following array using the quick sort algorithm: (4 Marks) a. 12 26 8 9 7 0 4 Pivot selection is defined to be the first element of each sub-list. Show the array before and after each quicksort round (when the array is partitioned after placing the pivot at its correct position). Also, clearly highlight the pivot in each partition b. Consider an unsorted array of integers of size n. Write a Java program to arrange the array...
6. Consider the following basic problem. You're given an array A consisting of n integers A[1], A[2], , Aln]. You'd like to output a two-dimensional n-by-n array B in which B[i, j] (for i <j) contains the sum of array entries Ali] through Aj]-that is, the sum A[i] Ai 1]+ .. +Alj]. (The value of array entry B[i. Λ is left unspecified whenever i >j, so it doesn't matter what is output for these values.) Here's a simple algorithm to...
QUESTION: //Sort the array arr[] for (int i = 0; i < arr.length - 1; i++) { //outer int index = i; for (int j = i + 1; j < arr.length; i++) if (arr[j] < arr[index]) index = j; int smallerNumber = arr[index]; arr[index] = arr[i]; arr[i] = smallerNumber; }//for i In the array= 16 13 15 14 19 24 9 3, the index of the smallest number at the outer iteration 4 = Answer
Maximum Value But Limited Neighbors You are given an array a[1..n] of positive numbers and an integer k. You have to produce an array b[1..n], such that: (i) For each j, b[j] is 0 or 1, (ii) Array b has adjacent 1s at most k times, and (iii) sum_{j=1 to n} a[j]*b[j] is maximized. For example, given an array [100, 300, 400, 50] and integer k = 1, the array b can be: [0 1 1 0], which maximizes the...
In Python import numpy as np Given the array a = np.array([[1, 2, 3], [10, 20, 30], [100, 200, 300]]), compute and print the sums over all rows (should give [6, 60, 600]) the sums over all columns (the sum of he first column is 111) the maximum of the array the maxima over all rows the mean of the sub-array formed by omitting the first row and column the products over the first two columns (hint: look for an...
50 MARKS TOTAL 3 Marks Question 1 If word c is present in array w of words, the following method is intended to return the index corresponding to the matched value. If c is not present, n (the number of elements in the array) is to be returned. At int match( String w, int n, String c) { for int i 0; i < n; it+) if w[i].equals( c) ) break; return i; Ar ae oakch C91g wntn g However,...
(+30) Provide a python program which will Populate an array(list) of size 25 with integers in the range -100 (negative 100) to +100 inclusive Display the array and its length (use the len function) Display the average of all the integers in the array Display the number of even integers (how many) Display the number of odd integers (how many) Display the number of integers > 0 (how many) Display the number of integers < 0 (how many) Display the...
Given the following array of integers (of capacity 20) with 12 items: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 8 4 10 15 5 7 11 3 9 13 1 6 Index of last element = 11 Does this array represent a min heap? If not, convert it to a min heap (i.e., “heapify” it). Please show all steps.