Skip to content

Instantly share code, notes, and snippets.

@thmain
Created July 30, 2017 21:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thmain/b998f8894cc460f6db6d94af83423ba5 to your computer and use it in GitHub Desktop.
Save thmain/b998f8894cc460f6db6d94af83423ba5 to your computer and use it in GitHub Desktop.
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