Skip to content

Instantly share code, notes, and snippets.

@nichtemna
Created August 10, 2016 08:15
Show Gist options
  • Save nichtemna/b9b2b44b3616b35fa188805b13a07084 to your computer and use it in GitHub Desktop.
Save nichtemna/b9b2b44b3616b35fa188805b13a07084 to your computer and use it in GitHub Desktop.
Get sum of two biggest integers in array
static long getMaxPairwiseProduct(int[] numbers) {
long result = 0;
int n = numbers.length;
int max_position1 = -1;
for (int i = 0; i < numbers.length; i++) {
if ((max_position1 == -1) || (numbers[i] > numbers[max_position1])) {
max_position1 = i;
}
}
int max_position2 = -1;
for (int i = 0; i < numbers.length; i++) {
if (((max_position2 == -1) || (numbers[i] > numbers[max_position2]))
&& (max_position1 != i)) {
max_position2 = i;
}
}
System.out.println(numbers[max_position1] + " " + numbers[max_position2]);
result = numbers[max_position1] + numbers[max_position2];
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment