How to find all ordered pairs of elements in array of integers whose sum lies in a given range of value

akashchandrakar

Given an array of integers find the number of all ordered pairs of elements in the array whose sum lies in a given range [a,b]

Here is an O(n^2) solution for the same

'''
counts all pairs in array such that the 
sum of pair lies in the range a and b
'''
def countpairs(array, a, b):
    num_of_pairs = 0
    for i in range(len(array)):
        for j in range(i+1,len(array)):
            total = array[i] + array[j]
            if total >= a and total <= b:
                num_of_pairs += 1
    return num_of_pairs

I know my solution is not optimal What is a better algorithm for doing this.

Ani
  1. Sort the array (say in increasing order).
  2. For each element x in the array:
    • Consider the array slice after the element.
    • Do a binary search on this array slice for [a - x], call it y0. If no exact match is found, consider the closest match bigger than [a - x] as y0.
    • Output all elements (x, y) from y0 forwards as long as x + y <= b

The time complexity is of course output-sensitive, but this is still superior to the existing algo:

O(nlogn) + O(k)

where k is the number of pairs that satisfy the condition.

Note: If you only need to count the number of pairs, you can do it in O(nlogn). Modify the above algorithm so [b - x] (or the next smaller element) is also searched for. This way, you can count the number of 'matches' each element has in O(logn) simply from the indices of the first and last match. Then it's just a question of summing those up to get the final count. This way, the initial O(nlogn) sorting step is dominant.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Find Pair Of Integers in Array whose Sum is Given Number in PHP

From Dev

Given 4 numbers of array of 1 to 10 elements. Find 3 numbers whose sum can generate all the four numbers?

From Dev

Find the pairs (2 Elements) from an array whose product must be greater then given Integer

From Dev

Determining the pairs of integers that sum to some value in the array

From Dev

Find the minimum set of integers whose sum is greater than a given integer

From Dev

given array find all combinations of elements that give sum

From Dev

Sum of all possible pairs of elements in an array

From Dev

Find all subtrees in a BST whose keys lie in a given range

From Dev

Find Range in which value lies in Java

From Dev

Find Range in which value lies in Java

From Dev

Find numbers of subarray of an array whose sum is divided by given number

From Dev

how can i find the sum of all integers given in the string by using JavaScript?

From Dev

How to find all subsets whose sum adds up to the value of a target value using dynamic programming in C

From Dev

find pairs which sum equal to given value with o(n) complexcity

From Dev

Mongoose - find all documents whose array property contains a given subset?

From Dev

How to SUM the number of duplicates in a range by given value?

From Dev

How to "or" all elements in the range of an array

From Dev

Given key\value pairs, find all rules that satisfy and return true

From Dev

Given an array of integers, output[i] = product of all elements besides itself

From Dev

Sum up all the integers in range()

From Dev

Given an interval stored as [{name, value},...], how would I find where x lies?

From Dev

Given an array of integers, find out the third largest value in the array

From Dev

How to find number of integers in a given range has even set bits

From Dev

Find all integers between m and n whose sum of squared divisors is itself a square

From Dev

How to quickly check if an Integer lies within a given range

From Dev

How to search the numerical range where a given numerical digit lies in between?

From Dev

How to convert an array to a hash with array elements as hash keys and all hash values all set to given value

From Dev

How to convert an array to a hash with array elements as hash keys and all hash values all set to given value

From Dev

Find all array subsequences of a given value

Related Related

  1. 1

    Find Pair Of Integers in Array whose Sum is Given Number in PHP

  2. 2

    Given 4 numbers of array of 1 to 10 elements. Find 3 numbers whose sum can generate all the four numbers?

  3. 3

    Find the pairs (2 Elements) from an array whose product must be greater then given Integer

  4. 4

    Determining the pairs of integers that sum to some value in the array

  5. 5

    Find the minimum set of integers whose sum is greater than a given integer

  6. 6

    given array find all combinations of elements that give sum

  7. 7

    Sum of all possible pairs of elements in an array

  8. 8

    Find all subtrees in a BST whose keys lie in a given range

  9. 9

    Find Range in which value lies in Java

  10. 10

    Find Range in which value lies in Java

  11. 11

    Find numbers of subarray of an array whose sum is divided by given number

  12. 12

    how can i find the sum of all integers given in the string by using JavaScript?

  13. 13

    How to find all subsets whose sum adds up to the value of a target value using dynamic programming in C

  14. 14

    find pairs which sum equal to given value with o(n) complexcity

  15. 15

    Mongoose - find all documents whose array property contains a given subset?

  16. 16

    How to SUM the number of duplicates in a range by given value?

  17. 17

    How to "or" all elements in the range of an array

  18. 18

    Given key\value pairs, find all rules that satisfy and return true

  19. 19

    Given an array of integers, output[i] = product of all elements besides itself

  20. 20

    Sum up all the integers in range()

  21. 21

    Given an interval stored as [{name, value},...], how would I find where x lies?

  22. 22

    Given an array of integers, find out the third largest value in the array

  23. 23

    How to find number of integers in a given range has even set bits

  24. 24

    Find all integers between m and n whose sum of squared divisors is itself a square

  25. 25

    How to quickly check if an Integer lies within a given range

  26. 26

    How to search the numerical range where a given numerical digit lies in between?

  27. 27

    How to convert an array to a hash with array elements as hash keys and all hash values all set to given value

  28. 28

    How to convert an array to a hash with array elements as hash keys and all hash values all set to given value

  29. 29

    Find all array subsequences of a given value

HotTag

Archive