Created
August 10, 2016 08:15
-
-
Save nichtemna/b9b2b44b3616b35fa188805b13a07084 to your computer and use it in GitHub Desktop.
Get sum of two biggest integers in array
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
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