What would be the binary search complexity to find second largest number in array - time-complexity

Can someone explain how to calculate the binary search complexity to find second largest number in array.

Binary search is done on a sorted array.
If you already have a sorted array, why do you need to do anything at all?
The second to last number in the array (sorted in ascending order) would be the second largest number.(O(1))
If the array contains duplicates:
For example,
{0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,... }
The time complexity would be O(log n) where n is the number of elements in the array.
The smallest number is the one at index 0 (call it x), now you can use binary search to find the array bounds within which all elements are equal to x. The immediate neighbour outside these bounds would be the second largest number in the array.
If you are using C++, you can use this method to get the upper_bound.

Binary search for an element with any given property is always logarithmic, provided that you can determine in constant time whether that property holds.
If the array can’t contain duplicates, you don’t need a binary search and the complexity is constant.

One way to do it in python efficiently can be to convert list[which allows duplicates] to set[which does not allow duplicates] almost in O(1) time and then fetching item at index[-2] again in O(1) time, assuming that as it is binary search list would be sorted in ascending order.

Related

Time Complexity of 1-pass lookup given input size N**2

Given a list of lists, i.e.
[[1,2,3],[4,5,6],[7,8,9]]:
What is the time complexity of using nested For loops to see if each numeral from 1-9 is used once and only once? Furthermore, what would be the time complexity if the input is now a singular combined list, i.e. [1,2,3,4,5,6,7,8,9]?
What really matters is the size of the input, not the format. Either you have a list of 9 elements or 9 lists with 1 element, you still have 9 elements to be checked in the worst case.
The answer to the question, as stated, would be O(1), because you have a constant size input.
If what you mean is something like Given N elements what is the time complexity of checking if all number between 1 and N are present, then it would take linear time, i.e., O(N).
Indeed, an option is to use a hash table (e.g., a python set) and check if the element is already in the set, if not adding it. Note that in using this specific option you would get an expected (but not guaranteed, due to potential collisions) linear time complexity algorithm.

How to calculate the worst case time for binary search for a key that appears twice in the sorted array?

What would be the worst case time complexity for finding a key that appears twice in the sorted array using binary search? I know that the worst case time complexity for binary search on a sorted array is O(log n). So, in the case that the key appears more than once the time complexity should be lesser than O(log n). However, I am not sure how to calculate this.
In the worst case the binary search needs to perform ⌊log_2(n) + 1⌋ iterations to find the element or to conclude that the element is not in the array.
By having a duplicate you might just need one step less.
For instance, suppose your duplicate elements appear in the first and second indices of the array (same if they are in the last and one before the last).
In such a case you would have ⌊log_2(n)⌋ comparisons, thus, still O(log(n)) as a worst case time complexity.

What is the time complexity of Search in ArrayList?

One interview question which I couldn't answer and couldn't find any relevant answers online.
I know the arraylist retrieve the data in constant time based on the indexes.
Suppose in an arraylist, there are 10000 data and the element is at 5000th location(We are not given the location), we have to search for a particular value( for eg integer 3 which happens to be on the 5000th index), for searching the value, we will have to traverse through the arraylist to find the value and it would take linear time right??
Because if we are traversing through the arraylist to find the data, it would take linear time and not constant time.
In short I want to know the internal working of contains method in which I have to check for the particular value and I don't have the index. It will have to traverse through the array to check for the particular value and it would take O(n) time right?
Thanks in advance.
I hope this is what you want to know about search in ArrayList:
Arrays are laid sequentially in memory. This means, if it is an array of integers that uses 4 bytes each, and starts at memory address 1000, next element will be at 1004, and next at 1008, and so forth. Thus, if I want the element at position 20 in my array, the code in get() will have to compute:
1000 + 20 * 4 = 1080
to have the exact memory address of the element. Well, RAM memory got their name of Random Access Memory because they are built in such way that they have a hierarchy of hardware multiplexers that allow them to access any stored memory unit (byte?) in constant time, given the address.
Thus, two simple arithmetic operations and one access to RAM is said to be O(1). See link to original answer.

When sequencial search is better than binary search?

I know that:
A linear search looks down a list, one item at a time, without jumping. In complexity terms this is an O(n) search - the time taken to search the list gets bigger at the same rate as the list does.
A binary search is when you start with the middle of a sorted list, and see whether that's greater than or less than the value you're looking for, which determines whether the value is in the first or second half of the list. Jump to the half way through the sublist, and compare again etc.
Is there a case where the sequencial/linear search becomes more eficient than Binary Search ?
Yes, e.g. when the item you are looking for happens to be one of the first to be looked at in a sequential search.

Order Integers In Ascending and Descending Orders

I'm working with Objective-C, but probably it doesn't matter the programming language for this. So basically I have an array, with say, the integers 12, 5, and 17, and I want to be able to pull out the largest number, or the smallest, or second smallest, etc.
Basically I want to be able to sort them into ascending or decending order so I could pick out, for instance, the second smallest number by retrieving the objectAtIndex: 1 if it is sorted in ascending order. I feel like this is incredibly obvious but I can't think of how to do it at the moment, so I would love it if someone could enlighten me.
If you have an NSArray with NSNumber instances, then the sort you are looking for is as easy as this:
NSArray* sortedNumbers = [unorderedNumbers sortedArrayUsingSelector:#selector(intValue)];
It will sort ascending, so [sortedNumbers lastObject] will be the greatest value.
There are many more sorting methods on NSArray if you have more specific needs. NSArray sorting
Almost every high level language, including objective-c, have library to sort an array. But as you said that language does not matter, probably you are looking for the algorithm itself. There are a number a sorting algorithms with different computational complexity. You can find them in any standard algorithm book. Or these 2 pages might be helpful:
Sorting Algorithms in Wikipedia.
sorting-algorithms.com. Contains nice explanation with animation.
And if you are interested particularly in objective-c, check the Sorting section of NSArray reference. This contains an example to sort an array of integer.
Just sort the array in ascending order (I don't use objective C, but I am sure there is a function for it) and then get the element wherever you want...
To get the largest
array[array.length - 1]
Second largest
array[array.length -2]
Smallest
array[0]
Second smallest
array[1]
You should check to make sure that the array index is valid:
if (array.length - 2> 0) //Second largest element
return array[array.length - 2];
Or:
if (array.length > 1) //Second smallest element
return array[1];
See here for how to sort an array in objective C:
http://howtomakeiphoneapps.com/2009/03/how-to-sort-an-array-in-objective-c/
If you want to preserve the order of the original array, one method is to create a second array that just contains the numbers 0, 1, ... n, representing indexes into the first array. Then sort the second array, but instead of comparing its values, compare the corresponding values that it points to in the first array. (You could also just store pointers and sort based on the dereferenced pointers.)
Then to find the second-largest number, look up the index in the second-to-last position in the second array and see where it points to in the first array.
If you want to get fancy and avoid sorting, this lecture describes an algorithm for finding the k-largest element in linear time. I haven't actually used it, but it looks like it might be a good method if your data changes often, as you wouldn't have to maintain the extra array.
If your goal is to get the highest number, or lowest, or second-lowest, or what have you, and you only need one number from the result, then sorting is overkill. Instead you should just iterate over the entire array and keep track of the highest (or lowest, or 2 lowest (for the second-lowest)) number seen so far. If your language supports this, it'll be called a "fold". The only reason to actually sort the array is if you need to access multiple different ranked values from the array.