Question

In: Computer Science

1. We have an array A of size n. There are only positive integers in this...

1. We have an array A of size n. There are only positive integers in this array. Note that the array may have integers that are not distinct, and it could be any array of positive integers in creation (I like the numbers found the decimal expansion of π for instance). When possible provide the exact complexity of the algorithm. If it’s not possible explain the O/Ω/Θ complexity.

  1. Design an efficient algorithm to find the maximum difference between any two integers in the array.

  2. Design an efficient algorithm to find the minimum difference between any two integers in the array. If there are multiple minimum differences, you only need to find one.

  3. Design an efficient algorithm to find the majority number in the array if it exists. By majority, I mean the number that occurs more than half the time in the array or ⌈(? + 1)/2 ⌉ (there are at least 3 ways that I can think of to approach this).

Solutions

Expert Solution

Question 1.

part (a):-

We can find the maximum difference between any two integer by traversing the array only once . Since all the elements in the array are positive so maximum difference ocuurence when we subtract minimum from maximum.so in one traversal we can find the minimum and maximum element from the array and reurn the maximum-minimum this will take O(n) times where n is the number of elements in the array.

Part(b):-

for finding minimum difference we have to sort the array and compare all adjacents pairs in sorted array and keep track of minimum differences . This algorithm will take O(nlogn) because in sorting it will take O(nlogn) and in find minimum between two adjacent pair it will take O(n) time so overall it will take O(nlogn).

Part (c):-

There are various ways to solve this problem :-

method 1:-

we will count the occurence of every element using two loop. and test if the count for that particular element is greater than (n+1)/2 then output that element as majority element. this will take O(n*n) time.

method 2:-

first of all sort the array and find the and check for the count of mid or (mid+1)th element if there count is greater than (n+1)/2 then output that element as majority element . this will take O(nlogn) for sorting.so overall time complexity is O(nlogn).

method 3:-

we can modify the method 1 using hashing we will count the occurence of every element in the array using hashing and check which element has occurence greater than (n+1)/2 .this will take O(n) time as insertion in hashing will take O(1) time .So overall time complexity is O(n) and also space complexity is O(n) for hashtable.

Note :-

If you have any problem let me know i will try to help you . Thank you.


Related Solutions

1. We have an array A of size n. There are only positive integers in this...
1. We have an array A of size n. There are only positive integers in this array. Note that the array may have integers that are not distinct, and it could be any array of positive integers in creation (I like the numbers found the decimal expansion of π for instance). When possible provide the exact complexity of the algorithm. If it’s not possible explain the O/Ω/Θ complexity. Design an efficient algorithm to find the maximum difference between any two...
We have an array A of size n. There are only positive integers in this array....
We have an array A of size n. There are only positive integers in this array. Note that the array may have integers that are not distinct, and it could be any array of positive integers in creation (I like the numbers found the decimal expansion of π for instance). When possible provide the exact complexity of the algorithm. If it’s not possible explain the O/Ω/Θ complexity. a. Design an efficient algorithm to find the maximum difference between any two...
We have an array A of size n. There are only positive integers in this array....
We have an array A of size n. There are only positive integers in this array. Note that the array may have integers that are not distinct, and it could be any array of positive integers in creation (I like the numbers found the decimal expansion of π for instance). When possible provide the exact complexity of the algorithm. If it’s not possible explain the O/Ω/Θ complexity. a. Design an efficient algorithm to find the maximum difference between any two...
Consider the problem of sorting an array A[1, ..., n] of integers. We presented an O(n...
Consider the problem of sorting an array A[1, ..., n] of integers. We presented an O(n log n)-time algorithm in class and, also, proved a lower bound of Ω(n log n) for any comparison-based algorithm. 2. Give an efficient sorting algorithm for an array C[1, ..., n] whose elements are taken from the set {1, 2, 3, 4, 5}.
Consider the problem of sorting an array A[1, ..., n] of integers. We presented an O(n...
Consider the problem of sorting an array A[1, ..., n] of integers. We presented an O(n log n)-time algorithm in class and, also, proved a lower bound of Ω(n log n) for any comparison-based algorithm. 3. Give an efficient sorting algorithm for an array D[1, ..., n] whose elements are distinct (D[i] ̸= D[j], for every i ̸= j ∈ {1, ..., n}) and are taken from the set {1, 2, ..., 2n}.
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 unsorted array A of size N of integers, find a continuous sub-array which adds...
Given an unsorted array A of size N of integers, find a continuous sub-array which adds to the given number. Declare dynamic arrays and use only pointers syntax. (no [ ]'s or (ptr+1) stuff. input will be the number of input values to enter followed by the sum to compare with. print out the continuous sub-array of values that are equal to sum or the message 'no sum ofund.' there may be more than one sub-array to be found in...
1) Write a function searchValue that accepts an array of integers, the size of the array,...
1) Write a function searchValue that accepts an array of integers, the size of the array, and an integer. Find the last occurrence of the integer passed in as an input argument in the array. Return the index of the last occurrence of the value. If the value is not found, return a -1 2) Write the line of code to call the previous function assuming you have an array vec with length n, and are looking for the number...
Consider an array of length n containing positive and negative integers in random order. Write the...
Consider an array of length n containing positive and negative integers in random order. Write the C++ code that rearranges the integers so that the negative integers appear before the positive integers. write a program that includes both functions and a main() function that tests them. Name the two functions rearrangeN() and rearrangeN2().
Let iqsort(A, 1, n) be an algorithm that sorts an array A with n integers. It...
Let iqsort(A, 1, n) be an algorithm that sorts an array A with n integers. It works as follows: iqsort(A, p, q){ if p ≥ q, return; r=partition(A, p, q); //run quick sort on the low part quicksort(A, p, r − 1); //run insert sort on the high part insertsort(A, r + 1, q); } Compute the best-case, worst-case, and average-case complexities of iqsort.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT