TopcoderArchive
SRM2 Nov 2006
SRM 325

ModularInequality

Problem statement, definition, constraints, and public examples.

Problem Statement

You will be given a int[] A that contains N elements and an int P. Return the number of distinct integer values of X for which the following inequality is true:

|A0 - X| + |A1 - X| + ... + |AN-1 - X| ≤ P

Definition

Class:
ModularInequality
Method:
countSolutions
Parameters:
int[], int
Returns:
int
Method signature:
int countSolutions(int[] A, int P)
(be sure your method is public)

Constraints

  • A will contain between 1 and 50 elements, inclusive.
  • Each element of A will be between -1,000,000,000 and 1,000,000,000 inclusive.
  • P will be between 0 and 1,000,000,000 inclusive.

Examples

  1. {1, 2, 3}
    6
    Returns: 5
    The possible values for X are 0, 1, 2, 3 and 4.
  2. {10, 30, 15, -1, 17}
    42
    Returns: 7
  3. {0, 2, 3, -5, 10}
    17
    Returns: 0
  4. {-693}
    1265
    Returns: 2531
  5. {965, -938, -396, -142, 926, 31, -720}
    6495
    Returns: 1781
Back to all problems