Question

In: Computer Science

Given an array of n numbers, not necessarily in sorted order, we wish to find: (i)...

Given an array of n numbers, not necessarily in sorted order, we
wish to find: (i) the range (max - min) and (ii) the interquartile range (IQR) of the numbers.
IQR is defined as the difference between the number at the 25% percentile and the number at the
75% percentile. What is the exact number of comparisons needed for computing the range and
why? Using any algorithm from the chapters of the textbook covered so far as a subroutine, give
an efficient algorithm for computing IQR and analyze its time complexity (you do NOT need to
analyze the subroutine if you remember its time complexity). Show all steps.
(b) Given positive integers, x and n, as input, we wish to compute efficiently the number xn. What
is the size of the input and why? How many multiplications are required by the straightforward
algorithm that initializes an output variable to x and does output = output * x repeatedly until
output = xn. Is this a polynomial-time algorithm? Explain precisely and in detail.

Solutions

Expert Solution

ques A)

To find the range of numbers in a given unsorted array we will require "2*(n-1)" comparisons.

This is because after assigning the value of first element of array, the rest of n-1 elements will be compared to both max and min . thus 2 comparisons for each element, computing to a total of 2*(n-1) comparisons.

now for calculating IQR.

size of array= n

element at 25 percentile= arr[floor(n/4)]

element at 75 percentile= arr[floor(3*n/4)]

IQR= arr[floor(n/4)] - arr[floor(3*n/4)]

Ques.B)

x and n are used as input to calculate x^n .

inputs are integers and its size depends on the plaform used.

for a straightforward algorithm:

output=x;

output=output*x

the no of multiplications will be n-1.

yes this is a polynomial time algorithm.

As there are "n-1" multipliacations. so code will be executed n-1 times. so time complexity will be in the order of n-1.

now, accoding to definiton of big O notation

i.e: any f(x+h) >= g(x) will be written as O(x)

f(n-1)>=g(n)  

so time complexity will be O(n). which is a polynomial time complexity.


Related Solutions

Given an array A of n distinct numbers, we say that a pair of numbers i,...
Given an array A of n distinct numbers, we say that a pair of numbers i, j ∈ {0, . . . , n − 1} form an inversion of A if i < j and A[i] > A[j]. Let inv(A) = {(i, j) | i < j and A[i] > A[j]}. Define the Inversion problem as follows: • Input: an array A consisting of distinct numbers. • Output: the number of inversions of A, i.e. |inv(A)|. Answer the following:...
Given an array A of n distinct real numbers, we say that a pair of numbers...
Given an array A of n distinct real numbers, we say that a pair of numbers i, j ∈ {0, . . . , n−1} form an inversion of A if i < j and A[i] > A[j]. Let inv(A) = {(i, j) | i < j and A[i] > A[j]}. Answer the following: (a) How small can the number of inversions be? Give an example of an array of length n with the smallest possible number of inversions. (b)...
Project: Given an array numbers. We define a running sum of an array as runningSum[i] =...
Project: Given an array numbers. We define a running sum of an array as runningSum[i] = sum(nums[0]…nums[i]). Return the running sum of numbers. Example: Input: nums = [1,2,3,4] Output: [1,3,6,10] Explanation: Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4]. You need to do: Create a class called RunningSumArray to perform the main method. Create a class called ArrayOperationto hold the actions for array. In this project, you will need 4methods: 1. Public static Int[] getArray() --- inside ArrayOperationclass,...
Given an integer array sorted in non-decreasing order, there is exactly one integer in the array...
Given an integer array sorted in non-decreasing order, there is exactly one integer in the array that occurs more than 25% of the time. Return that integer. Input: arr = [1,2,2,6,6,6,6,7,10] Output:
We are given a non sorted array so that the values are not pairwise disjoint (the...
We are given a non sorted array so that the values are not pairwise disjoint (the same values may appear in many entries). Say that we can find the k-th smallest element number in the array in time O(n) for any 1 <= k <= n. Give an algorithm that finds if there is a value that appears at least n/3 times. Please explain the algorithm in words and analyze the run time.
python3: Given a list of numbers in order order, return a new list sorted in non-decreasing...
python3: Given a list of numbers in order order, return a new list sorted in non-decreasing order, and leave the original list unchanged. function only def mergeSort(inputArray): #code here a = [3,4,6,1,21,11,9,8] b = mergeSort(a) print('original array is: ', a) print('sorted array is: ', b)
Given an array of numbers, find the index of the smallest array element (the pivot), for...
Given an array of numbers, find the index of the smallest array element (the pivot), for which the sums of all elements to the left and to the right are equal. The array may not be reordered. Example arr=[1,2,3,4,6] the sum of the first three elements, 1+2+3=6. The value of the last element is 6. Using zero based indexing, arr[3]=4 is the pivot between the two subarrays. The index of the pivot is 3. Function Description Complete the function balancedSum...
6. Given the following array of characters: REALITYSHOWShow how this array will be sorted with the...
6. Given the following array of characters: REALITYSHOWShow how this array will be sorted with the help of : (a) insertion sort; (b) selection sort; (c) bubble sort with swaps counting; (d) bubble sort without swaps counting subject design and analysis of algorithms answer should be like eg p u r p l e p r u p l e
On a circular array with n positions, we wish to place the integers 1, 2, ......
On a circular array with n positions, we wish to place the integers 1, 2, ... r in order, clockwise, such that consecutive integers, including the pair (r,1) are not in adjacent positions on the array. Arrangements obtained by rotation are considered the same. In how many ways can this be done? Give a combinatorial proof.
Given an array of positive numbers and a positive number S, find the length of the...
Given an array of positive numbers and a positive number S, find the length of the smallest contiguous subarray whose sum is greater than or equal to S. Return 0, if no such subarray exists. Example 1 Input: [2, 1, 5, 2, 3, 2], S=7 Output: 2 Explanation: The smallest subarray with a sum great than or equal to '7' is [5, 2]. Example 2 Input: [2, 1, 5, 2, 8], S=7 Output: 1 Explanation: The smallest subarray with a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT