Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,884 questions

51,810 answers

573 users

How to implement the two sum algorithm to find two values in array that add up to target with Java

3 Answers

0 votes
public class MyClass {
    private static int[] twoSum(int[] array, int target) {
        for (int i = 0; i < array.length; i++) {
            for (int j = i + 1; j < array.length; j++) {
                if (array[i] + array[j] == target) {
                    return new int[] { i, j };
                }
            }
        }
        return new int[] {};
    }
    public static void main(String args[]) {
        int[] array = {1, 5, 7, 6, 4, 3, 2};

        int target = 9;

        int[] indexes = twoSum(array, target);

        if (indexes.length == 2) {
            System.out.println(indexes[0] + " " + indexes[1]);
        } else {
            System.out.println("Not found");
        }
    }
}





/*
run:
  
1 4 
  
*/

 



answered Jul 17, 2023 by avibootz
0 votes
import java.util.Map;
import java.util.HashMap;

public class MyClass {
    private static int[] twoSum(int[] array, int target) {
        Map<Integer, Integer> hmap = new HashMap<>();
        
        for (int i = 0; i < array.length; i++) {
            int complement = target - array[i];
            if (hmap.containsKey(complement)) {
                return new int[] { hmap.get(complement), i };
            } else {
                hmap.put(array[i], i);
            }
        }
        
        return new int[] {};
    }
    public static void main(String args[]) {
        int[] array = {1, 5, 7, 6, 4, 3, 2};

        int target = 9;

        int[] indexes = twoSum(array, target);

        if (indexes.length == 2) {
            System.out.println(indexes[0] + " " + indexes[1]);
        } else {
            System.out.println("Not found");
        }
    }
}





/*
run:
  
1 4 
  
*/

 



answered Jul 17, 2023 by avibootz
0 votes
import java.util.Arrays;

public class MyClass {
    private static int[] twoSum(int[] array, int target) {
        Arrays.sort(array);
        int left = 0, right = array.length - 1;
         
        while (left < right) {
            if (array[left] + array[right] == target) {
                return new int[] {array[right], array[left]};
            } else if (array[right] + array[left] < target) {
                left++;
            } else {
                right--;
            }
        }
         
        return new int[] {};
    }
    public static void main(String args[]) {
        int[] array = {1, 5, 7, 6, 4, 38, 23};
 
        int target = 9;
 
        int[] values = twoSum(array, target);
 
        if (values.length == 2) {
            System.out.println(values[0] + " " + values[1]);
        } else {
            System.out.println("Not found");
        }
    }
}
 
 
 
 
 
/*
run:
   
5 4
   
*/

 



answered Jul 17, 2023 by avibootz
edited Jul 17, 2023 by avibootz
...