Binary Search Application
Aggressive Cows - Concept, Code, Complexity
It can be used a variety of problems!
We have seen Binary Search can be used for searching in a sorted array!
Non-increasing fn
Non-decreasing fn
Farmer John has built a new long farm, with N stalls. The stalls are located along a straight line at positions x1,...,xN.
x1 x2 x3 x4 x5
His C (2 <= C <= N) cows don't like this barn layout and become aggressive towards each other once put into a stall. To prevent the cows from hurting each other, Farmer wants to assign the cows to the stalls, such that the minimum distance between any two of them is as large as possible. What is the largest minimum distance?
x1 x2 x3 x4 x5
N = 5 Stalls
Stalls = [1,4, 8, 2 9]
C = 3 Cows
x=8
x=9
x=1
x=2
x=4
Goal : Place C cows in N stalls so that
min difference between two cows is maximum.
x=8
x=9
x=1
x=2
x=4
Possible Scenario #1
x=8
x=9
x=1
x=2
x=4
Possible Scenario #1
x=8
x=9
x=1
x=2
x=4
Possible Scenario #1
x=8
x=9
x=1
x=2
x=4
Possible Scenario #1
x=8
x=9
x=1
x=2
x=4
1 unit
2 unit
sep = min(1,2) = 1
Possible Scenario #2
x=8
x=9
x=1
x=2
x=4
Possible Scenario #2
x=8
x=9
x=1
x=2
x=4
Possible Scenario #2
x=8
x=9
x=1
x=2
x=4
Possible Scenario #2
x=8
x=9
x=1
x=2
x=4
2 unit
5 units
sep = min(2,5) = 2
Possible Scenario #3
x=8
x=9
x=1
x=2
x=4
Possible Scenario #3
x=8
x=9
x=1
x=2
x=4
Possible Scenario #3
x=8
x=9
x=1
x=2
x=4
Possible Scenario #3
x=8
x=9
x=1
x=2
x=4
3 unit
4 unit
ans = min(3,4) = 3
Scenario #1 min_sep = 1
Scenario #2 min_sep = 2
Scenerio #3 min_sep = 3
.
.
.
Find the maximum of min separation!
Scenario #1 min_sep = 1
Scenario #2 min_sep = 2
Scenerio #3 min_sep = 3
.
.
.
Find the maximum of min separation!
Find out the search space!
The search space is the range in which your answer can possibly lie.
Goal : Place C cows in N stalls so that
min difference between two cows is maximum.
x=8
x=9
x=1
x=2
x=4
Search Space : Distance between cows!
Min Distance : ___
x=8
x=9
x=1
x=2
x=4
Min Distance : 0
x=8
x=9
x=1
x=2
x=4
Max Distance :
Last Stall - First Stall
(Note: after Sorting according to coordinates)
= 9 - 1
= 8
x=8
x=9
x=1
x=2
x=4
x=8
x=9
x=1
x=2
x=4
0 1 2 3 4 5 6 7 8
Distance
Stalls
x=8
x=9
x=1
x=2
x=4
0 1 2 3 4 5 6 7 8
Distance
Stalls
x=8
x=9
x=1
x=2
x=4
0 1 2 3 4 5 6 7 8
Distance
Stalls
mid
x=8
x=9
x=1
x=2
x=4
0 1 2 3 4 5 6 7 8
Distance
Stalls
mid = 4
x=8
x=9
x=1
x=2
x=4
Distance = 4
Stalls
Cows Placed = 1
x=8
x=9
x=1
x=2
x=4
Distance = 4
Stalls
Cows Placed = 1
x=8
x=9
x=1
x=2
x=4
Distance = 4
Stalls
Cows Placed = 1
x=8
x=9
x=1
x=2
x=4
Distance = 4
Stalls
Cows Placed = 2
x=8
x=9
x=1
x=2
x=4
Distance = 4
Stalls
Cows Placed = 2
x=8
x=9
x=1
x=2
x=4
Distance = 4
Stalls
Cows Placed = 2
Hence, we could place only two cows! 😔
Clearly 4 is not the answer, go left in search space!
x=8
x=9
x=1
x=2
x=4
0 1 2 3 4 5 6 7 8
Distance
Stalls
mid = 4
x=8
x=9
x=1
x=2
x=4
0 1 2 3 4 5 6 7 8
Distance
Stalls
e = mid - 1
x=8
x=9
x=1
x=2
x=4
0 1 2 3 4 5 6 7 8
Distance
Stalls
mid
x=8
x=9
x=1
x=2
x=4
0 1 2 3 4 5 6 7 8
Distance
Stalls
mid
x=8
x=9
x=1
x=2
x=4
Distance = 1
Stalls
Cows Placed = 1
x=8
x=9
x=1
x=2
x=4
Distance = 1
Stalls
Cows Placed = 2
x=8
x=9
x=1
x=2
x=4
Distance = 1
Stalls
Cows Placed = 3
x=8
x=9
x=1
x=2
x=4
Distance = 1
Stalls
Cows Placed = 3
Hence, as all cows are placed 😁 we stop and assume 1 as our current ans.
x=8
x=9
x=1
x=2
x=4
0 1 2 3 4 5 6 7 8
Distance
Stalls
mid
current_ans = 1
x=8
x=9
x=1
x=2
x=4
0 1 2 3 4 5 6 7 8
Distance
Stalls
mid
s = mid + 1
current_ans = 1
x=8
x=9
x=1
x=2
x=4
0 1 2 3 4 5 6 7 8
Distance
Stalls
x=8
x=9
x=1
x=2
x=4
0 1 2 3 4 5 6 7 8
Distance
Stalls
mid
x=8
x=9
x=1
x=2
x=4
0 1 2 3 4 5 6 7 8
Distance
Stalls
mid
x=8
x=9
x=1
x=2
x=4
Distance = 2
Stalls
Cows Placed = 1
x=8
x=9
x=1
x=2
x=4
Distance = 2
Stalls
Cows Placed = 1
x=8
x=9
x=1
x=2
x=4
Distance = 2
Stalls
Cows Placed = 2
x=8
x=9
x=1
x=2
x=4
Distance = 2
Stalls
Cows Placed = 3
x=8
x=9
x=1
x=2
x=4
Distance = 2
Stalls
Cows Placed = 3
Hence, as all cows are placed 😁 we stop and update 2 as our current ans.
x=8
x=9
x=1
x=2
x=4
0 1 2 3 4 5 6 7 8
Distance
Stalls
mid
s = mid + 1
current_ans = 2
x=8
x=9
x=1
x=2
x=4
0 1 2 3 4 5 6 7 8
Distance
Stalls
x=8
x=9
x=1
x=2
x=4
0 1 2 3 4 5 6 7 8
Distance
Stalls
mid
x=8
x=9
x=1
x=2
x=4
0 1 2 3 4 5 6 7 8
Distance
Stalls
mid
x=8
x=9
x=1
x=2
x=4
Distance = 3
Stalls
Cows Placed = 1
x=8
x=9
x=1
x=2
x=4
Distance = 3
Stalls
Cows Placed = 1
x=8
x=9
x=1
x=2
x=4
Distance = 3
Stalls
Cows Placed = 1
x=8
x=9
x=1
x=2
x=4
Distance = 3
Stalls
Cows Placed = 2
x=8
x=9
x=1
x=2
x=4
Distance = 3
Stalls
Cows Placed = 3
x=8
x=9
x=1
x=2
x=4
Distance = 3
Stalls
Cows Placed = 3
Hence, as all cows are placed 😁 we stop and update 3 as our current ans.
x=8
x=9
x=1
x=2
x=4
0 1 2 3 4 5 6 7 8
Distance
Stalls
mid
s = mid + 1
current_ans = 3
x=8
x=9
x=1
x=2
x=4
0 1 2 3 4 5 6 7 8
Distance
Stalls
mid
s > e 🧐
current_ans = 3
x=8
x=9
x=1
x=2
x=4
0 1 2 3 4 5 6 7 8
Distance
Stalls
mid
STOP
current_ans = 3
x=8
x=9
x=1
x=2
x=4
0 1 2 3 4 5 6 7 8
Distance
Stalls
mid
ans = 3
current_ans = 3
0 1 2 3 4 5 6 7 8
Distance
0 1 2 3 4 5 6 7 8
f
1
0
ans = 3
Binary Search is an algorithm that can be used to search an element in a sorted dataset monotonic search space.
Search space depends upon the value to be searched.
Depending upon the problem statement we might be optimising for a quantity such time, distance, cost etc.
So binary search can be applied in all many optimisation problems of the min( max (....) ) or max(min(...) ) given the problem has a monotonic search space!
private static int binarySearch(int[] stalls, int C) {
}
private static int binarySearch(int[] stalls, int C) {
int n = stalls.length;
Arrays.sort(stalls);
int s = 0;
int e = stalls[n-1] - stalls[0];
}
private static int binarySearch(int[] stalls, int C) {
int n = stalls.length;
Arrays.sort(stalls);
int s = 0;
int e = stalls[n-1] - stalls[0];
int current_ans;
while(s<=e){
int mid = (s+e)/2;
}
return current_ans;
}
private static int binarySearch(int[] stalls, int C) {
int n = stalls.length;
Arrays.sort(stalls);
int s = 0;
int e = stalls[n-1] - stalls[0];
int current_ans;
while(s<=e){
int mid = (s+e)/2;
if(canPlace(stalls,mid,C)){
current_ans = mid;
s = mid + 1;
}
}
return current_ans;
}
private static int binarySearch(int[] stalls, int C) {
int n = stalls.length;
Arrays.sort(stalls);
int s = 0;
int e = stalls[n-1] - stalls[0];
int current_ans;
while(s<=e){
int mid = (s+e)/2;
if(canPlace(stalls,mid,C)){
current_ans = mid;
s = mid + 1;
}
else{
e = mid - 1;
}
}
}
private static int binarySearch(int[] stalls, int C) {
int n = stalls.length;
Arrays.sort(stalls);
int s = 0;
int e = stalls[n-1] - stalls[0];
int current_ans;
while(s<=e){
int mid = (s+e)/2;
if(canPlace(stalls,mid,C)){
current_ans = mid;
s = mid + 1;
}
else{
e = mid - 1;
}
}
return current_ans;
}
private static int binarySearch(int[] stalls, int C) {
int n = stalls.length;
Arrays.sort(stalls);
int s = 0;
int e = stalls[n-1] - stalls[0];
int current_ans;
while(s<=e){
int mid = (s+e)/2;
if(canPlace(stalls,mid,C)){
current_ans = mid;
s = mid + 1;
}
else{
e = mid - 1;
}
}
return current_ans;
}
private boolean canPlace(int[] stalls, int minSep, int C){
int cows=1;
int lastCowX = stalls[0];
}
private static int binarySearch(int[] stalls, int C) {
int n = stalls.length;
Arrays.sort(stalls);
int s = 0;
int e = stalls[n-1] - stalls[0];
int current_ans;
while(s<=e){
int mid = (s+e)/2;
if(canPlace(stalls,mid,C)){
current_ans = mid;
s = mid + 1;
}
else{
e = mid - 1;
}
}
return current_ans;
}
private boolean canPlace(int[] stalls, int minSep, int C){
int cows=1;
int lastCowX = stalls[0];
for(int i=1; i<stalls.size(); i++){
//place a cow if dist of current stall - prev >= threshold
}
}
private static int binarySearch(int[] stalls, int C) {
int n = stalls.length;
Arrays.sort(stalls);
int s = 0;
int e = stalls[n-1] - stalls[0];
int current_ans;
while(s<=e){
int mid = (s+e)/2;
if(canPlace(stalls,mid,C)){
current_ans = mid;
s = mid + 1;
}
else{
e = mid - 1;
}
}
return current_ans;
}
private boolean canPlace(int[] stalls, int minSep, int C){
int cows=1;
int lastCowX = stalls[0];
for(int i=1; i<stalls.size(); i++){
//place a cow if dist of current stall - prev >= threshold
if(stalls[i] - lastCowX >= minDist){
cows++;
lastCowX = stalls[i];
}
}
}
private static int binarySearch(int[] stalls, int C) {
int n = stalls.length;
Arrays.sort(stalls);
int s = 0;
int e = stalls[n-1] - stalls[0];
int current_ans;
while(s<=e){
int mid = (s+e)/2;
if(canPlace(stalls,mid,C)){
current_ans = mid;
s = mid + 1;
}
else{
e = mid - 1;
}
}
return current_ans;
}
private boolean canPlace(int[] stalls, int minSep, int C){
int cows=1;
int lastCowX = stalls[0];
for(int i=1; i<stalls.size(); i++){
//place a cow if dist of current stall - prev >= threshold
if(stalls[i] - lastCowX >= minDist){
cows++;
lastCowX = stalls[i];
if(cows==C){
return true;
}
}
}
}
private static int binarySearch(int[] stalls, int C) {
int n = stalls.length;
Arrays.sort(stalls);
int s = 0;
int e = stalls[n-1] - stalls[0];
int current_ans;
while(s<=e){
int mid = (s+e)/2;
if(canPlace(stalls,mid,C)){
current_ans = mid;
s = mid + 1;
}
else{
e = mid - 1;
}
}
return current_ans;
}
private boolean canPlace(int[] stalls, int minSep, int C){
int cows=1;
int lastCowX = stalls[0];
for(int i=1; i<stalls.size(); i++){
//place a cow if dist of current stall - prev >= threshold
if(stalls[i] - lastCowX >= minDist){
cows++;
lastCowX = stalls[i];
if(cows==C){
return true;
}
}
}
return false;
}
private static int binarySearch(int[] stalls, int C) {
int n = stalls.length;
Arrays.sort(stalls);
int s = 0;
int e = stalls[n-1] - stalls[0];
int current_ans;
while(s<=e){
int mid = (s+e)/2;
if(canPlace(stalls,mid,C)){
current_ans = mid;
s = mid + 1;
}
else{
e = mid - 1;
}
}
return current_ans;
}
Let's discuss space and time
private static int binarySearch(int[] stalls, int C) {
int n = stalls.length;
Arrays.sort(stalls);
int s = 0;
int e = stalls[n-1] - stalls[0];
int current_ans;
while(s<=e){
int mid = (s+e)/2;
if(canPlace(stalls,mid,C)){
current_ans = mid;
s = mid + 1;
}
else{
e = mid - 1;
}
}
return current_ans;
}
private boolean canPlace(int[] stalls, int minSep, int C){
int cows=1;
int lastCowX = stalls[0];
for(int i=1; i<stalls.size(); i++){
//place a cow if dist of current stall - prev >= threshold
if(stalls[i] - lastCowX >= minDist){
cows++;
lastCowX = stalls[i];
if(cows==C){
return true;
}
}
}
return false;
}
private static int binarySearch(int[] stalls, int C) {
int n = stalls.length;
Arrays.sort(stalls);
int s = 0;
int e = stalls[n-1] - stalls[0];
int current_ans;
while(s<=e){
int mid = (s+e)/2;
if(canPlace(stalls,mid,C)){
current_ans = mid;
s = mid + 1;
}
else{
e = mid - 1;
}
}
return current_ans;
}
private boolean canPlace(int[] stalls, int minSep, int C){
int cows=1;
int lastCowX = stalls[0];
for(int i=1; i<stalls.size(); i++){
//place a cow if dist of current stall - prev >= threshold
if(stalls[i] - lastCowX >= minDist){
cows++;
lastCowX = stalls[i];
if(cows==C){
return true;
}
}
}
return false;
}
private static int binarySearch(int[] stalls, int C) {
int n = stalls.length;
Arrays.sort(stalls);
int s = 0;
int e = stalls[n-1] - stalls[0];
int current_ans;
while(s<=e){
int mid = (s+e)/2;
if(canPlace(stalls,mid,C)){
current_ans = mid;
s = mid + 1;
}
else{
e = mid - 1;
}
}
return current_ans;
}