Run Code
|
API
|
Code Wall
|
Users
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Blog
Two Sum in Sorted input array
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
//'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; } }
[
+
]
Show input
Compilation time: 0.73 sec, absolute running time: 0.16 sec, cpu time: 0.15 sec, memory peak: 18 Mb, absolute service time: 0,9 sec
edit mode
|
history
|
discussion
1 3
stackse - search stackoverflow differently