Last active
July 12, 2020 13:40
-
-
Save codertcet111/8ffba50231e7d7508bb87e59e3161dd4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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