Find a pair of elements from an array whose sum equals a given number

后端 未结 30 1060
暗喜
暗喜 2020-11-22 10:14

Given array of n integers and given a number X, find all the unique pairs of elements (a,b), whose summation is equal to X.

The following is my solution, it is O(nLo

相关标签:
30条回答
  • 2020-11-22 10:38

    Python Implementation:

    import itertools
    list = [1, 1, 2, 3, 4, 5,]
    uniquelist = set(list)
    targetsum = 5
    for n in itertools.combinations(uniquelist, 2):
        if n[0] + n[1] == targetsum:
            print str(n[0]) + " + " + str(n[1])
    

    Output:

    1 + 4
    2 + 3
    
    0 讨论(0)
  • 2020-11-22 10:39

    https://github.com/clockzhong/findSumPairNumber

    I did it under O(m+n) complexity cost for both time and memory space. I suspect that's the best algorithm so far.

    0 讨论(0)
  • 2020-11-22 10:40

    O(n)

    def find_pairs(L,sum):
        s = set(L)
        edgeCase = sum/2
        if L.count(edgeCase) ==2:
            print edgeCase, edgeCase
        s.remove(edgeCase)      
        for i in s:
            diff = sum-i
            if diff in s: 
                print i, diff
    
    
    L = [2,45,7,3,5,1,8,9]
    sum = 10          
    find_pairs(L,sum)
    

    Methodology: a + b = c, so instead of looking for (a,b) we look for a = c - b

    0 讨论(0)
  • 2020-11-22 10:40

    One Solution can be this, but not optimul (The complexity of this code is O(n^2)):

    public class FindPairsEqualToSum {
    
    private static int inputSum = 0;
    
    public static List<String> findPairsForSum(int[] inputArray, int sum) {
        List<String> list = new ArrayList<String>();
        List<Integer> inputList = new ArrayList<Integer>();
        for (int i : inputArray) {
            inputList.add(i);
        }
        for (int i : inputArray) {
            int tempInt = sum - i;
            if (inputList.contains(tempInt)) {
                String pair = String.valueOf(i + ", " + tempInt);
                list.add(pair);
            }
        }
        return list;
       }
    }
    
    0 讨论(0)
  • 2020-11-22 10:41

    A Simple program in java for arrays having unique elements:

    import java.util.*;
    public class ArrayPairSum {
        public static void main(String[] args) { 
            int []a = {2,4,7,3,5,1,8,9,5};
            sumPairs(a,10);  
        }
    
        public static void sumPairs(int []input, int k){
          Set<Integer> set = new HashSet<Integer>();    
          for(int i=0;i<input.length;i++){
    
            if(set.contains(input[i]))
                System.out.println(input[i] +", "+(k-input[i]));
            else
                set.add(k-input[i]);
           }
        }
    }
    
    0 讨论(0)
  • 2020-11-22 10:41

    less than o(n) solution will be=>

    function(array,k)
              var map = {};
              for element in array
                 map(element) = true;
                 if(map(k-element)) 
                     return {k,element}
    
    0 讨论(0)
提交回复
热议问题