Created
February 18, 2020 16:08
-
-
Save Angehl/e8a19813f83218d670548e41fb5e469b 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
| import java.util.Arrays; | |
| public class Principal { | |
| public static int findMin(int input[]){ | |
| System.out.println("Min steps: 1"); | |
| if (input[0] < input[input.length-1]) | |
| return input[0]; | |
| return input[input.length-1]; | |
| } | |
| public static int findMax(int input[]){ | |
| int i = 0; | |
| int result[] = input; | |
| while(result.length >1){ | |
| i++; | |
| int half = result.length/2; | |
| int half1[] = Arrays.copyOfRange(result, 0, half); | |
| int half2[] = Arrays.copyOfRange(result, half, result.length); | |
| if(half1[half1.length-1] > half2[0]) | |
| result=half1; | |
| else | |
| result=half2; | |
| } | |
| System.out.println("Max steps: " + i); | |
| return result[0]; | |
| } | |
| public static void main (String args[]){ | |
| //{4,9,13,25,21,20,18,9,1} | |
| int arr[] = {1,2,3,5,9,18,24,29,1}; | |
| System.out.println("Size: " + arr.length); | |
| System.out.println("Min:" + findMin(arr)); | |
| System.out.println("Max:" + findMax(arr)); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment