Statistics

Problem Statement for "SumK"

Problem Statement

Some numbers can be expressed as the sum of k consecutive numbers. 9 = -1 + 0 + 1 + 2 + 3 + 4 so 9 can be expressed as the sum of 6 consecutive numbers. Your program will be given a collection of values and a positive k, and it will report how many of the values in the collection could be expressed as the sum of k consecutive numbers.

Definition

Class:
SumK
Method:
count
Parameters:
int, int[]
Returns:
int
Method signature:
int count(int k, int[] values)
(be sure your method is public)

Notes

  • If a value is repeated, count it each time it appears if it is a sum of k.
  • Values may be negative or zero, but k will be positive.

Constraints

  • k is between 1 and 1,000,000,000 (inclusive)
  • each element in values is between -1,000,000,000 and 1,000,000,000 (inclusive)
  • the number of elements in values is between 0 and 50 (inclusive)

Examples

  1. 3

    {-987,5,0}

    Returns: 2

    -987 = -330 + -329 + -328, 5 is not the sum of 3 consecutive values, 0 = -1 + 0 + 1

  2. 1000

    {0,-500,499500,499501,17,-500}

    Returns: 3

    -500 + -499 + ... + 499 = -500, 0 + 1 + ... + 999 = 499500, and neither 499501 nor 17 nor 0 can be expressed as the sum of 1000 consecutive values. (-500 is counted twice since it appears twice in values.)

  3. 1

    {3,4,5,-1000000000}

    Returns: 4

    Each value is the sum of itself

  4. 2

    {}

    Returns: 0

  5. 2

    {-999999999}

    Returns: 1

  6. 1

    {-9,-1,0,1,2,999999999}

    Returns: 6

  7. 2

    {-999999999,0,0,77,77,77,78,78,78,78}

    Returns: 4

  8. 3

    {622,623,623,624,624,624,-625,-625,-625,-625}

    Returns: 3

  9. 3

    {0,-1,-1,345,345,345,346,346,346,346,347,347,347,347,347}

    Returns: 4

  10. 911911911

    {0,0,0,0,1,1,911911911,911911911,911911911,911911912}

    Returns: 7

  11. 1000000000

    {-500000000,-1000000000,-1000000000,999999999,999999999,999999999}

    Returns: 1

  12. 4

    {4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4}

    Returns: 0

  13. 3

    {-987,5,0}

    Returns: 2

  14. 1000

    {0,-500,499500,499501,17,-500}

    Returns: 3

  15. 1

    {3,4,5,-1000000000}

    Returns: 4

  16. 2

    {}

    Returns: 0

  17. 999999999

    {1}

    Returns: 0


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: