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...
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...
(JAVA) Given an array of unique positive integers, write a function findSums that takes the array input and: 1. Creates a hashtable called “hashT” and inserts all the elements of the input array in the hashtable. 2. Finds pairs of elements in the hashtable whose sum is another element (sum) in the hashtable and print the pairs in the console. 3. Returns another hashtable names “sums” of those sum elements. For example: Input: [1,5,4,6,7,9] Output: [6,5,7,9] Explanation: 6 = 1...
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...
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,...
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...