a. Design an efficient algorithm to find the maximum difference
between any two integers in the array.
- For this we parse array one time and find minimum and maximum
integer in array.Than we can find difference of both.This can be
done in running loop 1 time. So overall time complexity is
O(N).
b. Design an efficient algorithm to find the minimum difference
between any two integers in the array.
- We will sort array in ascending order. Sorting will take
O(NlogN) time. Then we comapre all adjecent pairs in sorted array
and find min difference. This step will take O(N) time. So overall
time complexity is O(NlogN).
c. Design an efficient algorithm to find the majority number in
the array if it exists.
- We will sort the array. Sorting will take O(NlogN) time. Then
we will create a variable count and prevElement.We will traverse
the array. If the current element is equal to the previous element
increase the count. Else set the count to 1. If the value of count
is greater than half the size of array then it is the majority
number.
- Time Complexity = O(NlogN).
Like, if this helped :)