Skip to main content
Question Protected by tchrist
edited title
Link
Arslan Ali
  • 17.9k
  • 9
  • 64
  • 85

Finding three elements in an array whose sum is closest to ana given number

added 176 characters in body
Source Link
ZelluX
  • 73.6k
  • 20
  • 76
  • 106

Given an array of integers, A1, A2, ..., An, including negatives and positives, and another integer S. Now we need to find three different integers in the array, whose sum is closest to the given integer S. If there exists more than one solution, any of them is ok.

You can assume all the integers are within int32_t range, and no arithmetic overflow will occur with calculating the sum. S is nothing special but a randomly picked number.

Is there any efficient algorithm other than brute force search to find the three integers?

Given an array of integers, A1, A2, ..., An, including negatives and positives, and another integer S. Now we need to find three different integers in the array, whose sum is closest to the given integer S. If there exists more than one solution, any of them is ok.

Is there any efficient algorithm other than brute force search to find the three integers?

Given an array of integers, A1, A2, ..., An, including negatives and positives, and another integer S. Now we need to find three different integers in the array, whose sum is closest to the given integer S. If there exists more than one solution, any of them is ok.

You can assume all the integers are within int32_t range, and no arithmetic overflow will occur with calculating the sum. S is nothing special but a randomly picked number.

Is there any efficient algorithm other than brute force search to find the three integers?

Source Link
ZelluX
  • 73.6k
  • 20
  • 76
  • 106
Loading