Psuedocode and Runtime: (Java) 1. Write pseudocode for a function that calculate
ID: 3832825 • Letter: P
Question
Psuedocode and Runtime: (Java)
1. Write pseudocode for a function that calculates the largest difference between any two numbers in an array of positive integers with a runtime in (n2).
For example, the largest difference between any two numbers in the array storing the values [4, 6, 3, 9, 2, 1, 20] would be 19.
2. Can this function be written with a runtime (n)? If yes, write the pseudocode. If no, why? What would have to be different about the input to do so?
3. Can this function be written with a runtime (1)? If yes, write the pseudocode. If no, why? What would have to be different about the input to do so?
Explanation / Answer
1) Let A be the array.
n = length(A)
max = abs(a[0]-a[1])
for i = 1:n
for j = 1:n
if(abs(a[i]-a[j])>max)
max = abs(a[i]-a[j])
end
end
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.