Find all pairs of integers within an array which sum to a specified value

前端 未结 15 1920
隐瞒了意图╮
隐瞒了意图╮ 2020-12-01 08:00

Design an algorithm to find all pairs of integers within an array which sum to a specified value.

I have tried this problem using a hash

相关标签:
15条回答
  • 2020-12-01 08:46

    Creating a hash table and then looking for value in it.

    function sum_exist(num : number, arr : any[]) {
        var number_seen = {};
        for(let item of arr){
            if(num - item in number_seen){
                return true
            }
            number_seen[item] = 0;
        }
        return false;
    }
    

    Test case (using Jest)

    test('Given a list of numbers, return whether any two sums equal to the set number.', () => {
        expect(sum_exist(17 , [10, 15, 3, 7])).toEqual(true);
    });
    
    
    test('Given a list of numbers, return whether any two sums equal to the set number.', () => {
        expect(sum_exist(16 , [10, 15, 3, 7])).toEqual(false);
    });
    
    0 讨论(0)
  • 2020-12-01 08:49
    @Test
    public void hasPairWithSum() {
      assertFalse(hasPairWithSum_Ordered_Logarithmic(new int[] { 1, 2, 3, 9 }, 8));
      assertTrue(hasPairWithSum_Ordered_Logarithmic(new int[] { 1, 2, 4, 4 }, 8));
    
      assertFalse(hasPairWithSum_Ordered_Linear(new int[] { 1, 2, 3, 9 }, 8));
      assertTrue(hasPairWithSum_Ordered_Linear(new int[] { 1, 2, 4, 4 }, 8));
    
      assertFalse(hasPairWithSum_Unsorted_Linear(new int[] { 9, 1, 3, 2 }, 8));
      assertTrue(hasPairWithSum_Unsorted_Linear(new int[] { 4, 2, 1, 4 }, 8));
    
      assertFalse(hasPairWithSum_Unsorted_Quadratic(new int[] { 9, 1, 3, 2 }, 8));
      assertTrue(hasPairWithSum_Unsorted_Quadratic(new int[] { 4, 2, 1, 4 }, 8));
    }
    
    private boolean hasPairWithSum_Ordered_Logarithmic(int[] data, int sum) {
      for (int i = 0; i < data.length; i++) {
        int current = data[i];
        int complement = sum - current;
        int foundIndex = Arrays.binarySearch(data, complement);
        if (foundIndex >= 0 && foundIndex != i) {
          return true;
        }
      }
      return false;
    }
    
    private boolean hasPairWithSum_Ordered_Linear(int[] data, int sum) {
      int low = 0;
      int high = data.length - 1;
      while (low < high) {
        int total = data[low] + data[high];
        if (total == sum) {
          return true;
        } else if (total < sum) {
          low++;
        } else {
          high--;
        }
      }
      return false;
    }
    
    private boolean hasPairWithSum_Unsorted_Linear(int[] data, int sum) {
      Set<Integer> complements = Sets.newHashSet();
      for (int current : data) {
        if (complements.contains(current)) {
          return true;
        }
        complements.add(sum - current);
      }
      return false;
    }
    
    private boolean hasPairWithSum_Unsorted_Quadratic(int[] data, int sum) {
      for (int i = 0; i < data.length; i++) {
        int current = data[i];
        int complement = sum - current;
        for (int j = 0; j < data.length; j++) {
          if (data[j] == complement && i != j) {
            return true;
          }
        }
      }
      return false;
    }
    
    0 讨论(0)
  • 2020-12-01 08:50

    We can use C++ STL map to solve this

    void subsetSum(int arr[], int n, int sum)
    {
        map<int, int>Map;
    
        for(int i=0; i<n; i++)
        {
            Map[arr[i]]++;
            if(Map.count(sum-arr[i]))
            {
                cout<<arr[i]<<" "<<sum-arr[i]<<"\n";
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题