Created
September 4, 2017 20:09
-
-
Save thmain/53b493ce212d15761d8a17800aaa29dc 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
import java.util.Arrays; | |
public class LocalMaxORMin { | |
private static void find(int[] a) { | |
if(a==null||a.length==0){ | |
System.out.println("No Local maximum or minimum"); | |
return; | |
} | |
int size = a.length; | |
int first_element = a[0]; | |
int last_element = a[size-1]; | |
if(first_element+size-1==last_element || first_element-size+1==last_element){ | |
System.out.println("No Local maximum or minimum"); | |
return; | |
} | |
if(first_element<a[1]){ | |
//lets find local maximum | |
int last_should_be = first_element+(size-1); | |
int local_max = (last_should_be+last_element)/2; | |
System.out.println("local maximum: " + local_max); | |
}else{ | |
//lets find local maximum | |
int last_should_be = first_element-(size-1); | |
int local_min = (last_should_be+last_element)/2; | |
System.out.println("local minimum: " + local_min); | |
} | |
} | |
public static void main(String[] args) { | |
int a [] = {3,4,5,4,3,2,1,0,-1}; | |
System.out.print(Arrays.toString(a)); | |
find(a); | |
int b [] = {-4,-5,-6,-5,-4,-3}; | |
System.out.print(Arrays.toString(b)); | |
find(b); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment