Created
July 30, 2017 21:42
-
-
Save thmain/b998f8894cc460f6db6d94af83423ba5 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
public class MaxDifferenceBruteForce { | |
public static int maxDifference(int [] A){ | |
int maxDiff = -1; | |
for (int i = 0; i <A.length ; i++) { | |
for (int j = i; j <A.length ; j++) { | |
if(A[j]>A[i] && (A[j]-A[i]>maxDiff)) | |
maxDiff = A[j]-A[i]; | |
} | |
} | |
return maxDiff; | |
} | |
public static void main(String[] args) { | |
int [] A = { 2, 5, 1, 7, 3, 4, 9, 4, 5}; | |
System.out.println("Maximum Difference between two elements A[i] and A[j] and where j > i: " + maxDifference(A)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment