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

前端 未结 15 2044
隐瞒了意图╮
隐瞒了意图╮ 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:42

    Assume required sum = R

    1. sort the array
    2. for each number in the array A(n), do a binary search to find the number A(x) such that A(n) + A(x) = R

提交回复
热议问题