Problem You have a large text file containing words. Given any two words, find the shortest distance (in terms of number of words) between them in the file. Can you make the searching operation in O(1) time? What about the space complexity for your solution?
OR
Find the minimum distance between 2 numbers in the array?
Example File contains:
as was is the as the yahoo you me was the and
[Read More]
Stack with find-min/find-max in O(1) time
Problem
Stack with find-min/find-max in O(1) time
Solution
Method 1 - using extra stacks for min and max
The basic idea of the solution can be found here.
Here instead of 2 stacks we will be using 3 stacks -1 stack with normal elements, 1 with only max elements and 1 with min element.
Now the operations will be following :
push:
- If the element being pushed is less than the top element of ‘min_stack’ then push it on ‘min_stack’ as well.
[Read More]
Find the minimum and maximum in the array
Here are few question we find on finding min and max in the array, but array type changes.
Unsorted 1D array
Find the maximum (OR minimum) in an array Find the maximum AND minimum in an array with min number of comparisons Find the max and second maximum in an array (likewise for minimum) Find the max and nth max in an array Rotated Sorted array
Find the minimum element in the rotated sorted array.
[Read More]
Find Maximum Value in the unsorted array
**Problem : **
Find Maximum Value in the unsorted array
Solution
Method 1 - Linear search
Here is the code to do that :
public int findMax(int\[\] numbers)
{
int max = 0;
for (int i = 0; i < numbers.length; ++i)
if (numbers\[i\] > max) max = numbers\[i\];
return max;
}
So, in worst case the number of comparisons will be (n-1).
Time complexity - O(n)
Find kth largest in an Array
**Problem : **
You have an array of numbers; you need to find the k-th largest in the arra
Solution
**
Method 1 - Tournament principle**
We have to find the element which has competed with largest element, and is just (k-1)th level.We have discussed this principle here.
Method 2 - Using max heap
Consider the array of length n. Here is the algo : Build a max heap in O(n) ; Now remove k elements from the heap; wherein each removal costs log(n) time to maintain the heap property.
[Read More]
find out the largest and second largest number in an array
find out the largest and second largest number in an array
Question: Write an efficient C program to find smallest and second smallest element in an array.
Solution
Method 1 - Compare each element with current max and second max
Algorithm
1) Initialize both first and second smallest as a first = second = a 2) Loop through all the elements. a) If the current element is greater than firstMax, then update firstMax and secondMax. b) Else if the current element is greater than second then update secondMax pseudocode
[Read More]
How to find max. and min. in array using minimum comparisons?
Problem : Given an array of integers find the max. and min. using minimum comparisons.
Solution
Method 1 (Naive) - Iterate through the array, and update min and max pointers
1\. Iterate through the array, select element a 2\. Update min by comparing (min, a) 3\. Update max by comparing (max, a) Number of comparison here will be ~2N, if N is number of element.
Time complexity will be O(n) though.
[Read More]
Find Nth largest element in the rotated sorted array
Please give review to this post http://rawjava.blo…
Unknown - Apr 1, 2015
Please give review to this post
http://rawjava.blogspot.com/2015/04/java-program-to-find-maximum-number.html
Good question and equally good answer..Enjoyed your post :)
Find Nth largest element in the rotated sorted array
**Question : **Find Nth largest element in the rotated sorted array
So for example we have sorted array as -
2,3,6,12, 15, 18.
Now suppose the array is rotated k times ( we don’t know k), such that array becomes
15, 18,2,3,6,12
Solution
So, to find the Nth largest element in normal sorted array is a[N]. But in this case it is rotated k, which we don’t know.
[Read More]