Created
May 1, 2021 05:47
-
-
Save syedjafer/d761dafc107f77e9e10b9fab2e43046d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| class MinMax { | |
| int min; | |
| int max; | |
| MinMax(int min, int max) | |
| { | |
| min = min; | |
| max = max; | |
| } | |
| } | |
| public class MinMaxFinder{ | |
| static MinMax find_min_max(int[] arr, int low, int high){ | |
| MinMax min_max = new MinMax(-1, -1); | |
| if (low == high){ | |
| min_max.min = arr[low]; | |
| min_max.max = arr[low]; | |
| } | |
| else if(low+1 == high){ | |
| if (arr[low] > arr[high]){ | |
| min_max.min = arr[high]; | |
| min_max.max = arr[low]; | |
| } | |
| else{ | |
| min_max.min = arr[low]; | |
| min_max.max = arr[high]; | |
| } | |
| } | |
| else{ | |
| int mid = (low+high)/2; | |
| MinMax min_max_1 = find_min_max(arr, low, mid); | |
| MinMax min_max_2 = find_min_max(arr, mid+1, high); | |
| // Setting Values | |
| if (min_max_1.min < min_max_2.min){ | |
| min_max.min = min_max_1.min; | |
| } | |
| else{ | |
| min_max.min = min_max_2.min; | |
| } | |
| if (min_max_1.max > min_max_2.max){ | |
| min_max.max = min_max_1.max; | |
| } | |
| else{ | |
| min_max.max = min_max_2.max; | |
| } | |
| } | |
| return min_max; | |
| } | |
| public static void main(String []args){ | |
| int arr[] = {-1, -1, 9, 0, -2}; | |
| MinMax res = find_min_max(arr, 0, arr.length-1); | |
| System.out.println("Min "+res.min+" Max "+res.max); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment