Created
May 1, 2021 05:08
-
-
Save syedjafer/5b9a27e309577387723132eb31f6618d 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){ | |
| MinMax min_max = new MinMax(-1, -1); | |
| if (arr.length == 1){ | |
| min_max.min = arr[0]; | |
| min_max.max = arr[0]; | |
| } | |
| else if(arr.length > 1){ | |
| if (arr[0] > arr[1]){ | |
| min_max.min = arr[1]; | |
| min_max.max = arr[0]; | |
| } | |
| else{ | |
| min_max.min = arr[0]; | |
| min_max.max = arr[1]; | |
| } | |
| } | |
| for (int itr=2;itr<arr.length;itr++){ | |
| if (arr[itr]>min_max.max){ | |
| min_max.max = arr[itr]; | |
| } | |
| else if(arr[itr]<min_max.min){ | |
| min_max.min = arr[itr]; | |
| } | |
| } | |
| return min_max; | |
| } | |
| public static void main(String []args){ | |
| int arr[] = {-1, -1}; | |
| MinMax res = find_min_max(arr); | |
| 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