@@ -0,0 +1,1392 @@
+package com.freetymekiyan.algorithms.level.medium;%0A%0A/**%0A * Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a%0A * specific target number.%0A * %3Cp%3E%0A * The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must%0A * be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.%0A * %3Cp%3E%0A * You may assume that each input would have exactly one solution.%0A * %3Cp%3E%0A * Input: numbers=%7B2, 7, 11, 15%7D, target=9%0A * Output: index1=1, index2=2%0A * %3Cp%3E%0A * Tags: Array, Two Pointers, Binary Search%0A * Similar: Problems (E) Two Sum%0A */%0Apublic class TwoSum2 %7B%0A%0A    /**%0A     * Two pointers.%0A     * Use two indices from both ends of the array and compare sum with target.%0A     * If target is larger, move the end index.%0A     * If target is smaller, move the start index.%0A     * Stop till we find the target.%0A     */%0A    public int%5B%5D twoSum(int%5B%5D numbers, int target) %7B%0A        int start = 0;%0A        int end = numbers.length - 1;%0A        while (start %3C end) %7B%0A            long sum = numbers%5Bstart%5D + numbers%5Bend%5D;%0A            if (sum %3E target) %7B%0A                end--;%0A            %7D else if (sum %3C target) %7B%0A                start++;%0A            %7D else %7B%0A                break;%0A            %7D%0A        %7D%0A        return new int%5B%5D%7Bstart + 1, end + 1%7D;%0A    %7D%0A%7D%0A

add Two Sum 2