Shri
BAN USER
Comments (3)
Reputation 0
Page:
1
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0
of 0 vote
The binary search won't work for the following use case {1000,90,80,70,60,-200,3,4,5,100000000} as we would go towards the opposite end for getting the max value and also the min value is in middle.
Here's the code that deals with such usecase. The complexity is O(n).
int[] a= {1000,90,80,70,60,-200,3,4,5,100000000};
if (a.length < 2) {
throw new IllegalArgumentException("Getting a profit requires at least 2 prices");
}
int min = a[0];
int max = a[1];
for (int i = 1; i < a.length; i++) {
int current = a[i];
min = Math.min(min, current);
max = Math.max(max, current);
}
System.out.println("Min is :"+min);
System.out.println("Max is :"+max);
Page:
1
CareerCup is the world's biggest and best source for software engineering interview preparation. See all our resources.
I guess a simple way to do it could be as follows. Please let me know if there is any mistake
- Shri November 22, 2016