Hi,
Given question doesnt put any restrictions over the elements order
which makes it even simpler,
here is the pseduo code for the algorithm
def fun(A,T)
sum=0;
end=0
i=0
max=0
for i<A.size
if(sum>max && sum<=T)
max=sum
if(sum+A[end]<T && end<A.size)
sum=sum+A[end]
end++
else
sum=sum-i
i++
end.
We are basically maintaining 2 pointers, and moving them together
based on how the sum is varying, i,e
like in this example for input [3,7,10]
first sum becomes 3,
then adding 10 becomes it becomes 13 and max sum will be 13
then adding 17 takes it to else(since >T), where max sum will be
after subtracting from end, therefore 3 will be subtracted, and
then 17 will be returned
Since we are only looping through the list one time, the order is
O(n) only
Thumbs up if this was helpful, otherwise let me know in comments
Problem 3: (5 2 points) Design an algorithm that takes an array of positive integers A...
Design and implement a Θ(n) algorithm that will simultaneously find the largest and second-largest elements (integers) in an array. Note that the second largest element should be distinctly smaller than the largest element. You should also adhere to the following restrictions. (1) You should traverse through the array ONLY ONCE. (2) The array could have both positive and negative elements. So, you should NOT initialize any temporary variables to very small negative values as part of your algorithm. (3) You...
9. (5 points) Please describe an algorithm that takes as input a list of n integers and finds the number of negative integers in the list. 10. (5 points) Please devise an algorithm that finds all modes. (Recall that a list of integers is nondecreasing if each term of the list is at least as large as the preceding term.) 11. (5 points) Please find the least integer n such that f() is 0(3") for each of these functions f()...
Given as input an array A of n positive integers and another positive integer x, describe an O(nlogn)-time algorithm that determines whether or not there exist two elements Ai and AONn the array A such that is exactly x.
You are given an array A of integers in sorted order. However, you do not know the length n of the array. Assume that in our programming language arrays are implemented in such a way that you receive an out-of-bounds error message whenever you wish to access an element A[i] with i>n. For simplicity we assume that the error message simply returns the value INT_MAX and that every value in the array is smaller than INT_MAX. (a) Design an algorithm...
Consider the following problem: Input: a list of n-1 integers and these integers are in the range of 1 to n. There are no duplicates in list. One of the integers from 1 to n is missing in the list. Output: find the missing integer Let the input array be [2, 4, 1, 6, 3, 7, 8]. Elements in this list are in the range of 1 to 8. There are no duplicates, and 5 is missing. Your algorithm needs...
3. Chapter 7. Write an algorithm that accepts an array of positive integers and two more positive integers. The algorithm should return the array with all the numbers that are between the two numbers. For example, if the given array is 12 16 23 19 33 14 3 15 and the integers 13 and 20 are entered, then the algorithm will return 16 19 14 15 How many comparisons does your algorithm perform? Explain your answer. Zero points if there...
9. [10 points) Consider the following algorithm: procedure Algorithm(n: positive integer; ddd: distinet integers) for k:=1 to n-1 for 1-1 to n-k print(k, I, di,da...-1,dn) if ds dti then interchange dy and d (a) Assume that this algorithm receives as input the integer n 6 and the input sequence 하하하하하하, Miss ^-ruteae rehen i12|3141516 Fill out the table below: ds ds (b) Assume that the algorithm receives the same input values as in part a). Once the algorithm finishes, what...
Design an algorithm for the following description. Solution can be done in pseudo-code or steps of the algorithm. Describe and analyze an algorithm that takes an unsorted array A of n integers (in an unbounded range) and an integer k, and divides A into k equal-sized groups, such that the integers in the first group are lower than the integers in the second group, and the integers in the second group are lower than the integers in the third group,...
Introduction to the Design and Analysis of Algorithms Note: Present Algorithms in Pseudocode Give an algorithm to print all the common elements in two sorted arrays of integers, A and B of sizes m and n, respectively. Assume that the numbers in each of the arrays are distinct. Input: Sorted arrays A[m] and B [n]. Output: Common elements. ExA:3.5 6 9 12 17 23; B:2581011 12 15 16 17 19 22 25; Output: 5 12 17
8. [10 points) Consider the following algorithm procedure Algorithm(: integer, n: positive integer; 81,...a s integers with vhilei<r print (l, r, mı, arn, 》 if z > am then 1:= m + 1 if za then anstwer-1 return answer 18 and the (a) Assume that this algorithm receives as input the numbersz-32 and corresponding sequence of integers 2 | 3 1 1 4151617| 8| 9 | 10 İ 11 İ 12 | 13 | 14|15 | 16 | 17 |...