Optimal number of comparison to find min and max - algorithm

I know optimal number of comparison to for finding MINIMUM and MAXIMUM of n keys is
3n/2
but what if n is an integer power of 2 ? Then what would be number of comparison ?
Some more details:
For example, a graphics program may need to scale a set of (x,y)
data to fit onto a rectangular display screen or other graphical output device.To do so, the program must first determine the minimum and maximum value of each coordinate. At this point, it should be obvious how to determine both the minimum and the maximum of n elements using ‚theta(n) comparisons, which is asymptotically optimal: we simply find the minimum and maximum independently, using n -1 comparisons for each, for a total of 2n -2 comparisons.So in this case we can find both minimum and maximum in 3[n/2] comparison. But what if n is a power integer of 2 ? Then what would be the number of comparison to find MIN and MAX of n keys ?

Related

How can N integers be divided into M equally sized groups such that the difference between their maximum and minimum sums is minimum?

How can N integers be divided into M equally sized groups such that the difference between their maximum and minimum sums is minimum?
I see that it belongs to the partition kind of problems, but I didn't find a solution that matches both "M > 2" and "equally sized" criteria

Finding largest difference between two elements in array using at most 3/2n comparisons [duplicate]

This question already has answers here:
How to find max. and min. in array using minimum comparisons?
(14 answers)
Closed 2 years ago.
I am working on given an unsorted array with integer elements,
A={a_1,a_2,...,a_n}
finding largest difference between two elements in array (max|a_i-a_j|)with using at most 3/2n comparisons in the worst case.(Runtime do not matter and we can't use operations such as max or min).
I really doubt if this is possible: to find the maximum difference of two elements, in the worst case, shouldn't we always need about 2n comparisons, as we need to use about n comparisons to find the largest element of the array and another n comparisons to find the smallest element of the array? I don't see where can I cut the operations.
I have also considered divide and conquer. Suppose I divide this array into 2 subarrays with length n/2, but then I encountered the same problem, as finding maximum and minimum in each subarray with take about n comparisons so there will be 2n comparisons in total.
A hint on how to do this will be really appreciated.
cppreference proposes an example of implementation of std::minmax_element with a complexity of 3/2 n. The basic idea is to process 2 elements by 2 elements.
If A[i+1] > A[i]
A[i+1] is compared with Max
A[i] is compared with Min
Else
A[i] is compared with Max
A[i+1] is compared with Min
2 elements considered, 3 comparisons -> Complexity O(3/2 n)
Note: in n is odd, last element must be considered separately.
It is straightforward to show that finding the maximum difference is equal to finding the minimum and maximum of the array elements. On the other hand, you can find the minimum and the maximum of an array simultaneously with 3n/2 comparison (the third method in all common programming languages,i.e., C#, C++, Python, C, and Java):
If n is odd then initialize min and max as the first element.
If n is even then initialize min and max as minimum and maximum of the first two elements respectively.
For the rest of the elements, pick them in pairs and compare their
maximum and minimum with max and min respectively.
Total number of comparisons: Different for even and odd n, see below:
If n is odd: 3*(n-1)/2
If n is even: 1 Initial comparison for initializing min and max,
and 3(n-2)/2 comparisons for rest of the elements
= 1 + 3*(n-2)/2 = 3n/2 -2

Given Set of rationals and some value. Minimize elements needed for exceeding the value. (Linear time)

Given a set of n elements x > 0 and a weight W. Find the minimum of elements needed that sum up to W or greater.
I am struggling to find a linear time (O(n)) algorithm.
[Edit: Linear sorting algorithm cannot be used her since all values are rationals (also distinct rationals)]
Firstly you need to sort these elements. And then you can easily find the minimum number of elements in linear time. You need to go from the largest element to the smallest and sum them up as long as this sum <= W.
The usual sorting algorithm takes O(n*log(n)). So you need an algorithm for sorting numbers in linear time. If you have a set of integers, you can use one of these algorithms http://staff.ustc.edu.cn/~csli/graduate/algorithms/book6/chap09.htm

Find triplets in an array whose sum is some integer X

Given a sorted array[1..n] where each element ranging from 1 to 2n. Is there a way to find triplet whose sum is given integer x. I know O(n^2) solution. Is there any algorithm better than n^2 ones.
It is possible to achieve an O(n log n) time complexity using that fact the maximum value of each element is O(n).
For each 1 <= y <= 4 * n, let's find the number of pairs of elements that sum up to y. We can create a polynomial of 2 * n power, where the i-th coefficient of this polynomial is the number of occurrences of the number i in the given array. Now we can find the square(I'll call it s) of this polynomial in O(n log n) time using Fourier's Fast Transform. The i-th coefficient of s is exactly the number of pairs of elements that sum up to i.
Now we can iterate over the given array. Let's assume that the current element is a. Then we just need to check the number of pairs that sum up to X - a. We have already done it in the step 1).
If all triplets must consist of different elements, we need to subtract the number of such triplets that sum up to X but contain duplicates. We can do it in O(n log n) time, too(for triplets that consist of three equal elements, we just need to subtract the number of occurrences of X / 3 in the given array. For triplets with one duplicate, we can just iterate over the element that is repeated twice(a) and subtract the number of occurrences of X - 2 * a).
If we need to find a triplet itself, not just count them, we can do the following:
Count the number of triplets and pairs as suggested above.
Find such an element that there is a pair that sums up to X with it.
Find two elements that sum up to the desired sum of this pair.
All these steps can be accomplished in linear time(using the fact that all sums are O(n)).
Your problem is apparently the non-zero sum variant of the 3SUM problem.
Because you know the possible range of the integers beforehand, you can achieve lower bounds than the general case, according to the second paragraph of the article:
When the elements are integers in the range [-N, ..., N], 3SUM can be
solved in O(n + N log N) time by representing the input set S as a
bit vector, computing the set S + S of all pairwise sums as a discrete
convolution using the Fast Fourier transform, and finally comparing
this set to -S.
In your case, you would have to pre-process the array by subtracting n + X/3 before running the algorithm.
One thing to note is that the algorithm assumes you're working with a set of numbers, and I'm not sure what (if any) implications there may be on running time if your array may include duplicates.

faster algorithms for minimum maximum contiguous k partition

I was reading this http://www.cas.mcmaster.ca/~terlaky/4-6TD3/slides/DP/DP.pdf and would like to know if there exists a solution with better time complexity to the partition problem.
From the link:
"Suppose a given arrangement S of non-negative numbers
{s1,...,sn} and an integer k. How to cut S into k or fewer ranges,
so as to minimize the maximum sum over all the ranges?"
e.g.
S = 1,2,3,4,5,6,7,8,9
k=3
By cutting S into these 3 ranges, the sum of the maximum range (8,9) is 17, which is the minimum possible.
1,2,3,4,5|6,7|8,9
The algorithm suggested in the link runs in O(kn^2) and uses O(kn) space. Are there more efficient algorithms?
Ok so apparently this was closed for being "off-topic"!?
But it's back up now so anyway, I found the solution to be binary searching the answer. Sorry I forgot one of the constraints was that the sum of all the integers would not exceed 2^64. So let C = cumulative sum of all integers. Then we can binary search for the answer using a
bool isPossible(int x)
function which returns true if it is possible to divide S into k partitions with maximum partition sum less than X. isPossible(int x) can be done in O(n) (by adding everything from left to right and if it exceeds x make a new partition). So the total running time is O(n*log(s)).

Resources