codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

1154B.md (1869B)


      1 Source: https://codeforces.com/problemset/problem/1154/B
      2 
      3 B. Make Them Equal
      4 
      5 time limit per test
      6 
      7 2 seconds
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 You are given a sequence $a_1, a_2, \dots, a_n$ consisting of $n$ integers.
     22 
     23 You can choose any non-negative integer $D$ (i.e. $D \ge 0$), and for each $a_i$ you can:
     24 
     25 * add $D$ (only once), i. e. perform $a_i := a_i + D$, or
     26 * subtract $D$ (only once), i. e. perform $a_i := a_i - D$, or
     27 * leave the value of $a_i$ unchanged.
     28 
     29 It is possible that after an operation the value $a_i$ becomes negative.
     30 
     31 Your goal is to choose such minimum non-negative integer $D$ and perform changes in such a way, that all $a_i$ are equal (i.e. $a_1=a_2=\dots=a_n$).
     32 
     33 Print the required $D$ or, if it is impossible to choose such value $D$, print -1.
     34 
     35 For example, for array $[2, 8]$ the value $D=3$ is minimum possible because you can obtain the array $[5, 5]$ if you will add $D$ to $2$ and subtract $D$ from $8$. And for array $[1, 4, 7, 7]$ the value $D=3$ is also minimum possible. You can add it to $1$ and subtract it from $7$ and obtain the array $[4, 4, 4, 4]$.
     36 
     37 Input
     38 
     39 The first line of the input contains one integer $n$ ($1 \le n \le 100$) — the number of elements in $a$.
     40 
     41 The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 100$) — the sequence $a$.
     42 
     43 Output
     44 
     45 Print one integer — the minimum non-negative integer value $D$ such that if you add this value to some $a_i$, subtract this value from some $a_i$ and leave some $a_i$ without changes, all obtained values become equal.
     46 
     47 If it is impossible to choose such value $D$, print -1.
     48 
     49 Examples
     50 
     51 Input
     52 
     53 ```
     54 6
     55 1 4 4 7 4 1
     56 ```
     57 
     58 Output
     59 
     60 ```
     61 3
     62 ```
     63 
     64 Input
     65 
     66 ```
     67 5
     68 2 2 5 2 5
     69 ```
     70 
     71 Output
     72 
     73 ```
     74 3
     75 ```
     76 
     77 Input
     78 
     79 ```
     80 4
     81 1 3 3 7
     82 ```
     83 
     84 Output
     85 
     86 ```
     87 -1
     88 ```
     89 
     90 Input
     91 
     92 ```
     93 2
     94 2 8
     95 ```
     96 
     97 Output
     98 
     99 ```
    100 3
    101 ```