Skip to content

Instantly share code, notes, and snippets.

@codertcet111
Last active July 12, 2020 13:40
Show Gist options
  • Save codertcet111/8ffba50231e7d7508bb87e59e3161dd4 to your computer and use it in GitHub Desktop.
Save codertcet111/8ffba50231e7d7508bb87e59e3161dd4 to your computer and use it in GitHub Desktop.
function get_min_and_max(int i, int j){
//get the minimum and maximum values for the caller array
//If the array has a single element then
if (i == j) {
max, min = a[i]
}
else if (i == j-1) { //If the array has two elements, The Problem is too small to solve.
if (a[i] < a[j]) {
max = a[j]
min = a[i]
}
else{
max = a[i]
min = a[j]
}
}
else{ //The problem is too big to solve, hence need to divide into subproblems
mid_val = (i+j)/ 2
min_1, max_1 = get_min_and_max(i, mid_val)
min_2, max_2 = get_min_and_max(mid_val + 1, j)
//Now combine the solution of the subproblems
if min_1 < min_2{
min = min_1
}
else{
min = min_2
}
if max_1 > max_2{
max = max_1
}
else{
max = max_2
}
}
return min, max
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment