combinations - Given a sorted array find number of all unique pairs which sum is greater than X -


is there solution can achieved complexity better o(n^2)? linear complexity best o(nlogn) great.

i've tried use binary search each element o(nlogn) im missing something.

for example, given sorted array: 2 8 8 9 10 20 21 , number x=18 [2,20][2,21][8,20][8,21][8,20][8,21][9,10][9,20][9,21][10,20][10,21][20,21] function should return 12. help!

for problem, given set of numbers, there may which, included other number, guaranteed solution because in greater target amount summing.

identify those. combination of solution. example-data, 21 , 20 satisfy this, combinations of 21, six, , remaining combinations of 20, five, satisfy requirement (11 results far).

treat sorted array subset, excludes above set (if non-empty).

from new subset, find numbers can included number in subset satisfy requirement. starting highest (for "complexity" may not matter, ease of coding, knowing have none helps) number in subset, identify any, remembering there need @ least 2 such numbers more results added identified.

take data in successive adjacent pairs. once reach of sum not meet requirement, know have found bottom limit table.

with sequence:

1 2 12 13 14 15 16 17 18 19 

19 meets criteria. subset of 9 numbers. take 18 , 17. meets criteria. take 17 , 16. meets. 16 , 15. meets... 13 , 12. meets. 12 , 2. not meet. 12 lowest value in range, , number of items in range seven.

this process yields 10 , 9 original data. 1 more combination, giving 12 combinations sought.

determining number of combinations should abstracted out, simple calculate and, depending on actual situation, perhaps faster pre-calculate.

a rule-of-thumb if you, yourself, can answer looking @ data, pretty easy computer. doing same way starting point, though not end.


find subset of highest elements cause sum greater desired value when paired any other member of array.

taking subset excludes those, use highest element , work backwards find lowest element gives success. have third subset.

your combinations of entire group members of first subset, plus solely combinations of third subset.

you student, you'll have work out how complex is.


Comments

Popular posts from this blog

c - How to retrieve a variable from the Apache configuration inside the module? -

c# - Constructor arguments cannot be passed for interface mocks -

python - malformed header from script index.py Bad header -