find the closest sum to a target value
2014-08-27 03:35
633 查看
problem: given an array of integers including positive and negative, a target value. find 2 numbers in the array such that the sum of the 2 numbers is the closest to the target value.
Solution: this can be solved in O(nlogn).
First, invest some time to sort the array, O(nlogn).
Then,
Solution: this can be solved in O(nlogn).
First, invest some time to sort the array, O(nlogn).
Then,
int min= Integer.MAX; while(i< j){ if(A[i]+ A[j]== T) return (i,j); (A[i]+ A[j]- T> 0)? j--: i++; }
相关文章推荐
- Subarray Sum to the given target value
- closest node to the target in BST
- There is integer array like {1,2,4,5,6,1,2,4,3,5,7,2,1}. I want to find the possible combination of pair which sum is 4.
- failed to find target android-7 Please install the missing platform
- The origin server did not find a current representation for the target resource or is not willing to
- The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
- The origin server did not find a current representation for the target resource or is not willing to
- Find k closest elements to a given value
- In a bunch of number to find out the biggest minimum value
- org.apache.axis2.AxisFault: The AXIS engine could not find a target service to invoke! targetServic
- The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.报该错误的一种原因。
- The origin server did not find a current representation for the target resource or is not willing to
- Given a tree, find the node with the minimum sum of distances to other nodes
- Groupon面经:Find paths in a binary tree summing to a target value
- The origin server did not find a current representation for the target resource or is not willing to
- Unable to find the Xcode project `.xcodeproj` for the target `Pods`
- Closest node to the target in BST
- Two Sum - Closest to target
- perfect squares find the least number of perfect square numbers (1, 4, 9, 16, ...) which sum to n
- Q4.9 Find all paths which sum to a given value