Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the least element in an array, which has a pattern

An array is given such that its element's value increases from 0th index through some (k-1) index. At k the value is minimum, and than it starts increasing again through the nth element. Find the minimum element.

Essentially, its one sorted list appended to another; example: (1, 2, 3, 4, 0, 1, 2, 3).

I have tried all sorts of algorithm like buliding min-heap, quick select or just plain traversing. But cant get it below O(n). But there is a pattern in this array, something that suggest binary search kind of thing should be possible, and complexity should be something like O(log n), but cant find anything. Thoughts ??

Thanks

like image 998
ocwirk Avatar asked Sep 28 '11 18:09

ocwirk


People also ask

How do you find the least common element in an array?

Given an array, find the least frequent element in it. If there are multiple elements that appear least number of times, print any one of them. Input : arr [] = {1, 3, 2, 1, 2, 2, 3, 1} Output : 3 3 appears minimum number of times in given array.

How to find the smallest element of an array in Python?

START Step 1 → Take an array A and define its values Step 2 → Declare smallest as integer Step 3 → Set smallest to 0 Step 4 → Loop for each value of A Step 5 → If A [n] < smallest, Assign A [n] to smallest Step 6 → After loop finishes, Display smallest as smallest element of array STOP.

How to find the number with least frequency in an array?

The second way is to use a hash map to keep the count of the elements occurrence in the array and then return the number with least frequency. It is the most efficient method. Count the frequency of the numbers occurrence and store them in a object. Then iterate all the elements of the object and find the number with least frequency.

What is the least frequent element in an array in Python?

Finally, after the completion of the loops, the least frequent element in the array ( leastElement) is found to be 16 and its count was found to be 1. Following is the implementation of our Naive approach in Python:


2 Answers

No The drop can be anywhere, there is no structure to this.

Consider the extremes

1234567890
9012345678
1234056789
1357024689

It reduces to finding the minimum element.

like image 105
Captain Giraffe Avatar answered Sep 19 '22 18:09

Captain Giraffe


Do a breadth-wise binary search for a decreasing range, with a one-element overlap at the binary splits. In other words, if you had, say, 17 elements, compare elements

0,8
8,16
0,4
4,8
8,12
12,16
0,2
2,4

etc., looking for a case where the left element is greater than the right.

Once you find such a range, recurse, doing the same binary search within that range. Repeat until you've found the decreasing adjacent pair.

The average complexity is not less than O(log n), with a worst-case of O(n). Can anyone get a tighter average-complexity estimate? It seems roughly "halfway between" O(log n) and O(n), but I don't see how to evaluate it. It also depends on any additional constraints on the ranges of values and size of increment from one member to the next.

If the increment between elements is always 1, there's an O(log n) solution.

like image 39
Ed Staub Avatar answered Sep 21 '22 18:09

Ed Staub