Question 1 Develop well-documented pseudo code that finds all the elements of a
ID: 3757460 • Letter: Q
Question
Question 1 Develop well-documented pseudo code that finds all the elements of a given array (of any size n) that are multiple of x. The code must display the indices and the values of these elements. For instance, given an 1) array A: (22, 61,-10, 21, 0, 9, 50, 17, 35, 81,-46, 19, 5, 77) with x as 5, your code should find and display something similar to the following (notice that this is just an example. Your solution must not refer to this particular example) The elements of the array A that are multiple of 5 are: Index 2 with value -10 Index 6 with value 50 Index 8 with value 35 Index 12 with value 5 a. Briefly justify the motive(s) behind your design b. What is the Big-O complexity of vour solution? Explain clearly how vou obtained such c. What is the Big-2 complexity of your solution? Explain clearly how you obtained such d. What is the Big-O space complexity of your solution? complexity complexityExplanation / Answer
PSEUDOCODE:
function findMultiplesOfX(arr as Array, x as Integer)
for i = 0 to length(arr) - 1 do
if arr[i] mod x = 0 then
print ("Index " + i + " with value = " + arr[i])
end if
end for
end function
a) In order to find multiples of x, we need to iterate through all the elements of arr and find their remainder when divided by x. If the remainder is 0, then it is a multiple of x. In that case, print the index and its value.
b) The algorithm involves iterating though all the elements of arr. Hence, we have included a for loop which runs from i = 0 to n-1 (n is the length of the array). Hence, thetime complexity of this algorithm is n + c [c is a positive constant].
As per Big-O notation,
Clearly, Time complexity = O(n)
c) As per Big- notation,
Clearly, 0 <= c*1 <= n.
Hence, Big- time complexity = (1)
d. This algorithm requires only constant space. Hence, space complexity = O(1).
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.