Number of Subarray whose sum greater than given value

fyquah95

Given an array of N integers (positive and negative), find the number of contiguous sub array whose sum is greater or equal to K (also, positive or negative)

I have managed to work out a naive O(N2) solution, is it possible to get better?

kraskevich

Yes, it is possible to do it in O(n log n) time.

  1. Let's take a look at prefix sums. A sum of a (L, R] subarray is prefixSum[R] - prefixSum[L].

  2. It means that we can count the number of such L and R that L < R and prefixSum[R] - prefixSum[L] >= K, which means prefixSum[L] <= prefixSum[R] - K.

  3. Let's iterate over the array of prefix sums from left to right and maintain a data structure that can do the following operations efficiently :

    • Add a new element.
    • Find the number of elements less than or equal to a given number.

    We can use a balanced binary search tree for this purpose.

Here is a pseudo-code of this solution:

tree = an empty search tree
result = 0
// This sum corresponds to an empty prefix.
prefixSum = 0
tree.add(prefixSum) 
// Iterate over the input array from left to right.
for elem <- array:
    prefixSum += elem
    // Add the number of subarrays that have this element as the last one
    // and their sum is not less than K.
    result += tree.getNumberOfLessOrEqual(prefixSum - K) 
    // Add the current prefix sum the tree.
    tree.add(prefixSum)
print result

The time complexity is O(n log n) because there are O(n) add and count number of elements operations, and each of them can be done in O(log n).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Smallest sum of subarray with sum greater than a given value

From Dev

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

From Dev

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

From Dev

how to find the length of the longest contiguous subarray whose sum is divisible by a given number

From Dev

Find number greater than given parameter in regex

From Dev

Counting Number of Users Whose Average is Greater than X in Postgres

From Dev

Finding subarray with given sum

From Dev

Excel: count the number of cells in a column until their sum is greater than a set value

From Dev

smallest number greater than given number which is a palindrome

From Dev

max. distance of a number greater than a given number in array

From Dev

mysql value greater than percent of number of values

From Dev

Find permutations of a given set of numbers whose sum APPROACH a target number

From Dev

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

From Dev

MySQL delete all rows where id is greater than the given number

From Dev

How to delete files if a numerical part of their name is greater than a given number?

From Dev

Maximal subset sum smaller than a given value

From Dev

Binary tree - Delete nodes whose level is greater or equal than given one

From Dev

sum if greater than in r

From Dev

program to find perfect number : error in output . perfect number is number whose sum of factors equals the given number

From Dev

Waterline (Sails.js). Rows with 'updatedAt' greater than a given value

From Dev

Matplotlib histogram - plotting values greater than a given value

From Dev

Waterline (Sails.js). Rows with 'updatedAt' greater than a given value

From Dev

List files in ascending order and greater than a given value

From Dev

Plot only values greater than given value: ggplot R

From Dev

MySQL: Select rows with values nearest but greater than a given value

From Dev

Generate n random numbers whose sum is m and all numbers should be greater than zero

From Dev

How to select where sum of fields is greater than a value in MongoDB

From Dev

Maximum subarray with given number of elements

From Dev

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

Related Related

  1. 1

    Smallest sum of subarray with sum greater than a given value

  2. 2

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

  3. 3

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

  4. 4

    how to find the length of the longest contiguous subarray whose sum is divisible by a given number

  5. 5

    Find number greater than given parameter in regex

  6. 6

    Counting Number of Users Whose Average is Greater than X in Postgres

  7. 7

    Finding subarray with given sum

  8. 8

    Excel: count the number of cells in a column until their sum is greater than a set value

  9. 9

    smallest number greater than given number which is a palindrome

  10. 10

    max. distance of a number greater than a given number in array

  11. 11

    mysql value greater than percent of number of values

  12. 12

    Find permutations of a given set of numbers whose sum APPROACH a target number

  13. 13

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

  14. 14

    MySQL delete all rows where id is greater than the given number

  15. 15

    How to delete files if a numerical part of their name is greater than a given number?

  16. 16

    Maximal subset sum smaller than a given value

  17. 17

    Binary tree - Delete nodes whose level is greater or equal than given one

  18. 18

    sum if greater than in r

  19. 19

    program to find perfect number : error in output . perfect number is number whose sum of factors equals the given number

  20. 20

    Waterline (Sails.js). Rows with 'updatedAt' greater than a given value

  21. 21

    Matplotlib histogram - plotting values greater than a given value

  22. 22

    Waterline (Sails.js). Rows with 'updatedAt' greater than a given value

  23. 23

    List files in ascending order and greater than a given value

  24. 24

    Plot only values greater than given value: ggplot R

  25. 25

    MySQL: Select rows with values nearest but greater than a given value

  26. 26

    Generate n random numbers whose sum is m and all numbers should be greater than zero

  27. 27

    How to select where sum of fields is greater than a value in MongoDB

  28. 28

    Maximum subarray with given number of elements

  29. 29

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

HotTag

Archive