Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Two Sum in Sorted input array
//'main' method must be in a class 'Rextester'. //Compiler version 1.8.0_111 import java.util.*; import java.lang.*; //Two sum in sorted input array class Rextester { public static void main(String args[]) { for(int index:twoSum(new int[]{2,7,13,19,21},15)) System.out.print(index+" "); } public static int[] twoSum(int[] numbers, int target) { for(int i=0,j=numbers.length-1;i<j;){ int total = numbers[i]+numbers[j]; if(total==target) return new int[]{i+1,j+1}; if(total>target) j--; else if(total<target) i++; } return null; } }
run
|
edit
|
history
|
help
0
Granite Java
test1
bikeride
preincrement
java palindrome check
Max palindrome string
calc exp gxb2
ex25
1a
Nick-test